title: How to detect UNREACHABLE HOSTS in Ansible
body: <p>Below is my playbook</p>
  <pre><code>- name: Play 1.5 - Check each target
    hosts: all_hosts
    ignore_unreachable: yes
    ignore_errors: yes
    gather_facts: true
  
    tasks:
  
     - raw: &quot;echo {{ inventory_hostname }} is UNREACHABLE&quot;
       delegate_to: localhost
       when: &lt;Need help with the when condition here&gt;
  </code></pre>
  <p>I need help with the <code>when</code> condition in the above playbook.</p>
  <p>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 <code>inventory_host connection status</code></p>
  <p>Please see the output below:</p>
  <pre><code>TASK [Gathering Facts] *************************************************************************************************************************************************
  task path: /app/Ansible/playbook/check/check.yml:55
  &lt;10.9.80.111&gt; Attempting python interpreter discovery
  &lt;10.9.80.111&gt; ESTABLISH SSH CONNECTION FOR USER: root
  &lt;10.9.80.111&gt; SSH: EXEC ssh -o 'IdentityFile=&quot;/app/automation/ssh_keys/id_rsa&quot;' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User=&quot;root&quot;' -o ConnectTimeout=10 -o StrictHostKeyChecking=no 10.9.80.111 '/bin/sh -c '&quot;'&quot;'echo PLATFORM; uname; echo FOUND; command -v '&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'/usr/bin/python'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'; command -v '&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'python3.7'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'; command -v '&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'python3.6'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'; command -v '&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'python3.5'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'; command -v '&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'python2.7'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'; command -v '&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'python2.6'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'; command -v '&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'/usr/libexec/platform-python'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'; command -v '&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'/usr/bin/python3'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'; command -v '&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'python'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'&quot;'; echo ENDFOUND &amp;&amp; sleep 0'&quot;'&quot;''
  &lt;10.9.80.111&gt; (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.
  &lt;10.9.80.111&gt; ESTABLISH SSH CONNECTION FOR USER: root
  &lt;10.9.80.111&gt; SSH: EXEC ssh -o 'IdentityFile=&quot;/app/axmw/misc_automation/ssh_keys/axmw_id_rsa&quot;' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User=&quot;root&quot;' -o ConnectTimeout=10 -o StrictHostKeyChecking=no 10.9.80.111 '/bin/sh -c '&quot;'&quot;'/usr/bin/python &amp;&amp; sleep 0'&quot;'&quot;''
  fatal: [10.9.80.111]: UNREACHABLE! =&gt; {
      &quot;changed&quot;: false,
      &quot;msg&quot;: &quot;Data could not be sent to remote host \&quot;10.9.80.111\&quot;. Make sure this host can be reached over ssh: ssh: connect to host 10.9.80.111 port 22: Connection timed out\r\n&quot;,
      &quot;skip_reason&quot;: &quot;Host 10.9.80.111 is unreachable&quot;,
      &quot;unreachable&quot;: true
  }
  META: ran handlers 
  </code></pre>
  <p>From the output above i want to get the variable that has the below values:</p>
  <pre><code>fatal: [10.9.80.111]: UNREACHABLE! =&gt; {
      &quot;changed&quot;: false,
      &quot;msg&quot;: &quot;Data could not be sent to remote host \&quot;10.9.80.111\&quot;. Make sure this host can be reached over ssh: ssh: connect to host 10.9.80.111 port 22: Connection timed out\r\n&quot;,
      &quot;skip_reason&quot;: &quot;Host 10.9.80.111 is unreachable&quot;,
      &quot;unreachable&quot;: true
  </code></pre>
  <p>Thus, I wish to capture the <code>unreachable&quot;: true</code> status from there.</p>
  <p>Can someone please guide ?</p>