1

我正在尝试在 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()

我不知道我在哪里犯错误。任何帮助将不胜感激。谢谢你。

4

2 回答 2

2

在开发新模块时,我建议使用文档中描述的样板。这也表明您需要使用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"
于 2020-06-12T19:04:38.580 回答
0

错误!此任务“edit_hostname.py”有额外的参数,仅允许在以下模块中使用: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

正如您的错误消息所解释的,匿名默认参数仅受有限数量的模块支持。在您的自定义模块中,您创建的参数称为name. 此外,您不应.py在模块名称中包含扩展名。您必须像这样调用您的模块作为临时命令:

$ ansible node1 -m edit_hostname -a name=node2

我没有测试您的模块代码,因此您可能需要修复更多错误。

同时,我仍然强烈建议您使用 @Simon 的回答中提出的 ansible 文档中的默认样板。

于 2020-06-13T06:36:16.440 回答