Build /etc/hosts with template

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?