# Get list of failed instances

**URL:** <https://forum.ansible.com/t/get-list-of-failed-instances/36868>\
**Category:** Ansible Project\
**Created:** [July 19, 2022, 5:39am UTC](https://forum.ansible.com/t/get-list-of-failed-instances/36868 "2022-07-19T05:39:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vinita\_Shah1](https://avatars.discourse-cdn.com/v4/letter/v/76d3ee/32.png) [@Vinita\_Shah1](https://forum.ansible.com/u/Vinita_Shah1)\
**Post date:** [July 19, 2022, 5:39am UTC](https://forum.ansible.com/t/get-list-of-failed-instances/36868/1 "2022-07-19T05:39:49Z")

</div>

I have roles in the playbook.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [July 19, 2022, 6:54am UTC](https://forum.ansible.com/t/get-list-of-failed-instances/36868/2 "2022-07-19T06:54:03Z")

</div>

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

For example,

&nbsp;&nbsp;&nbsp;&nbsp;\_failed: "{{ ansible\_play\_hosts\_all|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;difference(ansible\_play\_hosts) }}"

See  
[https://docs.ansible.com/ansible/latest/reference\_appendices/special\_variables.html#special-variables](https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html#special-variables)

Let's test it. Given the inventory

&nbsp;&nbsp;\> cat hosts  
&nbsp;&nbsp;host1 ok=true  
&nbsp;&nbsp;host2 ok=false  
&nbsp;&nbsp;host3 ok=false

The playbook

&nbsp;&nbsp;- hosts: all  
&nbsp;&nbsp;&nbsp;&nbsp;gather\_facts: false  
&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_failed: "{{ ansible\_play\_hosts\_all|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;difference(ansible\_play\_hosts) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- assert:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;that: ok|bool  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: \_failed

gives

&nbsp;&nbsp;\_failed:  
&nbsp;&nbsp;&nbsp;&nbsp;- host2  
&nbsp;&nbsp;&nbsp;&nbsp;- host3

---

<div class="post-metadata">

**Author:** ![Vinita\_Deepak\_Shah](https://avatars.discourse-cdn.com/v4/letter/v/47e85d/32.png) [@Vinita\_Deepak\_Shah](https://forum.ansible.com/u/Vinita_Deepak_Shah)\
**Post date:** [July 19, 2022, 7:00am UTC](https://forum.ansible.com/t/get-list-of-failed-instances/36868/3 "2022-07-19T07:00:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Vinita\_Shah1](https://avatars.discourse-cdn.com/v4/letter/v/76d3ee/32.png) [@Vinita\_Shah1](https://forum.ansible.com/u/Vinita_Shah1)\
**Post date:** [July 20, 2022, 5:31pm UTC](https://forum.ansible.com/t/get-list-of-failed-instances/36868/4 "2022-07-20T17:31:15Z")

</div>

This helped. Thank you so much. Much appreciated.
