[英]Ansible Module Development: No module named ansible.runner?
I'm trying to write my first module in Ansible, which is essentially a wrapper around another module.我正在尝试用 Ansible 编写我的第一个模块,它本质上是另一个模块的包装器。 Here is my module:
这是我的模块:
#!/usr/bin/python
import ansible.runner
import sys
def main():
module.exit_json(changed=False)
from ansible.module_utils.basic import *
main()
and here is the error it gives me (stripped from 'msg'):这是它给我的错误(从'msg'中删除):
ImportError: No module named ansible.runner
导入错误:没有名为 ansible.runner 的模块
I am on ubuntu and installed ansible with aptitude, version is 1.9.1 Any ideas?我在 ubuntu 上并使用 aptitude 安装了 ansible,版本是 1.9.1 有什么想法吗?
Modules have to essentially be standalone.模块本质上必须是独立的。 The boilerplate gets injected at runtime (the text of the boilerplate replaces the import at the bottom), and the combined text of the module + boilerplate is squirted to the remote machine and run there.
样板在运行时被注入(样板的文本替换了底部的导入),并且模块 + 样板的组合文本被喷射到远程机器并在那里运行。 As such, you can't import things from ansible core like the runner (unless you install ansible on the remote machine- don't be that guy).
因此,您不能像 runner 一样从 ansible 核心导入东西(除非您在远程机器上安装 ansible - 不要成为那个人)。 "module" is one of the items that you have to create from stuff defined in the boilerplate.
“模块”是您必须根据样板文件中定义的内容创建的项目之一。 Here's a sample module skeleton I wrote:
这是我编写的示例模块骨架:
#! /usr/bin/python
import json
def main():
module = AnsibleModule(
argument_spec = dict(
state = dict(default='present', choices=['present', 'absent'])
),
supports_check_mode = True
)
p = module.params
changed = False
state = p['state']
if not module.check_mode:
# do stuff
pass
#module.fail_json(msg='it broke')
module.exit_json(changed=changed)
from ansible.module_utils.basic import *
main()
I just checked a module I wrote a while back and I don't have such an import line.我刚刚检查了我不久前写的一个模块,但我没有这样的导入行。 The only import I have is
from ansible.module_utils.basic import *
.我唯一的导入是
from ansible.module_utils.basic import *
。 The module
object I create myself in main
:我在
main
创建的module
对象:
module = AnsibleModule(
argument_spec=dict(
paramA=dict(required=True),
paramB=dict(required=False),
paramC=dict(required=False),
),
add_file_common_args=True,
supports_check_mode=True
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.