Interview Prep/Ansible/Variables, Facts and Precedence Problems

`web01` is in both `prod` and `eu` groups, and `ntp_server` has a different value on every run you check

AdvancedTroubleshootingVariables, Facts and Precedence Problems
Answer

Likely causes → Both groups define ntp_server. For groups at the same level, Ansible merges in alphabetical order, so the last one wins (prod beats eu). A child group beats its parent. A teammate adding a group named zz_* changes the result without anyone noticing.

Diagnose →

ansible-inventory --host web01 --vars | jq .ntp_server
ansible-inventory --graph web01

Fix → Give one owner to each variable. To make the order explicit, use ansible_group_priority (higher wins; set it in inventory, not in group_vars):

eu:
  vars:
    ansible_group_priority: 10

Prevent → Design groups along separate dimensions (environment, region, role), and never define the same variable in two dimensions.

All Ansible questions Roadmap: Ansible

More on Variables, Facts and Precedence Problems