name to IP aresolution

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

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

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

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

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

too many moustaches!

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

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

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…