# merging specific group\_vars without changing global behavior

**URL:** <https://forum.ansible.com/t/merging-specific-group-vars-without-changing-global-behavior/29567>\
**Category:** Ansible Project\
**Created:** [January 30, 2019, 1:40am UTC](https://forum.ansible.com/t/merging-specific-group-vars-without-changing-global-behavior/29567 "2019-01-30T01:40:44Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adam\_E](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@Adam\_E](https://forum.ansible.com/u/Adam_E)\
**Post date:** [January 30, 2019, 1:40am UTC](https://forum.ansible.com/t/merging-specific-group-vars-without-changing-global-behavior/29567/1 "2019-01-30T01:40:44Z")

</div>

In my environment i have a group\_vars structure something like the following:  
`  
[global:children]  
datacenter1

[datacenter1:children]  
network\_zone1  
network\_zone2

[network\_zone1]  
server1

[network\_zone2]  
server2  
`

so using a simple case where I want to add entries to /etc/hosts (although there are other use cases, this just seemed the simplest to demonstrate)  
I want to be able to set group\_vars within each group that get merged/appended together for processing by my role. However  
since the default ansible behavior is not to merge and I don’t really want to change the default.  
therefore I was thinking about doing something like this:

`  
[global:vars]  
linux\_hosts\_entries\_global:

- { ip: 123.124.125.125, names: [backup\_server]}

[datacenter1:vars]  
linux\_hosts\_entries\_dc1:

- { ip: 123.124.125.126, names: [dc1\_server]}

[network\_zone1:vars]  
linux\_hosts\_entries\_z1:

- { ip: 123.124.125.125, names: [z1\_server]}  
`

Then I could do something like below to loop through the variables to get a merged list.  
`

- name: “debug2”  
debug:  
msg: "value is: {{ lookup(‘vars’, item) }} "  
loop: “{{ hostvars[inventory\_hostname] | select(‘match’, ‘^linux\_hosts\_entries’) |list }}”  
`

or in the case of a template, you could do something like this:  
`{% set linux_hosts_merged_entries = hostvars[inventory_hostname] | select('match', '^linux_hosts_entries') |list %} {% for groupvar_name in linux_hosts_merged_entries %} {% set host_entries = lookup('vars', groupvar_name) %} {% for host_entry in host_entries %} {{ host_entry.ip }} {{ host_entry.names | join(' ') }} {% endfor %} {% endfor %}`

I was wondering how others are handling similar use cases? is there a simpler solution?  
This strategy seems to work quite well.

I am aware of combine/union filters, however these require that you know the names of the variables ahead of time. I like that in this model I can add additional variables later without affecting the existing setup.

I also created a PR to simplify further by [adding regex support](https://github.com/ansible/ansible/pull/51466) to the lookup vars plugin.
