# Cumulative group variables?

**URL:** <https://forum.ansible.com/t/cumulative-group-variables/22871>\
**Category:** Ansible Project\
**Created:** [October 24, 2016, 6:05pm UTC](https://forum.ansible.com/t/cumulative-group-variables/22871 "2016-10-24T18:05:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cody\_John](https://avatars.discourse-cdn.com/v4/letter/c/8e7dd6/32.png) [@Cody\_John](https://forum.ansible.com/u/Cody_John)\
**Post date:** [October 24, 2016, 6:05pm UTC](https://forum.ansible.com/t/cumulative-group-variables/22871/1 "2016-10-24T18:05:38Z")

</div>

I’m working on modeling our datacenter top-of-rack switches in Ansible, and have run into a potential problem with my approach. The basic idea is to use a hierarchal group structure that starts with general purpose (tor), and then get more specific for each deployment based on application that rack is supporting:

`  
[tors]

[app1switches]  
app1switch1  
app1switch2

[app2switches]  
app2switch1  
app2switch2

[tors:children]  
app1switches  
app2switches

`

I want to abstract all my VLAN configuration into group-defined variables. In this example, let’s say that there is at least one VLAN that is standard across all TORs, and then each application has their own unique set of VLANs that also need to be applied. My idea was to create group variable files such as:

`

---

<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:** [October 25, 2016, 1:05am UTC](https://forum.ansible.com/t/cumulative-group-variables/22871/2 "2016-10-25T01:05:35Z")

</div>

So, the problem is that you can’t combine lists, but you can merge dicts (it’s a config setting, default=false).

This will require either a double for loop or, (my preference, especially if you are going to use this type of construct a lot) a filter to combine them.

`

# file: /group\_vars/tors

---

<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:** [October 25, 2016, 1:26am UTC](https://forum.ansible.com/t/cumulative-group-variables/22871/3 "2016-10-25T01:26:34Z")

</div>

example filter: [https://github.com/chepazzo/ansible-misc/blob/master/filter\_plugins/listoflists.py#L35](https://github.com/chepazzo/ansible-misc/blob/master/filter_plugins/listoflists.py#L35)

Now, your template looks like this:

`{% for vlan in vlans|collapse_dict %} set vlan {{ vlan.name }} vlan-id {{ vlan.id }} {% endfor %}`
