merging (environment) dicts?

488 views
Skip to first unread message

Dick Davies

unread,
May 7, 2014, 12:53:58 PM5/7/14
to ansible list

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!


Serge van Ginderachter

unread,
May 7, 2014, 1:15:22 PM5/7/14
to ansible...@googlegroups.com

On 7 May 2014 18:53, Dick Davies <rasp...@gmail.com> wrote:
does anyone know if I can merge these dicts in Ansible ? this is within a role if that makes any difference.

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

Andrew Pashkin

unread,
May 7, 2014, 1:17:28 PM5/7/14
to ansible...@googlegroups.com
What if it is dict of dicts?
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAEhzMJB%3DOFn6zu-75FRxw%3Dric4WttfK0Gzous5cK_Gwi%3DyfCmw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

-- 
С наилучшими пожеланиями, Андрей Пашкин.
м.т - +7 (985) 898 57 59
Skype - waves_in_fluids
e-mail - andrew....@gmx.co.uk

Serge van Ginderachter

unread,
May 7, 2014, 1:19:37 PM5/7/14
to ansible...@googlegroups.com

On 7 May 2014 19:17, Andrew Pashkin <andrew....@gmx.co.uk> wrote:
What if it is dict of dicts?

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

Dick Davies

unread,
May 7, 2014, 2:34:59 PM5/7/14
to ansible list
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.

Michael DeHaan

unread,
May 9, 2014, 8:00:32 PM5/9/14
to ansible...@googlegroups.com
You may be able to try this directly in Jinja2.

Warning:  NOT TESTED

environment:  "{{ base_env.update(my_env) }}"




Dick Davies

unread,
May 17, 2014, 8:57:53 AM5/17/14
to ansible list
Thanks, I gave up and went with a shell script due to time pressures,
but will bookmark this approach for next time :)
> https://groups.google.com/d/msgid/ansible-project/CA%2BnsWgzZmEY%3DdCu2xXx521LdvB9w9TRMQ9JENi0Z8Uaruhm11Q%40mail.gmail.com.

Andrew Pashkin

unread,
May 21, 2014, 5:41:08 AM5/21/14
to ansible...@googlegroups.com
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.
-- 
With kind regards, Andrew Pashkin.
cell phone - +7 (985) 898 57 59

Jiri

unread,
Mar 17, 2015, 10:28:42 AM3/17/15
to ansible...@googlegroups.com
On Wednesday, May 21, 2014 at 10:41:08 AM UTC+1, Andrew Pashkin wrote:
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:

Please feel free to comment on this feature there.
Reply all
Reply to author
Forward
0 new messages