# merging (environment) dicts?

**URL:** <https://forum.ansible.com/t/merging-environment-dicts/15187>\
**Category:** Ansible Project\
**Created:** [May 7, 2014, 4:53pm UTC](https://forum.ansible.com/t/merging-environment-dicts/15187 "2014-05-07T16:53:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dick\_Davies1](https://avatars.discourse-cdn.com/v4/letter/d/22d042/32.png) [@Dick\_Davies1](https://forum.ansible.com/u/Dick_Davies1)\
**Post date:** [May 7, 2014, 4:53pm UTC](https://forum.ansible.com/t/merging-environment-dicts/15187/1 "2014-05-07T16:53:58Z")

</div>

I've got a task that needs a proxy environment car along with some app-specific env vars to be set.

seems like I can only apply one dict to the environment: field for the task.

does anyone know if I can merge these dicts in Ansible ? this is within a role if that makes any difference.

Thanks!

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [May 7, 2014, 5:15pm UTC](https://forum.ansible.com/t/merging-environment-dicts/15187/2 "2014-05-07T17:15:22Z")

</div>

​You can, but it's general config only, so all or nothing:

$ grep merge ansible.cfg  
# hash can be merged, or replaced  
hash\_behaviour=merge

​Serge​

---

<div class="post-metadata">

**Author:** ![Andrei\_Pashkin](https://avatars.discourse-cdn.com/v4/letter/a/a87d85/32.png) [@Andrei\_Pashkin](https://forum.ansible.com/u/Andrei_Pashkin)\
**Post date:** [May 7, 2014, 5:17pm UTC](https://forum.ansible.com/t/merging-environment-dicts/15187/3 "2014-05-07T17:17:28Z")

</div>

What if it is dict of dicts?

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [May 7, 2014, 5:19pm UTC](https://forum.ansible.com/t/merging-environment-dicts/15187/4 "2014-05-07T17:19:37Z")

</div>

With that merge setting you will also get deep merge.​

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [May 7, 2014, 6:34pm UTC](https://forum.ansible.com/t/merging-environment-dicts/15187/5 "2014-05-07T18:34:59Z")

</div>

Thanks Serge

trouble is i need the replace behaviour elsewhere in the play \>\_\<

Looks like I'll have to do a tactical dirty hack with set\_fact and  
inline jinja2 or something.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 10, 2014, 12:00am UTC](https://forum.ansible.com/t/merging-environment-dicts/15187/6 "2014-05-10T00:00:32Z")

</div>

You may be able to try this directly in Jinja2.

Warning: NOT TESTED

environment: “{{ base\_env.update(my\_env) }}”

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [May 17, 2014, 12:57pm UTC](https://forum.ansible.com/t/merging-environment-dicts/15187/7 "2014-05-17T12:57:53Z")

</div>

Thanks, I gave up and went with a shell script due to time pressures,  
but will bookmark this approach for next time 🙂

---

<div class="post-metadata">

**Author:** ![Andrei\_Pashkin](https://avatars.discourse-cdn.com/v4/letter/a/a87d85/32.png) [@Andrei\_Pashkin](https://forum.ansible.com/u/Andrei_Pashkin)\
**Post date:** [May 21, 2014, 9:41am UTC](https://forum.ansible.com/t/merging-environment-dicts/15187/8 "2014-05-21T09:41:08Z")

</div>

`Kind of works:`

> `- name: test playbook`  
> `hosts: 127.0.0.1`  
> `connection: local`  
> `gather_facts: no`  
> `vars:`  
> `- somevar: {'a': 1}`  
> `- anothervar: {'b': 2}`  
> `tasks:`  
> `- debug: msg="{{ somevar.update(anothervar) }}{{ somevar }}"`

But it is desireable to have `merge_dicts` filter for that.

---

<div class="post-metadata">

**Author:** ![Jiri](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@Jiri](https://forum.ansible.com/u/Jiri)\
**Post date:** [March 17, 2015, 2:28pm UTC](https://forum.ansible.com/t/merging-environment-dicts/15187/9 "2015-03-17T14:28:42Z")

</div>

I have created a pull request to extend the union filter with support to merge two dicts:[https://github.com/ansible/ansible/pull/10483](https://github.com/ansible/ansible/pull/10483)

Please feel free to comment on this feature there.
