# name to IP aresolution

**URL:** <https://forum.ansible.com/t/name-to-ip-aresolution/17559>\
**Category:** Ansible Project\
**Created:** [February 18, 2015, 11:15pm UTC](https://forum.ansible.com/t/name-to-ip-aresolution/17559 "2015-02-18T23:15:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Charles\_Brown](https://avatars.discourse-cdn.com/v4/letter/c/4bbf92/32.png) [@Charles\_Brown](https://forum.ansible.com/u/Charles_Brown)\
**Post date:** [February 18, 2015, 11:15pm UTC](https://forum.ansible.com/t/name-to-ip-aresolution/17559/1 "2015-02-18T23:15:45Z")

</div>

Hello,

Given a list of hostnames, how would I resolve the names to IP address for use in a Jinja2 template? I don’t see a Jinja filter that performs gethostbyname lookups, and I don’t see a way to do this in Ansible either.

Thanks

---

<div class="post-metadata">

**Author:** ![Giovanni\_Tirloni](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@Giovanni\_Tirloni](https://forum.ansible.com/u/Giovanni_Tirloni)\
**Post date:** [February 19, 2015, 1:12am UTC](https://forum.ansible.com/t/name-to-ip-aresolution/17559/2 "2015-02-19T01:12:27Z")

</div>

I don't think there is a lookup plugin for that, yet.

Not the cleanest way, but you could use register and dig:

&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;some\_ip: "8.8.8.8"  
&nbsp;&nbsp;&nbsp;&nbsp;some\_hostname: "google.com"

&nbsp;&nbsp;tasks:

&nbsp;&nbsp;&nbsp;&nbsp;- name: resolve some ip  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: "dig +short -x {{ some\_ip }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: resolved\_ip

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: resolved\_ip.stdout

&nbsp;&nbsp;&nbsp;&nbsp;- name: resolve some hostname  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: "dig +short {{ some\_hostname }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: resolved\_hostname

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: resolved\_hostname.stdout

Giovanni

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 19, 2015, 1:13am UTC](https://forum.ansible.com/t/name-to-ip-aresolution/17559/3 "2015-02-19T01:13:59Z")

</div>

lookup('pipe','dig ' + hostname + '|grep....')

or host/nslookup/dnsip, which ever system utility you have that does the same

---

<div class="post-metadata">

**Author:** ![Timo](https://avatars.discourse-cdn.com/v4/letter/t/c4cdca/32.png) [@Timo](https://forum.ansible.com/u/Timo)\
**Post date:** [June 15, 2015, 10:17am UTC](https://forum.ansible.com/t/name-to-ip-aresolution/17559/4 "2015-06-15T10:17:47Z")

</div>

Hi,

> lookup(‘pipe’,'dig ’ + hostname + ‘|grep…’)
> 
> or host/nslookup/dnsip, which ever system utility you have that does the same
> 
> –  
> Brian Coca

i am having somewhat the same problem.  
But i dont find any solution to that.

I have to use lookup within a template with jinja2.  
address {{ lookup(‘dig’, ‘({{hosts}}{{domain1}})’) }}

Were hosts and domain1 are lists defined in default/main.yml  
and will be used in a for loop inside the jinja2 template.

{% for hosts in hostlist1 %}  
define host {  
host\_name {{hosts}}  
alias {{hosts}}{{domain1}}  
address {{ lookup(‘dig’, ‘({{hosts}}{{domain1}})’) }}  
}  
{% endfor %}

Problem: lookup (‘dig’, does only accept the real string. Not the variables.  
So i can’t use this in this template.

Any ideas how to overcome this problem?

Many thx in advance!

Timo

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [June 15, 2015, 3:47pm UTC](https://forum.ansible.com/t/name-to-ip-aresolution/17559/5 "2015-06-15T15:47:32Z")

</div>

too many moustaches!

inside {{ }} you don't stack more, variables are directly interpolated already

{{ lookup('dig', hosts + '.'+ domain1) }}

---

<div class="post-metadata">

**Author:** ![Timo](https://avatars.discourse-cdn.com/v4/letter/t/c4cdca/32.png) [@Timo](https://forum.ansible.com/u/Timo)\
**Post date:** [June 16, 2015, 10:35am UTC](https://forum.ansible.com/t/name-to-ip-aresolution/17559/6 "2015-06-16T10:35:25Z")

</div>

Brian,

> too many moustaches!
> 
> inside {{ }} you don’t stack more, variables are directly interpolated already

you’ re the man!  
Thx so much.  
It works now like hell!!!

That would somehow be great as an example in the documentation…
