From a2cfe8770b9a93ed2a1372396fa7a1e648bd658e Mon Sep 17 00:00:00 2001 From: Serge van Ginderachter Date: Tue, 19 Aug 2014 14:55:57 +0200 Subject: [PATCH 1/3] Inventory: fix logic mistake in loading/retrieving variables for groups --- lib/ansible/inventory/__init__.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py index 00ee28debba..4ea9c9dcc42 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): @@ -429,20 +429,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: @@ -466,9 +468,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 From 3c06833302af823d5cfa792c8490bf01094fff54 Mon Sep 17 00:00:00 2001 From: Serge van Ginderachter Date: Tue, 19 Aug 2014 14:56:32 +0200 Subject: [PATCH 2/3] group_by: make sure new group has bigger priority than all group --- lib/ansible/runner/action_plugins/group_by.py | 1 + 1 file changed, 1 insertion(+) 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: From 0ab721d51e77ba4779b86b200128ca08cdcd6b7b Mon Sep 17 00:00:00 2001 From: Serge van Ginderachter Date: Tue, 19 Aug 2014 16:33:59 +0200 Subject: [PATCH 3/3] better logic for all-group variables for implicit localhost --- lib/ansible/inventory/__init__.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py index 4ea9c9dcc42..972042c9b14 100644 --- a/lib/ansible/inventory/__init__.py +++ b/lib/ansible/inventory/__init__.py @@ -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 @@ -452,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: