deps
指南
使用 deps
ansible_collections.community.general.plugins.module_utils.deps
模块实用程序简化了代码的导入,如导入和使用共享代码中所述。请注意,deps
旨在专门与 Ansible 模块一起使用,而不是其他类型的插件。
开发者指南中的相同示例将变为
from ansible_collections.community.general.plugins.module_utils import deps
with deps.declare("foo"):
import foo
然后在 main()
中,紧跟在 argspec 之后(或者代码中的任何位置),执行
deps.validate(module) # assuming module is a valid AnsibleModule instance
默认情况下,deps
将依赖于 ansible.module_utils.basic.missing_required_lib
来生成有关导入失败的消息。该函数接受参数 reason
和 url
,deps
也一样。
with deps.declare("foo", reason="foo is needed to properly bar", url="https://foo.bar.io"):
import foo
如果您希望编写自定义消息而不是使用 missing_required_lib
,则执行
with deps.declare("foo", msg="Custom msg explaining why foo is needed"):
import foo
deps
允许声明多个依赖项。
with deps.declare("foo"):
import foo
with deps.declare("bar"):
import bar
with deps.declare("doe"):
import doe
默认情况下,deps.validate()
将检查所有声明的依赖项,但如果需要,可以通过执行以下操作选择性地验证它们:
deps.validate(module, "foo") # only validates the "foo" dependency
deps.validate(module, "doe:bar") # only validates the "doe" and "bar" dependencies
deps.validate(module, "-doe:bar") # validates all dependencies except "doe" and "bar"
6.1.0 版本新增。