From f9b3f4f93442a8d9e2090d1d1a739741fdb9c589 Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Mon, 28 Aug 2017 17:21:11 -0400 Subject: [PATCH] make groups magic var dependant on inventory (#28677) * make groups magic var dependant on inventory it was overtly restricted by 'host' minor fixes to test_var_manager.py, need to test other values also * pepe hates extra blank line --- lib/ansible/vars/manager.py | 5 +---- test/units/vars/test_variable_manager.py | 12 +++++------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/lib/ansible/vars/manager.py b/lib/ansible/vars/manager.py index 0659b825e0..854b545966 100644 --- a/lib/ansible/vars/manager.py +++ b/lib/ansible/vars/manager.py @@ -457,10 +457,6 @@ class VariableManager: variables['playbook_dir'] = os.path.abspath(self._loader.get_basedir()) variables['ansible_playbook_python'] = sys.executable - if host: - # host already provides some magic vars via host.get_vars() - if self._inventory: - variables['groups'] = self._inventory.get_groups_dict() if play: variables['role_names'] = [r._role_name for r in play.roles] @@ -471,6 +467,7 @@ class VariableManager: variables['role_uuid'] = text_type(task._role._uuid) if self._inventory is not None: + variables['groups'] = self._inventory.get_groups_dict() if play: templar = Templar(loader=self._loader) if templar.is_template(play.hosts): diff --git a/test/units/vars/test_variable_manager.py b/test/units/vars/test_variable_manager.py index c32bbc8850..5f16a883c1 100644 --- a/test/units/vars/test_variable_manager.py +++ b/test/units/vars/test_variable_manager.py @@ -50,14 +50,12 @@ class TestVariableManager(unittest.TestCase): mock_inventory = MagicMock() v = VariableManager(loader=fake_loader, inventory=mock_inventory) - vars = v.get_vars(use_cache=False) + variables = v.get_vars(use_cache=False) - # FIXME: not sure why we remove all and only test playbook_dir - for remove in ['omit', 'vars', 'ansible_version', 'ansible_check_mode', 'ansible_playbook_python']: - if remove in vars: - del vars[remove] - - self.assertEqual(vars, dict(playbook_dir=os.path.abspath('.'))) + # Check var manager expected values, never check: ['omit', 'vars'] + # FIXME: add the following ['ansible_version', 'ansible_playbook_python', 'groups'] + for varname, value in (('playbook_dir', os.path.abspath('.')), ): + self.assertEqual(variables[varname], value) def test_variable_manager_extra_vars(self): fake_loader = DictDataLoader({})