# Get other group variables

**URL:** <https://forum.ansible.com/t/get-other-group-variables/20128>\
**Category:** Ansible Project\
**Created:** [January 5, 2016, 2:03pm UTC](https://forum.ansible.com/t/get-other-group-variables/20128 "2016-01-05T14:03:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![geoffrey.montel](https://avatars.discourse-cdn.com/v4/letter/g/b38774/32.png) [@geoffrey.montel](https://forum.ansible.com/u/geoffrey.montel)\
**Post date:** [January 5, 2016, 2:03pm UTC](https://forum.ansible.com/t/get-other-group-variables/20128/1 "2016-01-05T14:03:01Z")

</div>

Hello everyone !

I’m digging into Ansible capabilities a bit further, and I’m looking to implement with a beautiful manner the concept of VIP.  
To do so, I implemented this variable in the group\_vars of my inventory:

group\_vars/firstcluster :

`  
vips:

- name: cluster1\_vip  
ip : 1.2.3.4
- name: cluster1.othervip  
ip : 1.2.3.5  
`

group\_vars/secondcluster :

`  
vips:

- name: cluster2\_vip  
ip : 1.2.4.4
- name: cluster2.othervip  
ip : 1.2.4.5  
`

and in the inventory :

`  
[firstcluster]  
node10  
node11

[secondcluster]  
node20  
node21  
`

My question : if I want to set up a DNS server, that gathers all the VIP and associated names (_without redundancy_ for esthetism) , how do I proceed ? In short : is it possible to get all group variables notwithstanding the hosts underneath ?

like :

`{% for group in <THEMAGICVAR> %} {% for vip in group.vips %} {{ vip.name }} IN A {{ vip.ip }} {% end for %} {% end for %}`

Thank you very much !

– Geoff

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [January 5, 2016, 7:27pm UTC](https://forum.ansible.com/t/get-other-group-variables/20128/2 "2016-01-05T19:27:55Z")

</div>

once the inventory is `compiled` there are not group vars, just host  
vars, but you can get it by using any host in the group (as long as  
you don't override them per host).

{% for group in \<THEMAGICVAR\> %}  
{% for vip in hostvars[groups[group][0]]['vips'] %}  
{{ vip.name }} IN A {{ vip.ip }}  
{% end for %}  
{% end for %}

---

<div class="post-metadata">

**Author:** ![Mike\_Biancaniello](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@Mike\_Biancaniello](https://forum.ansible.com/u/Mike_Biancaniello)\
**Post date:** [January 5, 2016, 8:04pm UTC](https://forum.ansible.com/t/get-other-group-variables/20128/3 "2016-01-05T20:04:33Z")

</div>

You could also create a VIP list in ‘all’ and then reference them in the group\_vars:

group\_vars/all/vips:

`  
all\_vips:

- name: cluster1\_vip  
ip : 1.2.3.4  
otherattr: yes
- name: cluster1\_othervip  
ip : 1.2.3.5  
otherattr: yes
- name: cluster2\_vip  
ip : 1.2.4.4  
otherattr: yes
- name: cluster2\_othervip  
ip : 1.2.4.5  
otherattr: yes  
`

group\_vars/firstcluster:

`  
vips:

- cluster1\_vip
- cluster1\_othervip  
`

group\_vars/secondcluster:

`  
vips:

- cluster2\_vip
- cluster2\_othervip  
`

templates/zone.j2

`  
{% for vip in all\_vips %}  
{{ vip.name }} IN A {{ vip.ip }}  
{% end for %}

`

templates/vips.j2

`  
{% for vip\_name in vips %}  
{% for vip in all\_vips|selectattr(“name”, “equalto”, vip\_name) %}  
virtual server {{ vip.name }} {{ vip.ip }}  
{% endfor %}{# vip #}  
{% endfor %}{# vip\_name #}

`
