Ansible 在剧本末尾打印主机失败的任务?
我正在多台主机上同时运行部署。正如所料,输出在运行时会快速移动,很难跟踪每个任务结束时的状态。当我讲到剧本的结尾时,我可以看到哪个主持人失败了,这很好。然而,我需要滚动页面和输出页面,以找出某个主机在哪个任务上失败 有没有一种方法可以在剧本末尾打印出来,比如说:Ansible 在剧本末尾打印主机失败的任务?,ansible,Ansible,我正在多台主机上同时运行部署。正如所料,输出在运行时会快速移动,很难跟踪每个任务结束时的状态。当我讲到剧本的结尾时,我可以看到哪个主持人失败了,这很好。然而,我需要滚动页面和输出页面,以找出某个主机在哪个任务上失败 有没有一种方法可以在剧本末尾打印出来,比如说: “主机1在任务1/cmd上失败”不知道这是否完全符合您的问题,但您可以通过以下方式进行一些异常处理: --- - hosts: localhost any_errors_fatal: true tasks: - blo
“主机1在任务1/cmd上失败”不知道这是否完全符合您的问题,但您可以通过以下方式进行一些异常处理:
---
- hosts: localhost
any_errors_fatal: true
tasks:
- block:
- name: "Fail a command"
shell: |
rm someNonExistentFile
rescue:
- debug:
msg: "{{ ansible_failed_result }}"
#- fail:
# msg: "Playbook run failed. Aborting..."
# uncomment this failed section to actually fail a deployment after writing the error message
TASK [debug] ************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": {
"changed": true,
"cmd": "rm someNonExistentFile\n",
"delta": "0:00:00.036509",
"end": "2019-10-31 12:06:09.579806",
"failed": true,
"invocation": {
"module_args": {
"_raw_params": "rm someNonExistentFile\n",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"msg": "non-zero return code",
"rc": 1,
"start": "2019-10-31 12:06:09.543297",
"stderr": "rm: cannot remove ‘someNonExistentFile’: No such file or directory",
"stderr_lines": [
"rm: cannot remove ‘someNonExistentFile’: No such file or directory"
],
"stdout": "",
"stdout_lines": [],
"warnings": [
"Consider using the file module with state=absent rather than running 'rm'. If you need to use command because file is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message."
]
}
}
变量ansible\u failed\u result
包含如下内容:
---
- hosts: localhost
any_errors_fatal: true
tasks:
- block:
- name: "Fail a command"
shell: |
rm someNonExistentFile
rescue:
- debug:
msg: "{{ ansible_failed_result }}"
#- fail:
# msg: "Playbook run failed. Aborting..."
# uncomment this failed section to actually fail a deployment after writing the error message
TASK [debug] ************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": {
"changed": true,
"cmd": "rm someNonExistentFile\n",
"delta": "0:00:00.036509",
"end": "2019-10-31 12:06:09.579806",
"failed": true,
"invocation": {
"module_args": {
"_raw_params": "rm someNonExistentFile\n",
"_uses_shell": true,
"argv": null,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"stdin_add_newline": true,
"strip_empty_ends": true,
"warn": true
}
},
"msg": "non-zero return code",
"rc": 1,
"start": "2019-10-31 12:06:09.543297",
"stderr": "rm: cannot remove ‘someNonExistentFile’: No such file or directory",
"stderr_lines": [
"rm: cannot remove ‘someNonExistentFile’: No such file or directory"
],
"stdout": "",
"stdout_lines": [],
"warnings": [
"Consider using the file module with state=absent rather than running 'rm'. If you need to use command because file is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message."
]
}
}
如果适用,我通常使用stderr
。否则我使用“{{ansible\u failed\u result}to\u nice\u json}”
hth检查回调插件中的可操作插件(仅显示失败事件)是否有帮助:。