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

**URL:** <https://forum.ansible.com/t/can-the-host-group-that-is-used-on-the-playbook-be-recalled-as-a-variable-in-a-template/21695>\
**Category:** Ansible Project\
**Created:** [June 8, 2016, 1:28am UTC](https://forum.ansible.com/t/can-the-host-group-that-is-used-on-the-playbook-be-recalled-as-a-variable-in-a-template/21695 "2016-06-08T01:28:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Max\_Leon](https://avatars.discourse-cdn.com/v4/letter/m/7bcc69/32.png) [@Max\_Leon](https://forum.ansible.com/u/Max_Leon)\
**Post date:** [June 8, 2016, 1:28am UTC](https://forum.ansible.com/t/can-the-host-group-that-is-used-on-the-playbook-be-recalled-as-a-variable-in-a-template/21695/1 "2016-06-08T01:28:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Karsten](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@Karsten](https://forum.ansible.com/u/Karsten)\
**Post date:** [June 8, 2016, 3:24pm UTC](https://forum.ansible.com/t/can-the-host-group-that-is-used-on-the-playbook-be-recalled-as-a-variable-in-a-template/21695/2 "2016-06-08T15:24:58Z")

</div>

```auto
Hi,

```

```auto

```

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

`

```auto

```

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 😉

Kind regards

Karsten
