# rescue for target failed connection question using localhost

**URL:** <https://forum.ansible.com/t/rescue-for-target-failed-connection-question-using-localhost/38355>\
**Category:** Ansible Project\
**Created:** [August 21, 2023, 6:16pm UTC](https://forum.ansible.com/t/rescue-for-target-failed-connection-question-using-localhost/38355 "2023-08-21T18:16:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Barros](https://avatars.discourse-cdn.com/v4/letter/d/7993a0/32.png) [@Daniel\_Barros](https://forum.ansible.com/u/Daniel_Barros)\
**Post date:** [August 21, 2023, 6:16pm UTC](https://forum.ansible.com/t/rescue-for-target-failed-connection-question-using-localhost/38355/1 "2023-08-21T18:16:58Z")

</div>

Hi everyone. Good day.

When we have a regular task like creating a file, or something similar, we can use block and rescue to manage what happens in failure cases.

But can we do the same for the first task (connection) on a target and rescue using localhost?

What I would like to achieve is this.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [August 21, 2023, 6:23pm UTC](https://forum.ansible.com/t/rescue-for-target-failed-connection-question-using-localhost/38355/2 "2023-08-21T18:23:35Z")

</div>

I just did something very similar:

- name: Check Nodes  
&nbsp;&nbsp;hosts: all  
&nbsp;&nbsp;gather\_facts: false  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;- name: ping hosts  
&nbsp;&nbsp;&nbsp;&nbsp;ping:  
&nbsp;&nbsp;&nbsp;&nbsp;ignore\_errors: True  
&nbsp;&nbsp;&nbsp;&nbsp;ignore\_unreachable: True  
&nbsp;&nbsp;&nbsp;&nbsp;register: ping\_results

- hosts: localhost  
&nbsp;&nbsp;gather\_facts: false  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;- name: Add failed hosts to failed\_group  
&nbsp;&nbsp;&nbsp;&nbsp;add\_host:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hostname: "{{ item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;groups: unreachable  
&nbsp;&nbsp;&nbsp;&nbsp;when: hostvars[item]['ping\_results'] is unreachable  
&nbsp;&nbsp;&nbsp;&nbsp;loop: '{{groups["all"]}}'

&nbsp;&nbsp;- name: just for show, but here you can do any actions like  
'bootstraping' the machines, removing from inventory, etc  
&nbsp;&nbsp;&nbsp;&nbsp;debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ groups['unreachable'] }}"

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [August 21, 2023, 6:25pm UTC](https://forum.ansible.com/t/rescue-for-target-failed-connection-question-using-localhost/38355/3 "2023-08-21T18:25:31Z")

</div>

I forgot to mention, the reason your rescue does not work is because  
the task did not fail, but the host was unreachable.  
block/rescue/always only acts on task failure, not host being  
unreachable.

In my example above I ignore unreachable and task errors so the '2nd  
play' can handle them differently.

---

<div class="post-metadata">

**Author:** ![Daniel\_Barros](https://avatars.discourse-cdn.com/v4/letter/d/7993a0/32.png) [@Daniel\_Barros](https://forum.ansible.com/u/Daniel_Barros)\
**Post date:** [August 21, 2023, 11:41pm UTC](https://forum.ansible.com/t/rescue-for-target-failed-connection-question-using-localhost/38355/4 "2023-08-21T23:41:11Z")

</div>

Hi Brian.

Using ignore unreachable and failing based on the condition of the register variable based on your example did the trick for me.

I appreciate your time and sharing.  
Br
