Lookup host IP address(es) from global_vars

748 views
Skip to first unread message

Sergey Maslyakov

unread,
Sep 1, 2015, 12:04:50 AM9/1/15
to Ansible Project
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

David Karban

unread,
Sep 1, 2015, 12:18:55 AM9/1/15
to ansible...@googlegroups.com
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 %}

Key think is hostvars variable.

David Karban
Linux server specialist/Specialista na správu linuxových serverů
www.karban.eu

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/5bd52a9d-e30a-46cc-808a-7f1237c1294b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sergey Maslyakov

unread,
Sep 1, 2015, 12:36:05 AM9/1/15
to Ansible Project
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" or any other arbitrary string, right?


Cheers,
/Sergey

David Karban

unread,
Sep 1, 2015, 1:59:00 AM9/1/15
to ansible...@googlegroups.com
Hi Sergey, 

I see, in that case try pipe lookup plugin:

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


David Karban
Linux server specialist/Specialista na správu linuxových serverů
www.karban.eu

Duncan Hutty

unread,
Sep 1, 2015, 9:10:56 AM9/1/15
to ansible...@googlegroups.com
https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/lookup/dig.py

On 9/1/15 0:04, Sergey Maslyakov wrote:
> What is the right way to look up the IPv4 address (or addresses)
associated
> with a host name from global_vars?

<snip>
--
Duncan Hutty
http://www.allgoodbits.org

Sergey Maslyakov

unread,
Sep 1, 2015, 12:19:50 PM9/1/15
to Ansible Project, dhu...@allgoodbits.org
Nice and flexible. Thank you, Duncan!

Sergey Maslyakov

unread,
Sep 1, 2015, 12:21:59 PM9/1/15
to Ansible Project
Duncan gave a link to a very elaborate lookup plugin based on "dig" below.
Reply all
Reply to author
Forward
0 new messages