Get list of failed instances

20 views
Skip to first unread message

Vinita Shah

unread,
Jul 19, 2022, 1:39:49 AM7/19/22
to Ansible Project
I have roles in the playbook.

--- 
- hosts: platform_windows 
  gather_facts: yes 
  roles: 
    - role: xx 
    - role: xx

I have a dynamic inventory of hosts. How do I get a list of failed instances?
For e.g. the one with failed=1 state

10.x.x.x : ok=37 changed=2 unreachable=0 failed=1 skipped=0 rescued=0 
172.x.x.x : ok=37 changed=2 unreachable=0 failed=1 skipped=0 rescued=0 

The task/playbook output should be
10.x.x.x
172.x.x.x 

Also, where should I add the task/playbook

Vladimir Botka

unread,
Jul 19, 2022, 2:54:25 AM7/19/22
to Vinita Shah, ansible...@googlegroups.com
On Mon, 18 Jul 2022 22:39:49 -0700 (PDT)
Vinita Shah <vish...@colorado.edu> wrote:

> I have a dynamic inventory of hosts. *How do I get a list of failed
> instances?*

For example,

_failed: "{{ ansible_play_hosts_all|
difference(ansible_play_hosts) }}"

See
https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html#special-variables

Let's test it. Given the inventory

shell> cat hosts
host1 ok=true
host2 ok=false
host3 ok=false

The playbook

- hosts: all
gather_facts: false
vars:
_failed: "{{ ansible_play_hosts_all|
difference(ansible_play_hosts) }}"
tasks:
- assert:
that: ok|bool
- debug:
var: _failed

gives

_failed:
- host2
- host3


--
Vladimir Botka

Vinita Deepak Shah

unread,
Jul 19, 2022, 3:00:49 AM7/19/22
to Vladimir Botka, ansible...@googlegroups.com
Thank you. I tried adding debug after the roles.

: "{{ ansible_play_hosts_all|
                   difference(ansible_play_hosts) }}"
    
But it lists out the wrong instances.

Not sure if it’s coz of the dynamic inventory. 

Vinita Shah

unread,
Jul 20, 2022, 1:31:15 PM7/20/22
to Ansible Project
This helped. Thank you so much. Much appreciated. 
Reply all
Reply to author
Forward
0 new messages