# Lookup host IP address(es) from global\_vars

**URL:** <https://forum.ansible.com/t/lookup-host-ip-address-es-from-global-vars/19152>\
**Category:** Ansible Project\
**Created:** [September 1, 2015, 4:04am UTC](https://forum.ansible.com/t/lookup-host-ip-address-es-from-global-vars/19152 "2015-09-01T04:04:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sergey\_Maslyakov](https://avatars.discourse-cdn.com/v4/letter/s/59ef9b/32.png) [@Sergey\_Maslyakov](https://forum.ansible.com/u/Sergey_Maslyakov)\
**Post date:** [September 1, 2015, 4:04am UTC](https://forum.ansible.com/t/lookup-host-ip-address-es-from-global-vars/19152/1 "2015-09-01T04:04:49Z")

</div>

What is the right way to look up the IPv4 address (or addresses) associated with a host name from global\_vars?

Here is the scenario. I use Postgres with Host-Based Authentication (HBA). In order to configure HBA, I need to put **IP addresses** of the permitted clients into the configuration file, but what I have as the input are **host FQDNs** that I need to resolve somehow. What would be the best way to do it in a Jinja2 template or within group\_vars?

I tried to use Python socket.gethostbyname() but I could not get the syntax right.

Granted, I can fall back to a shell script and templetize it outside of Ansible/Jinja2 but I still hope to find a decent way to do it in Ansible.

Regards,  
/Sergey

---

<div class="post-metadata">

**Author:** ![David\_Karban](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@David\_Karban](https://forum.ansible.com/u/David_Karban)\
**Post date:** [September 1, 2015, 4:18am UTC](https://forum.ansible.com/t/lookup-host-ip-address-es-from-global-vars/19152/2 "2015-09-01T04:18:34Z")

</div>

Hi Sergey,

you can try something like:

{% for host in groups[‘app\_servers’] %}  
ACCEPT $FW int:{{ hostvars[host][‘ansible\_eth1’][‘ipv4’][‘address’] }} tcp 80,443  
{% endfor %}

---

<div class="post-metadata">

**Author:** ![Sergey\_Maslyakov](https://avatars.discourse-cdn.com/v4/letter/s/59ef9b/32.png) [@Sergey\_Maslyakov](https://forum.ansible.com/u/Sergey_Maslyakov)\
**Post date:** [September 1, 2015, 4:36am UTC](https://forum.ansible.com/t/lookup-host-ip-address-es-from-global-vars/19152/3 "2015-09-01T04:36:04Z")

</div>

Hi David,

Thank you very much for your response!

My understanding is that this solution requires that Ansible “visits” the remote host to get the list of interfaces and their settings, including the IPv4 address for each interface. For example, I would not be able to use it to resolve “[www.google.com](http://www.google.com)” or any other arbitrary string, right?

Cheers,  
/Sergey

---

<div class="post-metadata">

**Author:** ![David\_Karban](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@David\_Karban](https://forum.ansible.com/u/David_Karban)\
**Post date:** [September 1, 2015, 5:58am UTC](https://forum.ansible.com/t/lookup-host-ip-address-es-from-global-vars/19152/4 "2015-09-01T05:58:37Z")

</div>

Hi Sergey,

I see, in that case try pipe lookup plugin:  
[http://docs.ansible.com/ansible/playbooks\_lookups.html#more-lookups](http://docs.ansible.com/ansible/playbooks_lookups.html#more-lookups)

And try to resolve IP address from hostname by some cmdline resolver.

---

<div class="post-metadata">

**Author:** ![Duncan\_Hutty](https://avatars.discourse-cdn.com/v4/letter/d/ce73a5/32.png) [@Duncan\_Hutty](https://forum.ansible.com/u/Duncan_Hutty)\
**Post date:** [September 1, 2015, 1:09pm UTC](https://forum.ansible.com/t/lookup-host-ip-address-es-from-global-vars/19152/5 "2015-09-01T13:09:57Z")

</div>

[https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/lookup/dig.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/lookup/dig.py)

\> What is the right way to look up the IPv4 address (or addresses) associated  
\> with a host name from global\_vars?

\<snip\>

---

<div class="post-metadata">

**Author:** ![Sergey\_Maslyakov](https://avatars.discourse-cdn.com/v4/letter/s/59ef9b/32.png) [@Sergey\_Maslyakov](https://forum.ansible.com/u/Sergey_Maslyakov)\
**Post date:** [September 1, 2015, 4:19pm UTC](https://forum.ansible.com/t/lookup-host-ip-address-es-from-global-vars/19152/6 "2015-09-01T16:19:49Z")

</div>

Nice and flexible. Thank you, Duncan!

---

<div class="post-metadata">

**Author:** ![Sergey\_Maslyakov](https://avatars.discourse-cdn.com/v4/letter/s/59ef9b/32.png) [@Sergey\_Maslyakov](https://forum.ansible.com/u/Sergey_Maslyakov)\
**Post date:** [September 1, 2015, 4:21pm UTC](https://forum.ansible.com/t/lookup-host-ip-address-es-from-global-vars/19152/7 "2015-09-01T16:21:58Z")

</div>

Duncan gave a link to a very elaborate lookup plugin based on “dig” below.
