# Problem with function to merge nested dictionaries

**URL:** <https://forum.ansible.com/t/problem-with-function-to-merge-nested-dictionaries/19792>\
**Category:** Ansible Project\
**Created:** [November 18, 2015, 12:06pm UTC](https://forum.ansible.com/t/problem-with-function-to-merge-nested-dictionaries/19792 "2015-11-18T12:06:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Max\_Lock](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@Max\_Lock](https://forum.ansible.com/u/Max_Lock)\
**Post date:** [November 18, 2015, 12:06pm UTC](https://forum.ansible.com/t/problem-with-function-to-merge-nested-dictionaries/19792/1 "2015-11-18T12:06:53Z")

</div>

Hi All,

I’m using ansible 1.9.3, and want to merge two nested dicts to template to a config file. I’ve written a combine function similar to whats in 2.n.n. The problem is that the merged dictionary appears to be unicode preventing me from iterating over it in the template. This is the combine function:

`  
def combine(original, update):  
for key, value in original.items():  
if not key in update:  
update[key] = value  
elif isinstance(value, dict):  
combine(value, update[key])

return update

class FilterModule(object):

def filters(self):  
return {  
‘combine’: combine,  
}  
`

My testcase is this.

`

---

<div class="post-metadata">

**Author:** ![Max\_Lock](https://avatars.discourse-cdn.com/v4/letter/m/c57346/32.png) [@Max\_Lock](https://forum.ansible.com/u/Max_Lock)\
**Post date:** [November 23, 2015, 11:44am UTC](https://forum.ansible.com/t/problem-with-function-to-merge-nested-dictionaries/19792/2 "2015-11-23T11:44:45Z")

</div>

So this is the solution.

`

- set\_fact:  
c: “{{ a | combine(b) }}”  
`

[https://github.com/ansible/ansible/issues/5463](https://github.com/ansible/ansible/issues/5463)

set fact typecasts everything as a string when using key=value syntax. Use yaml syntax and you’re good to go.

Many many thanks to an off list unsung hero…

-Max.
