Interview Prep/Ansible/Variables, Facts and Precedence

How do you merge two dictionaries, and what happened to `hash_behaviour`?

AdvancedConceptVariables, Facts and Precedence
Answer

By default, a higher-precedence dict replaces a lower one completely instead of merging. The hash_behaviour = merge setting changes that globally, but it is deprecated because it makes variables unpredictable.

Merge explicitly with the combine filter:

app_config: "{{ app_config_defaults | combine(app_config_overrides | default({}), recursive=true) }}"

recursive=true merges nested dicts; lists are not merged and are replaced. The same pattern, a *_defaults dict plus a *_overrides dict, is a clean way to design role variables.

All Ansible questions Roadmap: Ansible

More on Variables, Facts and Precedence