How can a template list every host of an inventory group?
Loop over the magic variable groups: {% for myhost in groups['myhosts'] %}{{ myhost }}{% endfor %} writes one line per host in the group myhosts.
{% for myhost in groups['myhosts'] %}
{{ myhost }}
{% endfor %}
groups is a magic variable: Ansible defines it, not you. It is a dictionary mapping every inventory group name to the list of its hosts' names, including groups['all'] for every host. Because it comes from the inventory, the template stays correct when hosts are added or removed: add a web server to the inventory group, and the next run puts it into every generated file that lists that group.
Typical uses: a load balancer's backend list, a monitoring config listing all nodes, an /etc/hosts file (next card). Combined with hostvars, the loop can also pull each host's facts or variables, not only its name.
Go deeper:
Special variables: groups and hostvars (official docs) โ the magic variables available in templates.