# How to get a list of unreachable hosts

**URL:** <https://forum.ansible.com/t/how-to-get-a-list-of-unreachable-hosts/35438>\
**Category:** Ansible Project\
**Created:** [June 29, 2021, 7:42pm UTC](https://forum.ansible.com/t/how-to-get-a-list-of-unreachable-hosts/35438 "2021-06-29T19:42:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![omoeko](https://avatars.discourse-cdn.com/v4/letter/o/e47774/32.png) [@omoeko](https://forum.ansible.com/u/omoeko)\
**Post date:** [June 29, 2021, 7:42pm UTC](https://forum.ansible.com/t/how-to-get-a-list-of-unreachable-hosts/35438/1 "2021-06-29T19:42:21Z")

</div>

Hi All.

I have the ansible role coded below.

---

<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:** [June 30, 2021, 5:03am UTC](https://forum.ansible.com/t/how-to-get-a-list-of-unreachable-hosts/35438/2 "2021-06-30T05:03:40Z")

</div>

> Lastly, to add the gather\_facts: no. where do i add this in the ansible  
> role ?

See "Playbook Keywords". The directive gather\_facts is available in a  
play only.  
[https://docs.ansible.com/ansible/latest/reference\_appendices/playbooks\_keywords.html](https://docs.ansible.com/ansible/latest/reference_appendices/playbooks_keywords.html)

> ... is there a variable which contains all the unreachable hosts. Is it possible to have this in a  
> comma delimmeted list and stored in a variable ?

The following solution won't work if you set "gather\_facts: no".

Yes, there are "Special Variables"  
[https://docs.ansible.com/ansible/latest/reference\_appendices/special\_variables.html](https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html)

Quoting:

\* ansible\_play\_hosts\_all: List of all the hosts that were targeted by  
&nbsp;&nbsp;the play.

\* ansible\_play\_hosts: List of hosts in the current play run, not  
&nbsp;&nbsp;limited by the serial. Failed/Unreachable hosts are excluded from  
&nbsp;&nbsp;this list.

For example

- hosts: alpha,beta,charlie  
&nbsp;&nbsp;gather\_facts: true  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- block:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: ansible\_play\_hosts\_all  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: ansible\_play\_hosts  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;down: "{{  
&nbsp;&nbsp;ansible\_play\_hosts\_all|difference(ansible\_play\_hosts) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: down  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run\_once: true

gives when the host alpha is down

&nbsp;&nbsp;ansible\_play\_hosts\_all:  
&nbsp;&nbsp;- alpha  
&nbsp;&nbsp;- beta  
&nbsp;&nbsp;- charlie

&nbsp;&nbsp;ansible\_play\_hosts:  
&nbsp;&nbsp;- beta  
&nbsp;&nbsp;- charlie

&nbsp;&nbsp;down:  
&nbsp;&nbsp;- alpha

---

<div class="post-metadata">

**Author:** ![omoeko](https://avatars.discourse-cdn.com/v4/letter/o/e47774/32.png) [@omoeko](https://forum.ansible.com/u/omoeko)\
**Post date:** [June 30, 2021, 6:30pm UTC](https://forum.ansible.com/t/how-to-get-a-list-of-unreachable-hosts/35438/3 "2021-06-30T18:30:39Z")

</div>

Thank you so much for this.

I have now been able to use thesame login in my ansible role, and it appears to work.

- name Unreachable servers  
set\_fact:  
down: “{{ ansible\_play\_hosts\_all | difference(ansible\_play\_hosts)}}”

- name: Set Execution File and parameters  
set\_fact:  
scriptfile: “{{ansible\_user\_dir}}\scripts\host\_check.ps1”  
params: “-servername ‘{{the\_host\_name}}’ -response var\_ping.failed -unreachable\_hosts {{ down }}”

- name: Execute script  
win\_command: powershell.exe “{{scriptfile}}” “{{params}}”  
when: inventory\_hostname == {{ db\_server\_host }}

I do however have some questions. My playbook runs against hosts defined in my inventory, in this case what I want to achieve is a situation where the unreachable hosts is passed onto a powershell script right at the end and at once. So for example, 100 hosts, 10 were unreachable. The playbook should gather the 10 unreachable hosts, and pass the list of hosts in an array format to a powershell script or as a json data type.

All I want to do is be able to process the list of unreachable servers from my powershell script.

At the moment, I get

[“server1”, “server2”]

MY script will work with a format like this “server1”,“server2”

Lastly.The process of logging the unreachable servers at the end, only needs to happen once to a specific server which does the logging to a database. I created a task to do this as seen above.

db\_server\_host is being passed as a extra variables from ansible tower. The reason i added the when is that I only want it to run on the DB server and not on every host. I get the error The conditional check inventory\_hostname == {{ db\_server\_host }} failed. The error was error while evaluating conditional inventory\_hostname == {{ db\_server\_host }} ‘mydatabaseServerName’ is undefined

---

<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:** [June 30, 2021, 7:09pm UTC](https://forum.ansible.com/t/how-to-get-a-list-of-unreachable-hosts/35438/4 "2021-06-30T19:09:38Z")

</div>

Try

&nbsp;&nbsp;&nbsp;&nbsp;- name: Execute script  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;win\_command: "powershell.exe {{ scriptfile }} {{ params }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: db\_server\_host  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run\_once: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;params: "{{ down|join(',') }}"

1) Use the filter \*join\* to create the variable \*params\* given the  
&nbsp;&nbsp;&nbsp;list "down=[server1, server2]"

2) Use \*delegate\_to\* and \*run\_once\* instead of the condition

3) The braces {{ }} in the condition is the reason of the syntax  
&nbsp;&nbsp;&nbsp;error.

---

<div class="post-metadata">

**Author:** ![omoeko](https://avatars.discourse-cdn.com/v4/letter/o/e47774/32.png) [@omoeko](https://forum.ansible.com/u/omoeko)\
**Post date:** [June 30, 2021, 10:40pm UTC](https://forum.ansible.com/t/how-to-get-a-list-of-unreachable-hosts/35438/5 "2021-06-30T22:40:43Z")

</div>

Thanks so much for takig the time to explain, this has helped me a lot.

Implemented the changes, however its complaining about the vars definition below.

vars:  
params: “{{ down|join(‘,’) }}”

I kept it in its own block, rather than placing it under the win\_command block.

Is this the right approach or should it go under the vars\>\>main.yaml file ?

---

<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 1, 2021, 12:34am UTC](https://forum.ansible.com/t/how-to-get-a-list-of-unreachable-hosts/35438/6 "2021-07-01T00:34:00Z")

</div>

The only difference is the scope of the variable. It's visible to the  
task only if declared in the \*vars\* of the task. It should work also  
if declared in the \*vars\* of a playbook. Post the "Minimal working  
example".  
[https://en.wikipedia.org/wiki/Minimal\_working\_example](https://en.wikipedia.org/wiki/Minimal_working_example)

---

<div class="post-metadata">

**Author:** ![omoeko](https://avatars.discourse-cdn.com/v4/letter/o/e47774/32.png) [@omoeko](https://forum.ansible.com/u/omoeko)\
**Post date:** [July 9, 2021, 2:36pm UTC](https://forum.ansible.com/t/how-to-get-a-list-of-unreachable-hosts/35438/7 "2021-07-09T14:36:26Z")

</div>

thanks so much. I got it working.
