以下代码结果'dict object' has no attribute 'failed'
错误:
shell模块:
- hosts: localhost
tasks:
- shell: "not_available_command"
register: module_result
until: module_result.failed == false
retries: 2
delay: 1
结果:
fatal: [localhost]: FAILED! => {
"failed": true,
"msg": "The conditional check 'module_result.failed == false' failed. The error was: error while evaluating conditional (module_result.failed == false): 'dict object' has no attribute 'failed'"
}
但是当我将until
的语法更改为:
until: module_result.stderr == ""
成功重试。我用-vvv
选项执行它来进行调试;在2次重试后,它按预期失败:
fatal: [localhost]: FAILED! => {
"attempts": 2,
"changed": true,
"cmd": "not_available_command",
"delta": "0:00:00.010290",
"end": "2017-09-25 17:28:14.078318",
"failed": true,
"invocation": {
"module_args": {
"_raw_params": "not_available_command",
"_uses_shell": true,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"warn": true
}
},
"rc": 127,
"start": "2017-09-25 17:28:14.068028",
"stderr": "/bin/sh: 1: not_available_command: not found",
"stderr_lines": [
"/bin/sh: 1: not_available_command: not found"
],
"stdout": "",
"stdout_lines": []
}
如前所述,failed
或stderr
属性均已存在,但在尝试定义failed
语法时无法识别until
。
让我们尝试使用不同的模块:
get_url module:
- hosts: localhost
tasks:
- get_url:
url: "http://unavailable_file_on_the_web.txt"
dest: "{{ playbook_dir }}"
register: module_result
until: module_result.failed == false
retries: 2
delay: 1
此时until: module_result.failed == false
语法有效; 'failed'
属性已被识别。
那么如何在模块上识别这个问题呢?它似乎是一个错误吗?
答案 0 :(得分:4)
是的,它似乎是一个错误或预期的行为:failed
字段在中间尝试期间未填充。您可以使用ANSIBLE_DEBUG=1
:
{
"changed": true,
"end": "2017-09-25 13:01:17.269225",
"stdout": "",
"cmd": "not_available_command",
"rc": 127,
"start": "2017-09-25 13:01:17.257604",
"stderr": "/bin/sh: not_available_command: command not found",
"delta": "0:00:00.011621",
"invocation": {
"module_args": {
"warn": true,
"executable": null,
"_uses_shell": true,
"_raw_params": "not_available_command",
"removes": null,
"creates": null,
"chdir": null
}
},
"warnings": []
}
在Ansible 2.4中,failed
字段存在。
有关failed
/ succeeded
的{{3}},请提供相关信息
您可以在when/until
语句中使用它们:
until: module_result | succeeded
这在2.2.x和2.3.x中运行良好。