If a host belongs to several groups that define the same variable, which one wins?

AdvancedConceptInventory
Answer

Rules for inventory group variables, from lowest to highest:

  1. all
  2. Parent groups
  3. Child groups (a child beats its parent)
  4. Host variables (beat any group)

For groups at the same level, they are merged in alphabetical order, so the last one alphabetically wins. You can override that with ansible_group_priority (higher wins, default 1), which is only honoured when set in the inventory source, not in group_vars/ files.

[prod:vars]
ansible_group_priority=10

Relying on alphabetical order is fragile, so avoid putting the same variable in sibling groups.

All Ansible questions Roadmap: Ansible

More on Inventory