Db0
Db0

Reputation: 191

How to bypass failed ansible tasks, but still report them as failed?

I have a playbook, which just runs a series of checks on my inventory, and I want to see how many failed on each host.

---

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

All check task files look like this

---

- block:
  - name: check backups
    command: /usr/checks/check_backups
    changed_when: False
    register: OUTPUT_CHECK_backups
    tags: backups

However in this way, if the first check on a host fails, the rest of the checks won't be run at all.

I could put ignore_errors:yes on each individual check task, but then the Play Recap will report that all was checked OK on that host.

Is there a way to avoid a failed task blocking all other tasks, and still get a proper play recap of all failed tasks?

Upvotes: 0

Views: 1065

Answers (2)

Vladimir Botka
Vladimir Botka

Reputation: 68004

Rescuable erros can be handled by rescue. In this case "failed_when: false" is not needed to proceed, I think.

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

For example to include_tasks check[1-3].yml

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

gives (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

Upvotes: 1

Db0
Db0

Reputation: 191

As a workaround, I've put the following "dummy" rescue block in each task


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

But I'm not sure if there's a "proper" way to do this instead.

Upvotes: 0

Related Questions