# Merge lists

**URL:** <https://forum.ansible.com/t/merge-lists/30612>\
**Category:** Ansible Project\
**Created:** [June 20, 2019, 9:00am UTC](https://forum.ansible.com/t/merge-lists/30612 "2019-06-20T09:00:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Punit\_Jain](https://avatars.discourse-cdn.com/v4/letter/p/e56c9b/32.png) [@Punit\_Jain](https://forum.ansible.com/u/Punit_Jain)\
**Post date:** [June 20, 2019, 9:00am UTC](https://forum.ansible.com/t/merge-lists/30612/1 "2019-06-20T09:00:53Z")

</div>

Hi,

I have a fact as below:

“dns\_parsed”: [  
“10.4.3.2”  
]

and another fact as:

“actual\_config”: {  
“dns”: {  
“domains”: [‘[test.com](http://test.com)’],  
“servers”:   
}  
}

I want to update actual\_config to make it:

“actual\_config”: {  
“dns”: {  
“domains”: [‘[test.com](http://test.com)’],  
“servers”: [“10.4.3.2”]  
}  
}

my playbook part to accomplish this:

- name: merge dns data  
set\_fact:  
actual\_config: “{{ actual\_config.dns.servers | union(dns\_parsed) }}”

- debug:  
var=actual\_config

however it removes all other keys from actual\_config.

“actual\_config”: [  
“10.4.3.2”  
]

Any clue how to make it work ?

Thanks and Regards,  
Punit

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [June 20, 2019, 9:14am UTC](https://forum.ansible.com/t/merge-lists/30612/2 "2019-06-20T09:14:21Z")

</div>

You’re setting a dict to one of its own keys - I think that doesn’t work or at least results in nothing. But the union of that and dns\_parsed will be the dns\_parsed.

You could look into the ‘combine’ filter to overwrite a specific key.

Dick

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [June 20, 2019, 10:37am UTC](https://forum.ansible.com/t/merge-lists/30612/3 "2019-06-20T10:37:19Z")

</div>

My first assumption was wrong. But the combine filter is the way,  
you'd have to use the recursive option and create the containing dict,  
like this:

&nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;actual\_config: "{{ actual\_config | combine({'dns': {  
'servers': dns\_parsed }}, recursive=True) }}"

Dick

---

<div class="post-metadata">

**Author:** ![Punit\_Jain](https://avatars.discourse-cdn.com/v4/letter/p/e56c9b/32.png) [@Punit\_Jain](https://forum.ansible.com/u/Punit_Jain)\
**Post date:** [June 20, 2019, 11:04am UTC](https://forum.ansible.com/t/merge-lists/30612/4 "2019-06-20T11:04:11Z")

</div>

Thanks Dick works !!!
