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

# playbook/group_vars/all

  a: 1
  b: 2

# inventory/group_vars/main.yml

  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.

2 Answers 2


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.

  • Yes, my mistake. Well, hash_behaviour seems just right for my case! Thank you. Mar 7, 2018 at 12:24
  • Update 2021. The hash_behaviour is deprecated since ansible 2.10 reddit.com/r/ansible/comments/a7x92q/… So we'll go with separate variables having the same prefix.
    – geekQ
    May 7, 2021 at 15:47

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)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .