diff --git a/lib/ansible/vars/__init__.py b/lib/ansible/vars/__init__.py index e7f03ffb510..4496d20ab50 100644 --- a/lib/ansible/vars/__init__.py +++ b/lib/ansible/vars/__init__.py @@ -239,15 +239,15 @@ class VariableManager: # files and then any vars from host_vars files which may apply to # this host or the groups it belongs to - # we merge in vars from groups specified in the inventory (INI or script) - all_vars = combine_vars(all_vars, host.get_group_vars()) - - # then we merge in the special 'all' group_vars first, if they exist + # we merge in the special 'all' group_vars first, if they exist if 'all' in self._group_vars_files: data = preprocess_vars(self._group_vars_files['all']) for item in data: all_vars = combine_vars(all_vars, item) + # 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): if group.name in self._group_vars_files and group.name != 'all': for data in self._group_vars_files[group.name]: