# host unreachable

**URL:** <https://forum.ansible.com/t/host-unreachable/28806>\
**Category:** Ansible Developer\
**Created:** [October 17, 2018, 11:21am UTC](https://forum.ansible.com/t/host-unreachable/28806 "2018-10-17T11:21:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![maryam\_da](https://avatars.discourse-cdn.com/v4/letter/m/b9bd4f/32.png) [@maryam\_da](https://forum.ansible.com/u/maryam_da)\
**Post date:** [October 17, 2018, 11:21am UTC](https://forum.ansible.com/t/host-unreachable/28806/1 "2018-10-17T11:21:36Z")

</div>

hi  
i have two hosts and i need to run my playbook only on one of them. i.e host A is always the target, unless it is not reachable. in this case, host B will be the target.  
but the point is that if host A is reachable , playbook should’t work on host B.  
any advice is accepted.  
thanks

---

<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:** [November 13, 2018, 10:36pm UTC](https://forum.ansible.com/t/host-unreachable/28806/2 "2018-11-13T22:36:44Z")

</div>

1 play before your normal play:

hosts: hosta,hostb  
gather\_facts: False  
tasks:  
&nbsp;&nbsp;&nbsp;- ping:  
&nbsp;&nbsp;&nbsp;- group\_by: key=rechable

hosts: reachable  
....

---

<div class="post-metadata">

**Author:** ![dnsbob](https://avatars.discourse-cdn.com/v4/letter/d/ea5d25/32.png) [@dnsbob](https://forum.ansible.com/u/dnsbob)\
**Post date:** [November 14, 2018, 4:19pm UTC](https://forum.ansible.com/t/host-unreachable/28806/3 "2018-11-14T16:19:38Z")

</div>

Thanks for mentioning the ‘ping’ module - that is new to me. (It checks ssh connection and python, not an icmp ping)  
But I don’t see “reachable” as a return value in the docs.

Looks like that will run it for both hosts, which is not what the requestor wanted. More like:

hosts: hosta  
gather\_facts: False  
tasks:

- ping:  
register: pingtest
- hosts: hostb  
when: pingtest.failed

But so far I cannot get it to work.

I am looking to use the same logic to connect to the internal or external IP of an AWS server depending on where I happen to be running Ansible at that moment.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 14, 2018, 5:07pm UTC](https://forum.ansible.com/t/host-unreachable/28806/4 "2018-11-14T17:07:15Z")

</div>

> \> 1 play before your normal play:  
> \>  
> \>  
> \> hosts: hosta,hostb  
> \> gather\_facts: False  
> \> tasks:  
> \> - ping:  
> \> - group\_by: key=rechable  
> \>  
> \> hosts: reachable  
> \> ....  
> \>  
> \> ----------  
> \> Brian Coca  
> \>
> 
> Thanks for mentioning the 'ping' module - that is new to me. (It checks  
> ssh connection and python, not an icmp ping)  
> But I don't see "reachable" as a return value in the docs.

It's not a return value.  
group\_by is a module that will add the host in a group call rechable.

> Looks like that will run it for both hosts, which is not what the requestor  
> wanted. More like:
> 
> hosts: hosta  
> gather\_facts: False  
> tasks:  
> &nbsp;&nbsp;&nbsp;- ping:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: pingtest  
> &nbsp;&nbsp;&nbsp;- hosts: hostb  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: pingtest.failed
> 
> But so far I cannot get it to work.
> 
> I am looking to use the same logic to connect to the internal or external  
> IP of an AWS server depending on where I happen to be running Ansible at  
> that moment.

This will run on the first host in the list(host2) if it reachable if not it will run on the second host(host1)  
Not pretty but should work.

---

<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:** [November 14, 2018, 5:39pm UTC](https://forum.ansible.com/t/host-unreachable/28806/5 "2018-11-14T17:39:28Z")

</div>

there is no need to register as the host failing the ping will be  
removed from the play.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 14, 2018, 6:18pm UTC](https://forum.ansible.com/t/host-unreachable/28806/6 "2018-11-14T18:18:00Z")

</div>

Yes it's necessary, if not the group\_by still runs, just look at this run

PLAY [a2,a1] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [ping] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [a2]: UNREACHABLE! =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;"changed": false,  
&nbsp;&nbsp;&nbsp;&nbsp;"skip\_reason": "Host a2 is unreachable",  
&nbsp;&nbsp;&nbsp;&nbsp;"unreachable": true  
}

MSG:

Authentication or permission failure. In some cases, you may have been able to authenticate and did not have permissions on the target directory. Consider changing the remote tmp path in ansible.cfg to a path rooted in "/tmp". Failed command was: ( umask 77 && mkdir -p "` echo ~/.ansible/tmp/ansible-tmp-1542219116.25-109169561163325 `" && echo ansible-tmp-1542219116.25-109169561163325="` echo ~/.ansible/tmp/ansible-tmp-1542219116.25-109169561163325 `" ), exited with result 1

TASK [group\_by] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [a2] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;"add\_group": "rechable\_hosts",  
&nbsp;&nbsp;&nbsp;&nbsp;"changed": false,  
&nbsp;&nbsp;&nbsp;&nbsp;"parent\_groups": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"all"  
&nbsp;&nbsp;&nbsp;&nbsp;]  
}

---

<div class="post-metadata">

**Author:** ![flowerysong1](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@flowerysong1](https://forum.ansible.com/u/flowerysong1)\
**Post date:** [November 14, 2018, 8:12pm UTC](https://forum.ansible.com/t/host-unreachable/28806/7 "2018-11-14T20:12:09Z")

</div>

You might be interested in a proof of concept inventory plugin I wrote a few months back: [https://github.com/flowerysong/ansible/commit/900b3001](https://github.com/flowerysong/ansible/commit/900b3001)

A better implementation would be to do this at the connection layer so that the checking is done on demand instead of serially for every host in the inventory, but the inventory plugin approach works and isn’t unbearably slow for small inventories.

We don’t actually use this in production; for AWS our team uses Amazon’s DNS resolution, so the hostnames resolve to the internal IPs from within the VPC or the external IPs from outside:

`

ezekielh@ego ~ $ dig +short just-gofannon.ctools-mx.a.mail.umich.edu

[ec2-52-15-122-141.us-east-2.compute.amazonaws.com](http://ec2-52-15-122-141.us-east-2.compute.amazonaws.com).  
10.0.74.217

zeke@ironbull ~ $ dig +short just-gofannon.ctools-mx.a.mail.umich.edu  
[ec2-52-15-122-141.us-east-2.compute.amazonaws.com](http://ec2-52-15-122-141.us-east-2.compute.amazonaws.com).  
52.15.122.141

`

---

<div class="post-metadata">

**Author:** ![dnsbob](https://avatars.discourse-cdn.com/v4/letter/d/ea5d25/32.png) [@dnsbob](https://forum.ansible.com/u/dnsbob)\
**Post date:** [November 15, 2018, 8:53pm UTC](https://forum.ansible.com/t/host-unreachable/28806/8 "2018-11-15T20:53:41Z")

</div>

Thanks, will look into that.
