Interview Prep/Ansible/Inventory and Targeting

Two groups define the same variable `app_port` with different values and a host is in both. Which wins and how do you control it?

AdvancedScenarioInventory and Targeting
Answer

Situation read → Same-precedence group variables with a merge conflict.

What I'd do →

  1. Know the rule: at the same level, groups are merged in alphabetical order and the last one wins, unless ansible_group_priority is set.
  2. Set ansible_group_priority in the group that should win (higher wins).
  3. Better, avoid the conflict by defining the variable at one level.
# inventory source (not group_vars)
[canary:vars]
ansible_group_priority=20

Why → Relying on alphabetical order is fragile; an explicit priority documents intent.

Interview tip → ansible_group_priority only works when set in the inventory source, not in group_vars/ files, and ansible-inventory --host confirms the winner.

All Ansible questions Roadmap: Ansible

More on Inventory and Targeting