如何绕过失败的ansible任务,但仍将其报告为失败?

如何绕过失败的ansible任务,但仍将其报告为失败?,ansible,Ansible,我有一个playbook,它只是对我的清单运行一系列检查,我想看看每个主机上有多少失败 --- - hosts: all gather_facts: False remote_user: root vars: CHECKSLIST: - check1 - check2 - check3 tasks: - name: Check All include_tasks: "check_{{ item }}.yml"

我有一个playbook,它只是对我的清单运行一系列检查,我想看看每个主机上有多少失败

---

- hosts: all
  gather_facts: False
  remote_user: root

  vars:
    CHECKSLIST:
      - check1
      - check2
      - check3  

  tasks:
  - name: Check All
    include_tasks: "check_{{ item }}.yml"
    with_items: "{{ CHECKSLIST }}"
所有检查任务文件如下所示

---

- block:
  - name: check backups
    command: /usr/checks/check_backups
    changed_when: False
    register: OUTPUT_CHECK_backups
    tags: backups
但是,通过这种方式,如果主机上的第一次检查失败,那么其余的检查将根本不会运行

我可以在每个单独的检查任务上设置ignore_errors:yes,但是播放回放将报告在该主机上所有检查都正常


有没有一种方法可以避免失败的任务阻塞所有其他任务,并且仍然能够正确地重述所有失败的任务?

作为一种解决方法,我在每个任务中都设置了以下“虚拟”救援块



但我不确定是否有一种“正确”的方法来代替它。

可补救的错误可以通过以下方式处理。在这种情况下,我认为不需要继续“failed_when:false”

- block:
    - name: check backups
      command: /usr/checks/check_backups
      register: OUTPUT_CHECK_backups
  rescue:
    - debug: var=OUTPUT_CHECK_backups
  tags: backups
例如,要包含任务检查[1-3]。yml

- block:
    - command: /bin/false
      register: result
  rescue:
    - debug: msg="check1 failed."
    - debug: var=result
给出(grep消息:)


是的,我正在使用这种方法以及一种变通方法(见我自己的答案)。事实上,我刚刚改进了它,所以我使用rescue而不是调试来保存特定检查失败的主机的主机列表
- block:
    - command: /bin/false
      register: result
  rescue:
    - debug: msg="check1 failed."
    - debug: var=result
msg: non-zero return code
msg: check1 failed.
  msg: non-zero return code
msg: non-zero return code
msg: check2 failed.
  msg: non-zero return code
msg: non-zero return code
msg: check3 failed.
msg: non-zero return code