# Combine dictionaries based on a dictionary key value.

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

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [June 30, 2020, 8:00am UTC](https://forum.ansible.com/t/combine-dictionaries-based-on-a-dictionary-key-value/33281/1 "2020-06-30T08:00:14Z")

</div>

Hi,

a new lookup plugin was proposed for community.general which would help  
with this:  
[https://github.com/ansible-collections/community.general/pull/263](https://github.com/ansible-collections/community.general/pull/263)

We're currently trying to evaluate whether this plugin would be useful  
to the community. Feel free to comment there (or try it out!).

Cheers,  
Felix

---

<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:** [July 1, 2020, 6:14am UTC](https://forum.ansible.com/t/combine-dictionaries-based-on-a-dictionary-key-value/33281/2 "2020-07-01T06:14:20Z")

</div>

> a new lookup plugin was proposed for community.general which would help  
> with this:  
> [https://github.com/ansible-collections/community.general/pull/263](https://github.com/ansible-collections/community.general/pull/263)

I have proposed this filter plugin instead  
[https://github.com/ansible-collections/community.general/pull/604](https://github.com/ansible-collections/community.general/pull/604)

> cat filter\_plugins/list.py

from collections import defaultdict  
from operator import itemgetter

def lists\_mergeby(l1, l2, index):  
&nbsp;&nbsp;&nbsp;&nbsp;'''Merge lists by attribute index. Example:  
&nbsp;&nbsp;&nbsp;&nbsp;- debug: msg="{{ l1|lists\_mergeby(l2, 'index')|list }}"  
&nbsp;&nbsp;&nbsp;&nbsp;'''  
&nbsp;&nbsp;&nbsp;&nbsp;d = defaultdict(dict)  
&nbsp;&nbsp;&nbsp;&nbsp;for l in (l1, l2):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for elem in l:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d[elem[index]].update(elem)  
&nbsp;&nbsp;&nbsp;&nbsp;return sorted(d.values(), key=itemgetter(index))

class FilterModule(object):  
&nbsp;&nbsp;&nbsp;&nbsp;''' Ansible list filters '''

&nbsp;&nbsp;&nbsp;&nbsp;def filters(self):  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'lists\_mergeby': lists\_mergeby  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

Works fine also with Adam's data

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;aggregate\_info2: "{{ net\_interface|  
&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;lists\_mergeby(aggregate\_info, 'node\_name')|  
&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;list }}"  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: aggregate\_info2

give

&nbsp;&nbsp;aggregate\_info2:  
&nbsp;&nbsp;- address: XX.XXX.X.XXX  
&nbsp;&nbsp;&nbsp;&nbsp;address4oct: '224'  
&nbsp;&nbsp;&nbsp;&nbsp;data\_protcol: none  
&nbsp;&nbsp;&nbsp;&nbsp;failover\_group: Management  
&nbsp;&nbsp;&nbsp;&nbsp;name: agg\_Data1\_Cluster01"  
&nbsp;&nbsp;&nbsp;&nbsp;node\_name: TEST\_NA\_CLUSTER\_01-01  
&nbsp;&nbsp;&nbsp;&nbsp;percent\_used\_capacity: '46'  
&nbsp;&nbsp;&nbsp;&nbsp;role: node\_mgmt  
&nbsp;&nbsp;&nbsp;&nbsp;size\_available: 8141263818752  
&nbsp;&nbsp;&nbsp;&nbsp;size\_avaliable\_80%: 5122753242726  
&nbsp;&nbsp;&nbsp;&nbsp;size\_total: 15092552880128  
&nbsp;&nbsp;&nbsp;&nbsp;size\_total\_80%: 12074042304102  
&nbsp;&nbsp;&nbsp;&nbsp;size\_used: 6951289061376  
&nbsp;&nbsp;&nbsp;&nbsp;vserver: TEST\_NA\_CLUSTER\_01  
&nbsp;&nbsp;- address: XX.XXX.X.XXX  
&nbsp;&nbsp;&nbsp;&nbsp;address4oct: '226'  
&nbsp;&nbsp;&nbsp;&nbsp;data\_protcol: none  
&nbsp;&nbsp;&nbsp;&nbsp;failover\_group: Management  
&nbsp;&nbsp;&nbsp;&nbsp;name: agg\_Data1\_Cluster02  
&nbsp;&nbsp;&nbsp;&nbsp;node\_name: TEST\_NA\_CLUSTER\_01-02  
&nbsp;&nbsp;&nbsp;&nbsp;percent\_used\_capacity: '34'  
&nbsp;&nbsp;&nbsp;&nbsp;role: node\_mgmt  
&nbsp;&nbsp;&nbsp;&nbsp;size\_available: 5116275568640  
&nbsp;&nbsp;&nbsp;&nbsp;size\_avaliable\_80%: 3561215869747  
&nbsp;&nbsp;&nbsp;&nbsp;size\_total: 7775298494464  
&nbsp;&nbsp;&nbsp;&nbsp;size\_total\_80%: 6220238795571  
&nbsp;&nbsp;&nbsp;&nbsp;size\_used: 2659022925824  
&nbsp;&nbsp;&nbsp;&nbsp;vserver: TEST\_NA\_CLUSTER\_01
