1.

How can looping be done over a list of hosts in a group, inside of a template?

Answer»

This can be done by accessing the “$groups” DICTIONARY in the template, like so:

{% for host in groups['db_servers'] %}{{ host }}{% endfor %}

If we need to ACCESS facts also we need to make sure that the facts have been populated. For INSTANCE, a play that talks to db_servers:

- hosts: db_serverstasks:- DEBUG: MSG="Something to debug"

Now, this can be used within a template, like so:

{% for host in groups['db_servers'] %}{{ hostvars[host]['ansible_eth0']['ipv4']['address'] }}{% endfor %}.


Discussion

No Comment Found