# How to use group\_vars in ansible templates

**URL:** <https://forum.ansible.com/t/how-to-use-group-vars-in-ansible-templates/32189>\
**Category:** Ansible Project\
**Created:** [February 10, 2020, 11:21am UTC](https://forum.ansible.com/t/how-to-use-group-vars-in-ansible-templates/32189 "2020-02-10T11:21:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bala\_Mutyam](https://avatars.discourse-cdn.com/v4/letter/b/65b543/32.png) [@Bala\_Mutyam](https://forum.ansible.com/u/Bala_Mutyam)\
**Post date:** [February 10, 2020, 11:21am UTC](https://forum.ansible.com/t/how-to-use-group-vars-in-ansible-templates/32189/1 "2020-02-10T11:21:34Z")

</div>

Hi there,

Could someone help me with how to template this config like below please?

cluster\_formation.classic\_config.nodes.1 = rabbit@rabbitmq-1  
cluster\_formation.classic\_config.nodes.2 = rabbit@rabbitmq-2  
cluster\_formation.classic\_config.nodes.3 = rabbit@rabbitmq-3

I would like to change nodes.1,2,3 & rabbit@anisble\_hostname

Thanks

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [February 10, 2020, 1:01pm UTC](https://forum.ansible.com/t/how-to-use-group-vars-in-ansible-templates/32189/2 "2020-02-10T13:01:18Z")

</div>

> Hi there,
> 
> Could someone help me with how to template this config like below please?
> 
> cluster\_formation.classic\_config.nodes.1 = rabbit@rabbitmq-1  
> cluster\_formation.classic\_config.nodes.2 = rabbit@rabbitmq-2  
> cluster\_formation.classic\_config.nodes.3 = rabbit@rabbitmq-3
> 
> I would like to change nodes.1,2,3 & rabbit@anisble\_hostname

So you want to loop over a host group to get this configuration above?

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Mr\_sHamEem\_IT\_Club\_H](https://avatars.discourse-cdn.com/v4/letter/m/eada6e/32.png) [@Mr\_sHamEem\_IT\_Club\_H](https://forum.ansible.com/u/Mr_sHamEem_IT_Club_H)\
**Post date:** [February 10, 2020, 2:04pm UTC](https://forum.ansible.com/t/how-to-use-group-vars-in-ansible-templates/32189/3 "2020-02-10T14:04:29Z")

</div>

Hi there ,

i am getting following issue to connect with my Cisco CISCO2951/K9 (revision 1.1) router any one please help me !

---

<div class="post-metadata">

**Author:** ![Bala\_Mutyam](https://avatars.discourse-cdn.com/v4/letter/b/65b543/32.png) [@Bala\_Mutyam](https://forum.ansible.com/u/Bala_Mutyam)\
**Post date:** [February 12, 2020, 9:49am UTC](https://forum.ansible.com/t/how-to-use-group-vars-in-ansible-templates/32189/4 "2020-02-12T09:49:11Z")

</div>

Yes. I managed get the config as below.

{% for host in groups[‘rabbitmq’] %}  
cluster\_formation.classic\_config.nodes.{{ loop.index }} = rabbit@{{ hostvars[host][‘ansible\_hostname’] }}  
{% endfor %}

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [February 12, 2020, 12:54pm UTC](https://forum.ansible.com/t/how-to-use-group-vars-in-ansible-templates/32189/5 "2020-02-12T12:54:23Z")

</div>

You forgot to post your playbook
