Interview Prep/Ansible/Variables, Facts and Precedence Problems

`group_vars/prod.yml` defines `app_config: {log_level: warn}` and the other keys from `group_vars/all.yml` disappear

IntermediateTroubleshootingVariables, Facts and Precedence Problems
Answer

Likely causes → Dictionaries are replaced, not merged. The default hash_behaviour is replace, so the more specific app_config replaces the whole dictionary.

Fix → Use separate names for each layer and merge them explicitly:

# all.yml
app_config_defaults: {log_level: info, workers: 4, port: 8080}
# prod.yml
app_config_overrides: {log_level: warn}
# role
app_config: "{{ app_config_defaults | combine(app_config_overrides | default({}), recursive=true) }}"

Prevent → Do not set hash_behaviour = merge globally. It is deprecated in spirit and changes behaviour everywhere. Prefer flat variables for values that need overriding.

All Ansible questions Roadmap: Ansible

More on Variables, Facts and Precedence Problems