6

I have two YAML variable files which are reused for many playbooks:

# playbook/group_vars/all

settings:
  a: 1
  b: 2

# inventory/group_vars/main.yml

settings:
  c: 3

I want settings to be equal { a: 1, b: 2, c: 3 } in my playbooks.

Instead it equals { c: 3 } because inventory/group_vars/* has higher priority than playbook/group_vars/all and because hashes are overridden, not merged.

I see the following options:

  • Define variables separately, using naming conventions and prefixes, instead of using hashes, like so:

    # playbook/group_vars/all
    
    settings_a: 1
    settings_b: 2
    
    # inventory/group_vars/main.yml
    
    settings_c: 3
    

Our team does not consider this solution elegant enough.

  • Use set_fact and/or Jinja's "+" operator

{{ list_common + list_specific }}

This requires changes in many playbooks (we do have many of them) and also looks bad.

--

Are there other options to merge hashes from different files instead of redefining (overriding) them?

What we are trying to achieve is to stop duplicating lists across variable files and define only those parameters which are specific to the group/host.

Jason Holcomb
  • 63
  • 1
  • 5

2 Answers2

5

First of all, list is [1, 2, 3] and you talk about dictionary {a:1, b:2, c:3} or in other words hash.

There is configurable parameter: hash_behaviour.

By default it is replace, but you can set it to merge to achieve desired behaviour.

Konstantin Suvorov
  • 3,836
  • 1
  • 11
  • 13
  • Yes, my mistake. Well, hash_behaviour seems just right for my case! Thank you. – Jason Holcomb Mar 07 '18 at 12:24
  • Update 2021. The `hash_behaviour` is deprecated since ansible 2.10 https://www.reddit.com/r/ansible/comments/a7x92q/in_which_case_hash_behaviourmerge_can_become/ec8mf7f/ So we'll go with separate variables having the same prefix. – geekQ May 07 '21 at 15:47
0

Starting with Ansible v2.0 you can do it:

- name: merging hash_a and hash_b into hash_c
  set_fact: hash_c="{{ hash_a|combine(hash_b) }}"

Check more under Ansible filters - Combining hashes/dictionaries (coming from Jinja2)

mPrinC
  • 101
  • 1