From b87710a1df5bdc22ee9679e3e55cea35b887a5d1 Mon Sep 17 00:00:00 2001 From: Jeroen Hoekx Date: Sat, 28 Apr 2012 18:23:44 +0200 Subject: [PATCH] Introduce group_names in template variables. This is a list of all the groups a host is in. --- lib/ansible/inventory.py | 14 ++++++++++---- test/TestInventory.py | 25 ++++++++++++++++++------- test/yaml_hosts | 5 +++++ 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/lib/ansible/inventory.py b/lib/ansible/inventory.py index 7cb8c7253eb..ef790b6d313 100644 --- a/lib/ansible/inventory.py +++ b/lib/ansible/inventory.py @@ -81,13 +81,19 @@ class Inventory(object): def get_variables(self, host): """ Return the variables associated with this host. """ + variables = {} if host in self._variables: - return self._variables[host].copy() + variables.update(self._variables[host].copy()) - if not self._is_script: - return {} + if self._is_script: + variables.update(self._get_variables_from_script(host)) - return self._get_variables_from_script(host) + variables['group_names'] = [] + for name,hosts in self.groups.iteritems(): + if host in hosts: + variables['group_names'].append(name) + + return variables # ***************************************************** diff --git a/test/TestInventory.py b/test/TestInventory.py index 96991d77d00..c136430d18d 100644 --- a/test/TestInventory.py +++ b/test/TestInventory.py @@ -85,13 +85,13 @@ class TestInventory(unittest.TestCase): inventory = self.simple_inventory() vars = inventory.get_variables('thor') - assert vars == {} + assert vars == {'group_names': ['norse']} def test_simple_port(self): inventory = self.simple_inventory() vars = inventory.get_variables('hera') - assert vars == {'ansible_ssh_port': 3000} + assert vars == {'ansible_ssh_port': 3000, 'group_names': ['greek']} ### Inventory API tests @@ -146,7 +146,7 @@ class TestInventory(unittest.TestCase): inventory = self.script_inventory() vars = inventory.get_variables('thor') - assert vars == {"hammer":True} + assert vars == {"hammer":True, 'group_names': ['norse']} ### Tests for yaml inventory file @@ -205,7 +205,7 @@ class TestInventory(unittest.TestCase): inventory = self.yaml_inventory() vars = inventory.get_variables('thor') - assert vars == {"hammer":True} + assert vars == {"hammer":True, 'group_names': ['norse']} def test_yaml_change_vars(self): inventory = self.yaml_inventory() @@ -214,19 +214,30 @@ class TestInventory(unittest.TestCase): vars["hammer"] = False vars = inventory.get_variables('thor') - assert vars == {"hammer":True} + assert vars == {"hammer":True, 'group_names': ['norse']} def test_yaml_host_vars(self): inventory = self.yaml_inventory() vars = inventory.get_variables('saturn') - assert vars == {"moon":"titan", "moon2":"enceladus"} + assert vars == {"moon":"titan", + "moon2":"enceladus", + 'group_names': ['multiple']} def test_yaml_port(self): inventory = self.yaml_inventory() vars = inventory.get_variables('hera') - assert vars == {'ansible_ssh_port': 3000, 'ntp_server': 'olympus.example.com'} + assert vars == {'ansible_ssh_port': 3000, + 'ntp_server': 'olympus.example.com', + 'group_names': ['greek']} + + def test_yaml_multiple_groups(self): + inventory = self.yaml_inventory() + vars = inventory.get_variables('odin') + + assert 'group_names' in vars + assert sorted(vars['group_names']) == [ 'norse', 'ruler' ] ### Test Runner class method diff --git a/test/yaml_hosts b/test/yaml_hosts index 628c2843506..8d98d5133e4 100644 --- a/test/yaml_hosts +++ b/test/yaml_hosts @@ -25,6 +25,11 @@ - odin - loki +- group: ruler + hosts: + - zeus + - odin + - group: multiple hosts: - saturn