【问题标题】:Print out the task on which a host has failed at the end of a playbook?在剧本结束时打印出主机失败的任务?
【发布时间】:2019-10-31 20:17:08
【问题描述】:

我正在多个主机上同时运行部署。正如所料,输出在运行时快速移动,很难跟踪每个任务结束的状态。当我读完剧本时,我可以看到哪个主机失败了,这很棒。但是,我需要滚动页面和输出页面,以找出某个主机在哪个任务上失败了。

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

【问题讨论】:

标签: ansible


【解决方案1】:

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

---

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

【讨论】:

    猜你喜欢
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    • 2022-08-12
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 2020-01-09
    • 1970-01-01
    相关资源
    最近更新 更多