# Ansible react on unreachable / failed hosts

**URL:** <https://forum.ansible.com/t/ansible-react-on-unreachable-failed-hosts/31633>\
**Category:** Ansible Project\
**Created:** [November 11, 2019, 12:03pm UTC](https://forum.ansible.com/t/ansible-react-on-unreachable-failed-hosts/31633 "2019-11-11T12:03:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![profhase85](https://avatars.discourse-cdn.com/v4/letter/p/2bfe46/32.png) [@profhase85](https://forum.ansible.com/u/profhase85)\
**Post date:** [November 11, 2019, 12:03pm UTC](https://forum.ansible.com/t/ansible-react-on-unreachable-failed-hosts/31633/1 "2019-11-11T12:03:00Z")

</div>

I am administering a heterogeneous environment with a lot of stakeholders. It would be crucial to react on unreachable and failed hosts differently. So far my solution is:

`

- hosts: all:localhost  
tasks:

# If fact gathering fails - \> host unreachable

- set\_fact:  
unreachable\_hosts: “{{ ansible\_play\_hosts\_all | difference(ansible\_play\_hosts) }}”  
reachable\_hosts: “{{ ansible\_play\_hosts }}”  
delegate\_to: localhost  
run\_once: True

- import\_role: …

# if the role has failed

- set\_fact:  
failed\_deployment: “{{ reachable\_hosts | difference(ansible\_play\_hosts) }}”  
delegate\_to: localhost  
run\_once: True

# Here could be the notification modules

- debug:  
msg: “unreachable: {{ unreachable\_hosts }}, failed: {{ failed\_connection }}”  
delegate\_to: localhost  
run\_once: True

`

It seems a little clumsy to me. Is there a way to check if a host is reachable with a module?  
Is this the standard way of handling failed connections?

Thanks

---

<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:** [November 11, 2019, 1:09pm UTC](https://forum.ansible.com/t/ansible-react-on-unreachable-failed-hosts/31633/2 "2019-11-11T13:09:38Z")

</div>

It's possible to test the connection, register and use the result. For example

&nbsp;&nbsp;&nbsp;&nbsp;- delegate\_to: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: ping -c1 "{{ inventory\_hostname }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: result  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ignore\_errors: true

Your solution is cleaner. I'd propose to create a group of reachable  
hosts and structure the the playbook into more plays. For example

&nbsp;&nbsp;&nbsp;&nbsp;- name: Create group of reachable hosts  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hosts: all  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gather\_facts: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- add\_host:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: '{{ item }}'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;groups: 'reachable'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ ansible\_play\_hosts }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run\_once: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unreachable\_hosts: "{{ ansible\_play\_hosts\_all|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_facts: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run\_once: true

&nbsp;&nbsp;&nbsp;&nbsp;- name: Manage reachable hosts  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hosts: reachable  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gather\_facts: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: inventory\_hostname

&nbsp;&nbsp;&nbsp;&nbsp;- name: Manage unreachable hosts  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hosts: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gather\_facts: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: item  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ unreachable\_hosts }}"

Notes:

1) "gather\_facts: true" in the first play is necessary to  
&nbsp;&nbsp;&nbsp;remove unreachable hosts from "ansible\_play\_hosts".

2) It's not necessary to delegate "add\_hosts".

3) It's necessary "delegate\_facts: true" to declare the variable  
&nbsp;&nbsp;&nbsp;"unreachable\_hosts" in the "hostvars['localhost']".

Cheers,

&nbsp;&nbsp;-vlado
