diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py index 00ee28debba..972042c9b14 100644 --- a/lib/ansible/inventory/__init__.py +++ b/lib/ansible/inventory/__init__.py @@ -146,7 +146,7 @@ class Inventory(object): # get host vars from host_vars/ files and vars plugins for host in self.get_hosts(): - host.vars = utils.combine_vars(host.vars, self.get_variables(host.name, vault_password=self._vault_password)) + host.vars = utils.combine_vars(host.vars, self.get_host_variables(host.name, vault_password=self._vault_password)) def _match(self, str, pattern_str): @@ -324,6 +324,7 @@ class Inventory(object): if ungrouped is None: self.add_group(Group('ungrouped')) ungrouped = self.get_group('ungrouped') + self.get_group('all').add_child_group(ungrouped) ungrouped.add_host(new_host) return new_host @@ -429,20 +430,22 @@ class Inventory(object): if updated is not None: vars = utils.combine_vars(vars, updated) - # get group variables set by Inventory Parsers - vars = utils.combine_vars(vars, group.get_variables()) - # Read group_vars/ files vars = utils.combine_vars(vars, self.get_group_vars(group)) return vars def get_variables(self, hostname, update_cached=False, vault_password=None): + + return self.get_host(hostname).get_variables() + + def get_host_variables(self, hostname, update_cached=False, vault_password=None): + if hostname not in self._vars_per_host or update_cached: - self._vars_per_host[hostname] = self._get_variables(hostname, vault_password=vault_password) + self._vars_per_host[hostname] = self._get_host_variables(hostname, vault_password=vault_password) return self._vars_per_host[hostname] - def _get_variables(self, hostname, vault_password=None): + def _get_host_variables(self, hostname, vault_password=None): host = self.get_host(hostname) if host is None: @@ -450,10 +453,6 @@ class Inventory(object): vars = {} - # special case for ungrouped hosts, make sure group_vars/all is loaded - if len(host.groups) == 1 and host.groups[0].name == 'ungrouped': - vars = self.get_group_variables('all', vault_password=self._vault_password) - # plugin.run retrieves all vars (also from groups) for host vars_results = [ plugin.run(host, vault_password=vault_password) for plugin in self._vars_plugins if hasattr(plugin, 'run')] for updated in vars_results: @@ -466,9 +465,6 @@ class Inventory(object): if updated is not None: vars = utils.combine_vars(vars, updated) - # get host variables set by Inventory Parsers - vars = utils.combine_vars(vars, host.get_variables()) - # still need to check InventoryParser per host vars # which actually means InventoryScript per host, # which is not performant diff --git a/lib/ansible/runner/action_plugins/group_by.py b/lib/ansible/runner/action_plugins/group_by.py index d00ddc8d8cf..25c2073fa0f 100644 --- a/lib/ansible/runner/action_plugins/group_by.py +++ b/lib/ansible/runner/action_plugins/group_by.py @@ -93,6 +93,7 @@ class ActionModule(object): if not inv_group: inv_group = ansible.inventory.Group(name=group) inventory.add_group(inv_group) + inventory.get_group('all').add_child_group(inv_group) inv_group.vars = inventory.get_group_variables(group, update_cached=False, vault_password=inventory._vault_password) for host in hosts: if host in self.runner.inventory._vars_per_host: