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:
all- Parent groups
- Child groups (a child beats its parent)
- 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.
More on Inventory
What is an inventory and why does Ansible need one?BeginnerWhat are the INI and YAML inventory formats and how do they differ?BeginnerHow do you create groups of groups?BeginnerExplain host patterns: `:`, `:&`, `:!` and wildcards.IntermediateWhich connection variables can you set per host in inventory?BeginnerWhat are `group_vars` and `host_vars` directories?Intermediate