# Merging/extending List of Dicts?

**URL:** <https://forum.ansible.com/t/merging-extending-list-of-dicts/17872>\
**Category:** Ansible Project\
**Created:** [March 30, 2015, 12:58pm UTC](https://forum.ansible.com/t/merging-extending-list-of-dicts/17872 "2015-03-30T12:58:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nico\_K](https://avatars.discourse-cdn.com/v4/letter/n/46a35a/32.png) [@Nico\_K](https://forum.ansible.com/u/Nico_K)\
**Post date:** [March 30, 2015, 12:58pm UTC](https://forum.ansible.com/t/merging-extending-list-of-dicts/17872/1 "2015-03-30T12:58:07Z")

</div>

Hi guys,

I’m trying to configure an EC2 Security Group using Ansible, since all of my security groups contain a set of ‘base rules’, I’m trying to split that portion out to avoid duplication.  
To do so I have:

base\_rules: [  
{ proto: …  
, from\_port: …  
…  
},  
{ proto: …  
…  
]

Which works fine on it’s own, I have however been unable to add entries to it, or to pass in multiple lists of dicts to ec2\_groups.  
What I’ve tried is:

‘{{ base\_rules }} + {{ group\_rules }}’ # whereby group\_rules is defined similar to base\_rules, this does run but complains.  
'{{ base\_rules }} + [] # same result.

Anyone has a suggestion on how to deal with this?

Thanks,  
Nico.

---

<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:** [March 30, 2015, 1:12pm UTC](https://forum.ansible.com/t/merging-extending-list-of-dicts/17872/2 "2015-03-30T13:12:27Z")

</div>

try {{ base\_rule|union(group\_rules) }}

---

<div class="post-metadata">

**Author:** ![Nico\_K](https://avatars.discourse-cdn.com/v4/letter/n/46a35a/32.png) [@Nico\_K](https://forum.ansible.com/u/Nico_K)\
**Post date:** [March 31, 2015, 8:16pm UTC](https://forum.ansible.com/t/merging-extending-list-of-dicts/17872/3 "2015-03-31T20:16:58Z")

</div>

Hey Brian,

Thanks for that, works like a charm.

I did notice there are a bunch of quoting issues going on, for instance:

x:

- y: b  
z: c
- y: “{{ d }}”  
z: “{{ e }}”

Works fine, where-as defining the structure as per my initial post the variable evaluation fails ( end up with {# x.item #} or group\_id’s that do not evaluate.  
Now I will say, the variables are defined in group\_vars over a couple of different groups and are then passed in through a ‘with\_items’ iterator in the actual role.

Anyway, it’s not a real problem since defining it as above works as expected.

Thanks again for your suggestion!  
Nico

---

<div class="post-metadata">

**Author:** ![Johnathan\_Komara](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@Johnathan\_Komara](https://forum.ansible.com/u/Johnathan_Komara)\
**Post date:** [April 14, 2015, 4:23pm UTC](https://forum.ansible.com/t/merging-extending-list-of-dicts/17872/4 "2015-04-14T16:23:01Z")

</div>

What if you go a few levels deeper? Say I have different rules for “all”, “location”, “groupA”, and “groupB”. I will always have base rules for “all”. I may or may not have base rules for “location”, “groupA”, or “groupB”.

Right now I am handling it like this. I am using Ansible 1.8.2 with hash merging turned on. I have a hash in each group\_var that needs it.

`  
group\_vars/all:

base\_rule:  
all:  
rule:

- rule1

group\_vars/location:

base\_rule:  
location:  
rule:

- rule2

group\_vars/groupB:

base\_rule:  
groupB:  
rule:

- rule3  
`

Then when I want to call them in a task I would do so like this:

`

- name: echo base rules  
shell: /usr/bin/echo item.1  
with\_subelements:
- base\_rule
- rule  
`

In a jinja template I would use:

`{% for key, value in base_rule.items() %} {% for key, a in value.iteritems() %} {% for rule in a %} {{ rule }} {% endfor %} {% endfor %} {% endfor %}`

I was wondering if there were a better way do accomplish this. If I were to use union I would have to list out ever possible instance of the hash that I am trying to merge right? Something like:

`{{ base_rule|union(location_rules)|union(groupA_rules)|union(groupB_rules) }}`

Or is there a way to accomplish this without listing each hash that needs to be joined?

Thanks,

John
