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 →
- Know the rule: at the same level, groups are merged in alphabetical order and the last one wins, unless
ansible_group_priorityis set. - Set
ansible_group_priorityin the group that should win (higher wins). - 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.
More on Inventory and Targeting
You are handed a fresh Ansible project and 10 servers. How do you make sure Ansible can reach all of them before writing any playbook?BeginnerYour team keeps prod and staging hosts in one inventory file and someone ran a playbook on prod by mistake. How do you prevent it?IntermediateYou need to run a playbook on only the web servers in Mumbai that are not in the canary group. How?BeginnerServers are autoscaled in AWS and IPs change daily. How do you keep the inventory accurate?IntermediateA host is reachable by SSH on port 2222 with a different user from all others. How do you model that?BeginnerYou must configure servers in a private subnet that is only reachable through a bastion.Intermediate