AnsibleModule对象不可调用错误

时间:2018-12-26 09:41:42

标签: python python-3.x ansible ansible-inventory

我的剧本如下

---
- hosts: nodes
  become: yes
  tasks:
  - name: Run Shell Script to get IPs with 4xx and 5xx errors

    script: /home/ubuntu/ips.sh
    args:
      chdir: /home/ubuntu
    register: ips

  - set_fact:
       iperrors: "{{ groups.nodes | map('extract', hostvars, 'ips') | map(attribute='stdout') | join('') }}"
    run_once: true
    delegate_to: localhost


  - name: Python Custom Module to get Top 5 Ips
    5ips:
      iperrors: "{{iperrors}}"
    run_once: true
    delegate_to: localhost
    register: 5ip

我已经编写了一个5ips的自定义模块,但是当我执行剧本时,它会引发如下错误

TASK [Python Custom Module to get Top 5 Ips] ***************************************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: TypeError: 'AnsibleModule' object is not callable
fatal: [54.183.110.130 -> localhost]: FAILED! => {"changed": false, "module_stderr": "Traceback (most recent call last):\n  File \"/home/ubuntu/.ansible/tmp/ansible-tmp-1545815995.35-86175850009970/AnsiballZ_5ips.py\", line 113, in <module>\n    _ansiballz_main()\n  File \"/home/ubuntu/.ansible/tmp/ansible-tmp-1545815995.35-86175850009970/AnsiballZ_5ips.py\", line 105, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/home/ubuntu/.ansible/tmp/ansible-tmp-1545815995.35-86175850009970/AnsiballZ_5ips.py\", line 48, in invoke_module\n    imp.load_module('__main__', mod, module, MOD_DESC)\n  File \"/tmp/ansible_5ips_payload_7Vi28k/__main__.py\", line 41, in <module>\n  File \"/tmp/ansible_5ips_payload_7Vi28k/__main__.py\", line 12, in main\nTypeError: 'AnsibleModule' object is not callable\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc"

我的5ips.py如下

#!/usr/bin/python

from ansible.module_utils.basic import *

def main():
    fields = {
        "iperrors" :{"required":True, "type":"str"}
        }
    module = AnsibleModule(argument_spec = fields)
    iperrors = module.params['iperrors']
    module(iperrors)

    if IpList !=0:
        module.exit_json(changed = True, msg = "top5 ips done")
    else:
        module.fail_json(changed = False, Error = "Something went wrong in Top5IPS.py")

def module(iperrors):
    ipErrors ={}
    IpList=[]

输入的iperrors im给出的是多行字符串。我不确定错误在哪里?

1 个答案:

答案 0 :(得分:0)

检查输出中的“ module_stderr”键。在结尾处说“ AnsibleModule”对象不可调用\ n”

我对自定义ansible模块不是很熟悉,但是看起来编写它们的方式已经改变,请在此处查看新文档:https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_general.html#developing-modules-general

或者,您可以尝试使用旧版本(可能为<2.0)的ansible。