Can the host group that is used on the playbook be recalled as a variable in a template?

Hi,
I’m trying to set a playbook that will get the fqdn or the IPs of the host group where it is being run on (to automatically populate nodes of a cluster on all nodes).

So, can the host group that is define in the site.yml be called from a template as a variable so I can iterate and populate the node list?

For instance {% set comma = joiner(“,”) %}
{% for server in groups[hosts] -%}
{{ comma() }}{{ hostvars[server].ansible_eth0.ipv4.address }}
{%- endfor %}

Where hosts in this scenario is hosts: from the site.yml

Thank you

Hi,

`
{% for hostname in groups[‘<name of group in inventory’] %}

{% endfor %}

`


should work if you know the group in advance.

I think you can get the names of the groups the current target host is part of with the variable “group_names”. So to iterate over all hosts which are in the same groups as the current host this should work:

{% for group in group_names %} {% for host in groups[group] %} ... {% endfor %} {% endfor %}

Filtering duplicates and the current host itself will be more complex but not really a problem :wink:

Kind regards

Karsten