# Refer other host to get facts when running in a host

**URL:** <https://forum.ansible.com/t/refer-other-host-to-get-facts-when-running-in-a-host/17442>\
**Category:** Ansible Project\
**Created:** [February 4, 2015, 10:36am UTC](https://forum.ansible.com/t/refer-other-host-to-get-facts-when-running-in-a-host/17442 "2015-02-04T10:36:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Le\_Van](https://avatars.discourse-cdn.com/v4/letter/l/53a042/32.png) [@Le\_Van](https://forum.ansible.com/u/Le_Van)\
**Post date:** [February 4, 2015, 10:36am UTC](https://forum.ansible.com/t/refer-other-host-to-get-facts-when-running-in-a-host/17442/1 "2015-02-04T10:36:38Z")

</div>

Hi,

I’m using Ansible to provision an application deployment.

here is my structure

hosts  
[dbservers]  
db.local

[memcached]  
mc.local

[appservers]  
app1.local  
#app2.local

I’m using Tomcat and have a context like this in appservers ( app1.local)  
roles/tomcat/templates/context.j2

\<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"

\<Resource name=“jdbc/postgres/configuration”

url=“jdbc:postgresql:// **db.local** :5432/configuration”  
/\>

My questions are:

- How can I resolve mc.local ( db.local) to IP?  
Doing this, I don’t have to set hosts in appservers like  
/etc/hosts  
192.168… mc.local  
…

- In case of using dymamic inventory ( Ec2), how can I refer a db ( from db tags), resolve its ip and resolve the template.

- Or is there another way to accomplish this?

I guess this model is trivial and should have a normal way out there ( but I’m still a Ansible newbie to get it)

Thanks,  
Pilgrim

---

<div class="post-metadata">

**Author:** ![Le\_Van1](https://avatars.discourse-cdn.com/v4/letter/l/e36b37/32.png) [@Le\_Van1](https://forum.ansible.com/u/Le_Van1)\
**Post date:** [February 5, 2015, 3:12am UTC](https://forum.ansible.com/t/refer-other-host-to-get-facts-when-running-in-a-host/17442/2 "2015-02-05T03:12:55Z")

</div>

this seems a basic question and have a simple answer.

{% for host in groups[‘dbservers’] %}  
{{ hostvars[host][‘ansible\_eth1’][‘ipv4’][‘address’] }}  
{% endfor %}

the interesting point is I normally run a specfic roles/tasks by tagging  
like this

ansible-playbook site.yml --limit=appservers --tags=sb\_tomcat

note: since we limit appservers only then Ansible have no idea to get facts from other servers ( dbserver, memcache) in this case.

I have to do cheat like this

ansible-playbook site.yml --limit=dbservers, appservers --tags=sb\_tomcat ( add dbservers to gather facts)  
but it’s not enough, I have to add a fake task like this for dbservers with tag sb\_tomcat

- name: Dont fool me  
command: echo “can you hear me”  
tags:
- sb\_tomcat

Doing this to tell Ansible to gather dbserver facts and

{% for host in groups[‘dbservers’] %}  
{{ hostvars[host][‘ansible\_eth1’][‘ipv4’][‘address’] }}  
{% endfor %}

works.

Questions:

- In real case, we really need a specfic tag. Do we have another way to gather fact explitcitly without modifying --limit or add fake tasks?

Regards,  
Pilgrim
