AnsibleModule 对象不可调用错误
AnsibleModule object is not callable error
我的剧本如下
---
- 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=[]
我给出的输入错误是多行字符串。我不确定错误到底在哪里?
检查输出中的 "module_stderr" 键。接近尾声时它说“'AnsibleModule' 对象不是 callable\n”
我不太熟悉自定义 ansible 模块,但看起来编写它们的方式已经改变,请在此处查看新文档:https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_general.html#developing-modules-general
或者,您可以尝试使用较旧(<2.0,可能)版本的 ansible。
我的剧本如下
---
- 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=[]
我给出的输入错误是多行字符串。我不确定错误到底在哪里?
检查输出中的 "module_stderr" 键。接近尾声时它说“'AnsibleModule' 对象不是 callable\n”
我不太熟悉自定义 ansible 模块,但看起来编写它们的方式已经改变,请在此处查看新文档:https://docs.ansible.com/ansible/latest/dev_guide/developing_modules_general.html#developing-modules-general
或者,您可以尝试使用较旧(<2.0,可能)版本的 ansible。