# List/Dictionary - Combine elements based on item value

**URL:** <https://forum.ansible.com/t/list-dictionary-combine-elements-based-on-item-value/33277>\
**Category:** Ansible Project\
**Created:** [June 29, 2020, 8:38pm UTC](https://forum.ansible.com/t/list-dictionary-combine-elements-based-on-item-value/33277 "2020-06-29T20:38:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adam\_McGill](https://avatars.discourse-cdn.com/v4/letter/a/a9adbd/32.png) [@Adam\_McGill](https://forum.ansible.com/u/Adam_McGill)\
**Post date:** [June 29, 2020, 8:38pm UTC](https://forum.ansible.com/t/list-dictionary-combine-elements-based-on-item-value/33277/1 "2020-06-29T20:38:26Z")

</div>

Hello,  
Basically want to add the items of net\_interface into aggregate\_info or create new list/dictionary based on key value of node\_name. Looking for assistance on how i can achieve this please and thank you.  
Outcome of what want it listed below.

**list called: aggregate\_info**

{  
“name”: “agg\_Data1\_Cluster01”,  
“node\_name”: “TEST\_NA\_CLUSTER\_01-01”,  
“percent\_used\_capacity”: “46”,  
“size\_available”: 8141263818752,  
“size\_avaliable\_80%”: 5122753242726,  
“size\_total”: 15092552880128,  
“size\_total\_80%”: 12074042304102,  
“size\_used”: 6951289061376  
},  
{  
“name”: “agg\_Data1\_Cluster02”,  
“node\_name”: “TEST\_NA\_CLUSTER\_01-02”,  
“percent\_used\_capacity”: “34”,  
“size\_available”: 5116275568640,  
“size\_avaliable\_80%”: 3561215869747,  
“size\_total”: 7775298494464,  
“size\_total\_80%”: 6220238795571,  
“size\_used”: 2659022925824  
}

**list called: net\_interface**

{  
“address”: “XX.XXX.X.XXX”,  
“address4oct”: “224”,  
“data\_protcol”: “none”,  
“failover\_group”: “Management”,  
“node\_name”: “TEST\_NA\_CLUSTER\_01-01”,  
“role”: “node\_mgmt”,  
“vserver”: “TEST\_NA\_CLUSTER\_01”  
},  
{  
“address”: “XX.XXX.X.XXX”,  
“address4oct”: “226”,  
“data\_protcol”: “none”,  
“failover\_group”: “Management”,  
“node\_name”: “IQ-REG\_NA\_CLUSTER\_01-02”,  
“role”: “node\_mgmt”,  
“vserver”: “TEST\_NA\_CLUSTER\_01”  
}

**Out Come want:**

Dictionary called: aggregate\_info (original) or new name

{  
“name”: “agg\_Data1\_Cluster01”,  
“node\_name”: “TEST\_NA\_CLUSTER\_01-01”,  
“percent\_used\_capacity”: “46”,  
“size\_available”: 8141263818752,  
“size\_avaliable\_80%”: 5122753242726,  
“size\_total”: 15092552880128,  
“size\_total\_80%”: 12074042304102,  
“size\_used”: 6951289061376

“address”: “XX.XXX.X.XXX”,  
“address4oct”: “224”,  
“data\_protcol”: “none”,  
“failover\_group”: “Management”,  
“role”: “node\_mgmt”,  
“vserver”: “TEST\_NA\_CLUSTER\_01”

},  
{  
“name”: “agg\_Data1\_Cluster02”,  
“node\_name”: “TEST\_NA\_CLUSTER\_01-02”,  
“percent\_used\_capacity”: “34”,  
“size\_available”: 5116275568640,  
“size\_avaliable\_80%”: 3561215869747,  
“size\_total”: 7775298494464,  
“size\_total\_80%”: 6220238795571,  
“size\_used”: 2659022925824

“address”: “XX.XXX.X.XXX”,  
“address4oct”: “226”,  
“data\_protcol”: “none”,  
“failover\_group”: “Management”,  
“role”: “node\_mgmt”,  
“vserver”: “TEST\_NA\_CLUSTER\_01”

}

---

<div class="post-metadata">

**Author:** ![Adam\_McGill](https://avatars.discourse-cdn.com/v4/letter/a/a9adbd/32.png) [@Adam\_McGill](https://forum.ansible.com/u/Adam_McGill)\
**Post date:** [June 29, 2020, 10:32pm UTC](https://forum.ansible.com/t/list-dictionary-combine-elements-based-on-item-value/33277/2 "2020-06-29T22:32:49Z")

</div>

Adding further to above, i know i can get the value i want by doing this.

- debug:  
msg: “{% for address in net\_interface if address.node\_name == ‘IQ-REG\_NA\_CLUSTER\_01-01’ %}{{ address.address }}{% endfor %}”

Anyway to add that logic when creating the aggregate\_info list. for example, but when trying to do this i get a template error. Don’t know if its possible to do or i am missing something with my syntax.

- name: Get\_Aggregate\_Info  
#no\_log: True  
set\_fact:  
aggregate\_info: “{{ aggregate\_info |  
d() + [ {  
‘name’: item.value.aggregate\_name,  
‘size\_available’: item.value.aggr\_space\_attributes.size\_available | int,  
‘size\_avaliable\_80%’: (((item.value.aggr\_space\_attributes.size\_total | int) \* 0.8) - (item.value.aggr\_space\_attributes.size\_used | int)) | round | int,  
‘size\_total’: item.value.aggr\_space\_attributes.size\_total | int,  
‘size\_total\_80%’: ((item.value.aggr\_space\_attributes.size\_total | int) \* 0.8 ) | round | int,  
‘size\_used’: item.value.aggr\_space\_attributes.size\_used | int,  
‘node\_name’: item.value.nodes.node\_name,  
‘percent\_used\_capacity’: item.value.aggr\_space\_attributes.percent\_used\_capacity  
‘test’: {% for address in net\_interface if address.node\_name == item.value.nodes.node\_name %}{{ address.address }}{% endfor %}  
} ] }}”  
with\_items: “{{ netapp\_info.ontap\_info.aggregate\_info | dict2items }}”  
when:
- (‘agg\_’ in item.value.aggregate\_name)

---

<div class="post-metadata">

**Author:** ![Adam\_McGill](https://avatars.discourse-cdn.com/v4/letter/a/a9adbd/32.png) [@Adam\_McGill](https://forum.ansible.com/u/Adam_McGill)\
**Post date:** [June 30, 2020, 3:24am UTC](https://forum.ansible.com/t/list-dictionary-combine-elements-based-on-item-value/33277/3 "2020-06-30T03:24:28Z")

</div>

Figured out how to merge the lists based on values in each list.

- name: merge lists  
set\_fact:  
merged\_list: “{{ merged\_list|default() + [{  
‘name’: item[0].name,  
‘size\_available’: item[0].size\_available,  
‘size\_avaliable\_80’: item[0].size\_avaliable\_80,  
‘size\_total’: item[0].size\_total,  
‘size\_total\_80’: item[0].size\_total\_80,  
‘size\_used’: item[0].size\_used,  
‘node\_name’: item[0].node\_name,  
‘percent\_used\_capacity’: item[0].percent\_used\_capacity,  
‘current\_node’: item[1].current\_node,  
‘address’: item[1].address,  
‘address4oct’: item[1].address4oct,  
‘failover\_group’: item[1].failover\_group,  
‘data\_protcol’: item[1].data\_protcol| default(‘’),  
‘role’: item[1].role,  
‘vserver’: item[1].vserver  
}] }}”  
when: “item[0].node\_name == item[1].current\_node”  
loop: “{{ query(‘nested’, aggregate\_info, net\_interface) }}”
