# How to get UNREACHABLE hosts in ansible-playbook

**URL:** <https://forum.ansible.com/t/how-to-get-unreachable-hosts-in-ansible-playbook/33783>\
**Category:** Ansible Project\
**Created:** [September 10, 2020, 6:18am UTC](https://forum.ansible.com/t/how-to-get-unreachable-hosts-in-ansible-playbook/33783 "2020-09-10T06:18:30Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Easy\_King](https://avatars.discourse-cdn.com/v4/letter/e/c2a13f/32.png) [@Easy\_King](https://forum.ansible.com/u/Easy_King)\
**Post date:** [September 10, 2020, 6:18am UTC](https://forum.ansible.com/t/how-to-get-unreachable-hosts-in-ansible-playbook/33783/1 "2020-09-10T06:18:30Z")

</div>

Below is my playbook

- name: Play 1.5 - Check each target  
hosts: all\_hosts  
ignore\_unreachable: yes  
ignore\_errors: yes  
gather\_facts: true

tasks:

- raw: “echo {{ inventory\_hostname }} is UNREACHABLE”  
delegate\_to: localhost  
when: 

I need help with the when condition in the above playbook.

When i run the play against unreachable hosts the debug output clearly shows that the output is in JSON format and there must a variable that captures inventory\_host connection status

Please see the output below:

TASK [Gathering Facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /app/Ansible/playbook/check/check.yml:55  
\<10.9.80.111\> Attempting python interpreter discovery  
\<10.9.80.111\> ESTABLISH SSH CONNECTION FOR USER: root  
\<10.9.80.111\> SSH: EXEC ssh -o ‘IdentityFile=“/app/automation/ssh\_keys/id\_rsa”’ -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ‘User=“root”’ -o ConnectTimeout=10 -o StrictHostKeyChecking=no 10.9.80.111 ‘/bin/sh -c ‘"’“'echo PLATFORM; uname; echo FOUND; command -v '”’“'”‘"’“'”‘"’“‘/usr/bin/python’”‘"’“'”‘"’“'”‘"’; command -v ‘"’“'”‘"’“'”‘"’“‘python3.7’”‘"’“'”‘"’“'”‘"’; command -v ‘"’“'”‘"’“'”‘"’“‘python3.6’”‘"’“'”‘"’“'”‘"’; command -v ‘"’“'”‘"’“'”‘"’“‘python3.5’”‘"’“'”‘"’“'”‘"’; command -v ‘"’“'”‘"’“'”‘"’“‘python2.7’”‘"’“'”‘"’“'”‘"’; command -v ‘"’“'”‘"’“'”‘"’“‘python2.6’”‘"’“'”‘"’“'”‘"’; command -v ‘"’“'”‘"’“'”‘"’“‘/usr/libexec/platform-python’”‘"’“'”‘"’“'”‘"’; command -v ‘"’“'”‘"’“'”‘"’“‘/usr/bin/python3’”‘"’“'”‘"’“'”‘"’; command -v ‘"’“'”‘"’“'”‘"’“‘python’”‘"’“'”‘"’“'”‘"’; echo ENDFOUND && sleep 0’“'”‘’  
\<10.9.80.111\> (255, ‘’, ‘ssh: connect to host 10.9.80.111 port 22: Connection timed out\r\n’)  
[WARNING]: Unhandled error in Python interpreter discovery for host 10.9.80.111: Failed to connect to the host via ssh: ssh: connect to host 10.9.80.111 port 22:  
Connection timed out

Using module file /usr/lib/python2.7/site-packages/ansible/modules/system/setup.py  
Pipelining is enabled.  
\<10.9.80.111\> ESTABLISH SSH CONNECTION FOR USER: root  
\<10.9.80.111\> SSH: EXEC ssh -o ‘IdentityFile=“/app/axmw/misc\_automation/ssh\_keys/axmw\_id\_rsa”’ -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ‘User=“root”’ -o ConnectTimeout=10 -o StrictHostKeyChecking=no 10.9.80.111 ‘/bin/sh -c ‘"’"’/usr/bin/python && sleep 0’“'”‘’  
fatal: [10.9.80.111]: UNREACHABLE! =\> {  
“changed”: false,  
“msg”: “Data could not be sent to remote host "10.9.80.111". Make sure this host can be reached over ssh: ssh: connect to host 10.9.80.111 port 22: Connection timed out\r\n”,  
“skip\_reason”: “Host 10.9.80.111 is unreachable”,  
“unreachable”: true  
}  
META: ran handlers  
From the output above i want to get the variable that has the below values:

fatal: [10.9.80.111]: UNREACHABLE! =\> {  
“changed”: false,  
“msg”: “Data could not be sent to remote host "10.9.80.111". Make sure this host can be reached over ssh: ssh: connect to host 10.9.80.111 port 22: Connection timed out\r\n”,  
“skip\_reason”: “Host 10.9.80.111 is unreachable”,  
“unreachable”: true

Thus, I wish to capture the unreachable": true status from there.

Can someone please guide ?
