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

时间:2019-02-14 10:46:43

标签: ansible

我有一本剧本,它仅对我的清单进行一系列检查,我想查看每台主机上有多少故障。

---

- 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,但是随后,Play Recap将报告该主机上的所有检查均正常。

是否有办法避免失败的任务阻塞所有其他任务,并仍然对所有失败的任务进行适当的重述?

2 个答案:

答案 0 :(得分:1)

rescue可以处理可修复的错误。我认为,在这种情况下,不需要进行“ failed_when:false ”。

- block:
    - name: check backups
      command: /usr/checks/check_backups
      register: OUTPUT_CHECK_backups
  rescue:
    - debug: var=OUTPUT_CHECK_backups
  tags: backups

例如include_tasks check [1-3] .yml

- block:
    - command: /bin/false
      register: result
  rescue:
    - debug: msg="check1 failed."
    - debug: var=result

给予(grep msg:)

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

答案 1 :(得分:0)

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


- block:
  - name: check backups
    command: /usr/nagios/plugins/check_backups
    changed_when: False
    register: OUTPUT_CHECK_backups
    tags: backups
  rescue:
    - fail:
      failed_when: false 

但是我不确定是否有“适当”的方法来代替。