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.