Build /etc/hosts with template

32 views
Skip to first unread message

jepper

unread,
Oct 7, 2015, 5:08:48 AM10/7/15
to Ansible Project
I have a template to build /etc/hosts by taking stuff from the inventory file. It's fragile to be sure, and I'm looking for suggestions. Here what I have so far:

$ cat hosts.j2
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
{% for host in groups['all'] %}
{{ hostvars[host]['ansible_eth0']['ipv4']['address'] }} {{ host }} {{ host }}.{{ clientspecific_dns_domain }}
{% endfor %}

This generates
$ cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
10.124.44.2 box1 box1.domain.com
10.124.44.3 box2 box2.domain.com

The problem with this is if I change the scope of targets, e.g.
ansible-playbook site.yml -l box1

I end with just box1 in the hosts file on box1. Is there a better way?
Reply all
Reply to author
Forward
0 new messages