# Best practice for referring to other host IPs

**URL:** <https://forum.ansible.com/t/best-practice-for-referring-to-other-host-ips/15399>\
**Category:** Ansible Project\
**Created:** [June 4, 2014, 1:40am UTC](https://forum.ansible.com/t/best-practice-for-referring-to-other-host-ips/15399 "2014-06-04T01:40:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Mahemoff](https://avatars.discourse-cdn.com/v4/letter/m/5f9b8f/32.png) [@Michael\_Mahemoff](https://forum.ansible.com/u/Michael_Mahemoff)\
**Post date:** [June 4, 2014, 1:40am UTC](https://forum.ansible.com/t/best-practice-for-referring-to-other-host-ips/15399/1 "2014-06-04T01:40:50Z")

</div>

I know this has come up before, but I’ve not come across a real answer for it.

It’s common for a given host to depend on other hosts. App servers need to know the IP of databases they consume, databases need to know IPs of app servers that will be accessing them etc etc. In Ansible, the IP of other hosts is not automatically exposed. One practice I’ve seen is to run a dummy play just to force the other host’s facts to be discovered, but this gets tedious, clutters up scripts, and slows down their execution. I’m sure there must be a better way, but I don’t know what.

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [June 4, 2014, 2:15pm UTC](https://forum.ansible.com/t/best-practice-for-referring-to-other-host-ips/15399/2 "2014-06-04T14:15:58Z")

</div>

It’s really not bad, the “dummy” play just looks like this:

- hosts: all

And that’s it, followed by other plays. You can limit it more of course, if you don’t need facts about every system in your inventory, but the setup step needs to be run on systems to get their facts. However it only needs to be run once, and after that all other plays can specify “gather\_facts: no” to skip it.

---

<div class="post-metadata">

**Author:** ![Michael\_Peters](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@Michael\_Peters](https://forum.ansible.com/u/Michael_Peters)\
**Post date:** [June 4, 2014, 3:24pm UTC](https://forum.ansible.com/t/best-practice-for-referring-to-other-host-ips/15399/3 "2014-06-04T15:24:07Z")

</div>

But it gets more unwieldy if you're using roles. If my app role needs  
to have the IPs for the databases I just have to hope that the  
playbook that ends up running the tasks in my play have gathered all  
the facts from those other systems. It gets even more  
action-at-a-distance when your role is a dependency of another role.  
My role can't force a fact gathering expedition to the other hosts.

Now you could say that if my role relies on this information I could  
check if there are hosts in the group it cares about and then do a  
fail if there aren't any, but lack of information doesn't necessarily  
mean something is wrong. For instance, I have dev setups using the  
same ansible stuff that I use in staging and production. Dev setups  
don't need read-only database slaves running on their laptop. But  
staging and production do. If gathering facts from all hosts shows  
there aren't any db-slave servers, no biggie we just send the reads to  
the master. But it's a problem in production if something changes in  
some other role that depends on the app server role but doesn't happen  
to gather facts from all the servers. Now production is sending all  
reads to the master.

This is a very specific case that could probably be worked around in a  
very verbose way, but it would all be simpler if my role could gather  
facts as a task. Or maybe have some dependency on facts in the  
meta/main.yml or something to make it explicit that I want me some  
facts 🙂

---

<div class="post-metadata">

**Author:** ![Michael\_Mahemoff](https://avatars.discourse-cdn.com/v4/letter/m/5f9b8f/32.png) [@Michael\_Mahemoff](https://forum.ansible.com/u/Michael_Mahemoff)\
**Post date:** [June 4, 2014, 3:50pm UTC](https://forum.ansible.com/t/best-practice-for-referring-to-other-host-ips/15399/4 "2014-06-04T15:50:03Z")

</div>

True, the dummy boilerplate is small, but it has to precede every playbook. (I also thought it had to include at least one task to be in effect.)

The main issue is performance. I’m not dynamically provisioning, so these things never change. I thought maybe there’s some trick people are using to avoid the lookup, e.g. automatically using DNS/hosts file or auto-generating config files.

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [June 5, 2014, 3:14pm UTC](https://forum.ansible.com/t/best-practice-for-referring-to-other-host-ips/15399/5 "2014-06-05T15:14:42Z")

</div>

Is there an option to use the FQDNs in the inventory itself then?  
If they resolve then you don't need to facts gather, right?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [June 6, 2014, 12:37pm UTC](https://forum.ansible.com/t/best-practice-for-referring-to-other-host-ips/15399/6 "2014-06-06T12:37:26Z")

</div>

We’ve discussed fact caching options in the past.

There may be some in the future.

Until now, talk to the hosts first.
