merging (environment) dicts?

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!

​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​

What if it is dict of dicts?

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

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.

You may be able to try this directly in Jinja2.

Warning: NOT TESTED

environment: “{{ base_env.update(my_env) }}”

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

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.

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

Please feel free to comment on this feature there.