Linux – How to merge variables of type hash across different variable files in ansible

ansibleautomationlinuxyaml

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.

Best Answer

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.