name to IP aresolution

2,687 views
Skip to first unread message

Charles Brown

unread,
Feb 18, 2015, 6:15:45 PM2/18/15
to ansible...@googlegroups.com
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 

Giovanni Tirloni

unread,
Feb 18, 2015, 8:12:33 PM2/18/15
to ansible...@googlegroups.com
On Wed, 18 Feb 2015 15:15 -0800, Charles Brown <charl...@gmail.com>
wrote:
I don't think there is a lookup plugin for that, yet.

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

vars:
some_ip: "8.8.8.8"
some_hostname: "google.com"

tasks:

- name: resolve some ip
command: "dig +short -x {{ some_ip }}"
register: resolved_ip

- debug:
var: resolved_ip.stdout

- name: resolve some hostname
command: "dig +short {{ some_hostname }}"
register: resolved_hostname

- debug:
var: resolved_hostname.stdout


Giovanni

Brian Coca

unread,
Feb 18, 2015, 8:14:03 PM2/18/15
to ansible...@googlegroups.com
lookup('pipe','dig ' + hostname + '|grep....')

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



--
Brian Coca

Timo

unread,
Jun 15, 2015, 8:38:49 AM6/15/15
to ansible...@googlegroups.com
Hi,

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

 

Brian Coca

unread,
Jun 15, 2015, 11:47:37 AM6/15/15
to ansible...@googlegroups.com
too many moustaches!

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

{{ lookup('dig', hosts + '.'+ domain1) }}
> --
> 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/2bf3fa47-f894-46f1-90c9-55ce130ac28f%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
Brian Coca

Timo

unread,
Jun 16, 2015, 6:35:25 AM6/16/15
to ansible...@googlegroups.com
Brian,

On Monday, June 15, 2015 at 5:47:37 PM UTC+2, Brian Coca wrote:
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....
 
 
--
Brian Coca

 
Reply all
Reply to author
Forward
0 new messages