打印出剧本结尾时主机失败的任务?

时间:2019-10-31 10:57:32

标签: ansible

我正在多个主机上同时运行部署。可以预期,输出在运行时会快速移动,并且很难跟踪每个任务以什么状态结束。当我到达剧本的结尾时,我可以看到哪个主机失败了,这很棒。但是,我需要滚动浏览页面和输出页面,以找出某个主机在哪个任务上失败了。

有没有办法在剧本的末尾打印出来,例如: “主机1在任务1 / cmd上失败”

1 个答案:

答案 0 :(得分:0)

不知道这是否完全适合您的问题,但是您可以通过以下一些异常处理来帮助自己:

---

- 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

变量ansible_failed_result包含以下内容:

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_failed_result | to_nice_json }}"

hth