# Merge variables (dicts, lists) with same names from different yml files

**URL:** <https://forum.ansible.com/t/merge-variables-dicts-lists-with-same-names-from-different-yml-files/30119>\
**Category:** Ansible Developer\
**Created:** [April 16, 2019, 8:24am UTC](https://forum.ansible.com/t/merge-variables-dicts-lists-with-same-names-from-different-yml-files/30119 "2019-04-16T08:24:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frank\_Fllr](https://avatars.discourse-cdn.com/v4/letter/f/35a633/32.png) [@Frank\_Fllr](https://forum.ansible.com/u/Frank_Fllr)\
**Post date:** [April 16, 2019, 8:24am UTC](https://forum.ansible.com/t/merge-variables-dicts-lists-with-same-names-from-different-yml-files/30119/1 "2019-04-16T08:24:46Z")

</div>

Hi,

I need to loop over a directory with yml files all containing the same variable names (dicts, list, simple vars) e.g.

---- file1.yml ----

serviceconf:  
document:  
xxx: “{{ ansible\_hostname }}”:8080

proxyconf:

- /api/document

---- file2.yml ----  
serviceconf:  
portalcode:  
val: “{{ binary\_content\_dir }}/special”

proxyconf:

- /api/portalcode
- /api/imports

And would like to have as result:

serviceconf:

document:  
xxx: “{{ ansible\_hostname }}”:8080

portalcode:  
val: “{{ binary\_content\_dir }}/special”

proxyconf:

- /api/document1

- /api/other1

- /api/portalcode

- /api/imports

While searching on google, I found _include\_vars\_merged_ action plugin ([https://gist.github.com/udondan/b730206056a018cf9838](https://gist.github.com/udondan/b730206056a018cf9838)) which I though could be suitable for my task - but unfortunatly it is not compatible with Ansible 2+.

My question: Is there a possiblity to do the desired task (with standard ansible means) or has someone developed a similiar _include\_vars\_merged_ for Ansible 2.7+ and like to share it?

Tnx in advance  
Frank

---

<div class="post-metadata">

**Author:** ![Surjeet\_Singh](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@Surjeet\_Singh](https://forum.ansible.com/u/Surjeet_Singh)\
**Post date:** [May 5, 2019, 5:33am UTC](https://forum.ansible.com/t/merge-variables-dicts-lists-with-same-names-from-different-yml-files/30119/2 "2019-05-05T05:33:25Z")

</div>

I think this issue is because of you are trying to merge different data format please check you data format you have used at different places like group\_vars and vars.

Regards/Surjeet

---

<div class="post-metadata">

**Author:** ![Tchia04](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@Tchia04](https://forum.ansible.com/u/Tchia04)\
**Post date:** [May 5, 2019, 4:45pm UTC](https://forum.ansible.com/t/merge-variables-dicts-lists-with-same-names-from-different-yml-files/30119/3 "2019-05-05T16:45:51Z")

</div>

Hi Frank,  
There is an option you can add to the default section of the ansible.cfg to merge the results instead of replacing them which is the default.

```auto
hash_behaviour = replace

```

You can find more information at  
[https://docs.ansible.com/ansible/2.4/intro\_configuration.html#hash-behaviour](https://docs.ansible.com/ansible/2.4/intro_configuration.html#hash-behaviour)

Cheers,  
Tony Chia
