# use current group in part of jinja of a template

**URL:** <https://forum.ansible.com/t/use-current-group-in-part-of-jinja-of-a-template/17835>\
**Category:** Ansible Project\
**Created:** [March 25, 2015, 1:54pm UTC](https://forum.ansible.com/t/use-current-group-in-part-of-jinja-of-a-template/17835 "2015-03-25T13:54:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yariv\_Graf](https://avatars.discourse-cdn.com/v4/letter/y/5e9695/32.png) [@Yariv\_Graf](https://forum.ansible.com/u/Yariv_Graf)\
**Post date:** [March 25, 2015, 1:54pm UTC](https://forum.ansible.com/t/use-current-group-in-part-of-jinja-of-a-template/17835/1 "2015-03-25T13:54:42Z")

</div>

Hi,  
I have this code of template for zookeeper role:

{% for host in groups[‘all’] %}  
server.{{ hostvars[host].zookeeper\_id }}={{ hostvars[host][‘ansible\_hostname’] }}:2888:3888  
{% endfor %}

In this example it applies for all hosts in the inventory file.  
I know you can specify a group like : groups[‘zookeepers’] but I need to apply it on the current group the role runs on.  
Is it possible?

Thanks

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [March 25, 2015, 2:47pm UTC](https://forum.ansible.com/t/use-current-group-in-part-of-jinja-of-a-template/17835/2 "2015-03-25T14:47:22Z")

</div>

There isn’t really a current group that the role is running on.

You might be able to use the “group\_names” variable, however since a host can be in multiple groups, that is a list and not singular. You might however still might be able to make it work.

Your other option is instead to use “play\_hosts” which is an explicit list of hosts targeted by the play. That may suit your needs better than trying to guess the group.

---

<div class="post-metadata">

**Author:** ![Yariv\_Graf](https://avatars.discourse-cdn.com/v4/letter/y/5e9695/32.png) [@Yariv\_Graf](https://forum.ansible.com/u/Yariv_Graf)\
**Post date:** [March 25, 2015, 3:38pm UTC](https://forum.ansible.com/t/use-current-group-in-part-of-jinja-of-a-template/17835/3 "2015-03-25T15:38:44Z")

</div>

Great!  
This is what I needed:

{% for host in play\_hosts %}

Thank you.
