Thanks Dayton for you final solution.
If I use it, I notice that the privat IP wasn't set correctly; like this:
192.168.1.1 
1.2.3.4 hostname hostname.domain.com

So I use this solution
{% for interface in ansible_interfaces if interface != 'lo' %}
{{ hostvars[inventory_hostname]['ansible_' + interface]['ipv4']['address'] 
}} {{ inventory_hostname_short }} {{ inventory_hostname }}
{% endfor %}

and now it's ok for me
192.168.1.1 hostname hostname.domain.com
1.2.3.4 hostname hostname.domain.com

thanks

Il giorno lunedì 1 febbraio 2016 15:47:58 UTC+1, Dayton Jones ha scritto:
>
> Final solution:
>
> {% for interface in ansible_interfaces if interface != 'lo' %}{{ 
> hostvars[inventory_hostname]['ansible_' + interface]['ipv4']['address'] }} 
> {{ lookup('dig', hostvars[inventory_hostname]['ansible_' + 
> interface]['ipv4']['address'] + '/PTR').rstrip('.') }} {{ lookup('dig', 
> hostvars[inventory_hostname]['ansible_' + interface]['ipv4']['address'] + 
> '/PTR').split('.')[0] }}
> {% endfor %} 
>
>
> results in :
> 1.2.3.4 hostname.mydomain.com hostname
> 2.3.4.5 hostname-alt.otherdomain.com hostname-alt
>
>
> This of course assumes that DNS has been properly set beforehand...
>
> On Friday, January 29, 2016 at 11:17:47 AM UTC-6, Dayton Jones wrote:
>>
>> I'm trying to dynamically set /etc/hosts based on the ipv4 address 
>> assigned to all the interfaces on a host... my template reads as such
>>
>>
>> 127.0.0.1       localhost.localdomain localhost
>>
>> {% for item in ansible_interfaces if item != 'lo' %}
>>
>> {{ 'ansible_' + item + '.ipv4.address' }} {{ lookup('dig', 'ansible_' + 
>> item + '.ipv4.address' + '/PTR') }}
>>
>> {% endfor %}
>>
>>
>> which results in :
>>
>> 127.0.0.1 localhost.localdomain localhost
>>
>> ansible_eth1.ipv4.address NXDOMAIN 
>>
>> ansible_eth0.ipv4.address NXDOMAIN 
>>
>>
>>
>> I've tried several iterations, but I'm struggling to find the proper 
>> syntax to get the file to write as expected... is this possible?  Any 
>> suggestions greatly appreciated.
>>
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/4c700efa-d0d4-445e-a853-195bfae7b3d3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to