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
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?