我正在尝试在amazon-ec2中实现类似于模块的主机名和目标计算机。但是当我运行脚本时,它给了我下面的错误:
[ansible-user@ansible-master ~]$ ansible node1 -m edit_hostname.py -a node2
ERROR! this task 'edit_hostname.py' has extra params, which is only allowed in the following modules: meta, group_by, add_host, include_tasks, import_role, raw, set_fact, command, win_shell, import_tasks, script, shell, include_vars, include_role, include, win_command
我的模块是这样的:
#!/usr/bin/python
from ansible.module_utils.basic import *
try:
import json
except ImportError:
import simplejson as json
def write_to_file(module, hostname, hostname_file):
try:
with open(hostname_file, 'w+') as f:
try:
f.write("%s\n" %hostname)
finally:
f.close()
except Exception:
err = get_exception()
module.fail_json(msg="failed to write to the /etc/hostname file")
def main():
hostname_file = '/etc/hostname'
module = AnsibleModule(argument_spec=dict(name=dict(required=True, type=str)))
name = module.params['name']
write_to _file(module, name, hostname_file)
module.exit_json(changed=True, meta=name)
if __name__ == "__main__":
main()
我不知道我在哪里犯错。任何帮助将不胜感激。谢谢。
开发新模块时,建议使用boilerplate described in the documentation。这也表明您需要使用AnsibleModule
定义参数。
在main
中,您应该添加如下内容:
def main():
# define available arguments/parameters a user can pass to the module
module_args = dict(
name=dict(type='str', required=True)
)
# seed the result dict in the object
# we primarily care about changed and state
# change is if this module effectively modified the target
# state will include any data that you want your module to pass back
# for consumption, for example, in a subsequent task
result = dict(
changed=False,
original_hostname='',
hostname=''
)
module = AnsibleModule(
argument_spec=module_args
supports_check_mode=False
)
# manipulate or modify the state as needed (this is going to be the
# part where your module will do what it needs to do)
result['original_hostname'] = module.params['name']
result['hostname'] = 'goodbye'
# use whatever logic you need to determine whether or not this module
# made any modifications to your target
result['changed'] = True
# in the event of a successful module execution, you will want to
# simple AnsibleModule.exit_json(), passing the key/value results
module.exit_json(**result)
然后,您可以像这样调用模块:
ansible node1 -m mymodule.py -a "name=myname"
错误!此任务'edit_hostname.py'具有额外的参数,仅在以下模块中允许:meta,group_by,add_host,include_tasks,import_role,raw,set_fact,command,win_shell,import_tasks,脚本,shell,include_vars,include_role,include, win_command
正如您的错误消息所解释的那样,仅有限数量的模块支持匿名默认参数。在您的自定义模块中,您创建的参数称为name
。此外,您不应在模块名称中包括.py
扩展名。您必须像临时命令那样调用模块:
$ ansible node1 -m edit_hostname -a name=node2
我没有测试您的模块代码,因此您可能要修复其他错误。
与此同时,我仍然强烈建议您使用@Simon答案中所建议的ansible文档中的默认样板。