Python 基本ansible自定义模块,触摸文件

Python 基本ansible自定义模块,触摸文件,python,ansible,Python,Ansible,我正试图写一个模块,将触摸一个基于用户输入的概念验证目的的文件。例如,以下touch.yml和touch.py文件应接触名为foo.txt的文件: touch.yml: --- - hosts: localhost tasks: - name: testing python in ansible touch: fname: foo.txt 库/touch.py #!/usr/bin/python from ansible.module_utils

我正试图写一个模块,将触摸一个基于用户输入的概念验证目的的文件。例如,以下touch.yml和touch.py文件应接触名为foo.txt的文件:

touch.yml:

---
- hosts: localhost   
  tasks:
    - name: testing python in ansible
      touch:
        fname: foo.txt
库/touch.py

#!/usr/bin/python

from ansible.module_utils.basic import *

def filename(fname):
    open(fname, 'a').close()

def main():
        module_args = dict(
            fname=dict(type='str', required=False)
        )
        module = AnsibleModule(
                     argument_spec=module_args,
                     supports_check_mode=False
                 )
        fname = module.params['fname']
        result = dict(
            changed=False
        )

        module.exit_json(changed=False)

这是不正确的,我希望有人能帮我理顺逻辑,把问题联系起来。

我解决了这个问题。对于任何想了解如何用python编写ansible模块的人,请注意module.params[“…”],这是指向模块的playbook yaml键的链接

#!/usr/bin/python
from ansible.module_utils.basic import *
import json

def main():

        module_args = {
                "name": {"default": True, "type": "str"},
                "surname": {"default": True, "type": "str"}
                }
        module = AnsibleModule(
                     argument_spec=module_args,
                     supports_check_mode=False
                 )

        open(module.params["name"], 'a').close()

        module.exit_json(changed=False, meta=module_args)

if __name__ == '__main__':
    main()
这里我接触的是一个名为“name”的文件,它将在yaml playbook键中指定。yaml看起来像:

---
- hosts: localhost
  tasks:
    - name: testing python in ansible
      mymodulename:
        name: "testing123"
      register: result

    - debug: var=result

这将涉及一个名为“testing123”的文件。

如果您说出了“不正确”在您的案例中的含义,这将有助于我们帮助您。它是否产生了错误?是否找不到您的
触摸屏
模块?还有别的吗?或者你只是在找一个新的工作?