diff --git a/lib/ansible/inventory/host.py b/lib/ansible/inventory/host.py index 268eef2f46d..a48fbce6ffd 100644 --- a/lib/ansible/inventory/host.py +++ b/lib/ansible/inventory/host.py @@ -138,6 +138,6 @@ class Host: def get_group_vars(self): results = {} groups = self.get_groups() - for group in sorted(groups, key=lambda g: g.depth): + for group in sorted(groups, key=lambda g: (g.depth, g.name)): results = combine_vars(results, group.get_vars()) return results diff --git a/lib/ansible/vars/__init__.py b/lib/ansible/vars/__init__.py index 7a584545554..eba7916ebba 100644 --- a/lib/ansible/vars/__init__.py +++ b/lib/ansible/vars/__init__.py @@ -252,7 +252,7 @@ class VariableManager: # we merge in vars from groups specified in the inventory (INI or script) all_vars = combine_vars(all_vars, host.get_group_vars()) - for group in sorted(host.get_groups(), key=lambda g: g.depth): + for group in sorted(host.get_groups(), key=lambda g: (g.depth, g.name)): if group.name in self._group_vars_files and group.name != 'all': for data in self._group_vars_files[group.name]: data = preprocess_vars(data)