# Combine dictionaries based on a dictionary key value.

**URL:** <https://forum.ansible.com/t/combine-dictionaries-based-on-a-dictionary-key-value/33280>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [June 30, 2020, 7:17am UTC](https://forum.ansible.com/t/combine-dictionaries-based-on-a-dictionary-key-value/33280 "2020-06-30T07:17:27Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [June 30, 2020, 7:17am UTC](https://forum.ansible.com/t/combine-dictionaries-based-on-a-dictionary-key-value/33280/1 "2020-06-30T07:17:27Z")

</div>

Create dictionaries from the lists

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;agg: "{{ agg|default({})|combine({item.node\_name: item}) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ aggregate\_info }}"  
&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;net: "{{ net|default({})|combine({item.node\_name: item}) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ net\_interface }}"

Then combine the corresponding dictionaries and concatenate the results into  
a new list

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aggregate\_info2: "{{ aggregate\_info2|default() +  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[agg[item]|combine(net[item])] }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ agg.keys()|list }}"

HTH,

&nbsp;&nbsp;-vlado
