1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Introduce group_names in template variables.

This is a list of all the groups a host is in.
This commit is contained in:
Jeroen Hoekx 2012-04-28 18:23:44 +02:00
parent 6341361a5b
commit b87710a1df
3 changed files with 33 additions and 11 deletions

View file

@ -81,13 +81,19 @@ class Inventory(object):
def get_variables(self, host): def get_variables(self, host):
""" Return the variables associated with this host. """ """ Return the variables associated with this host. """
variables = {}
if host in self._variables: if host in self._variables:
return self._variables[host].copy() variables.update(self._variables[host].copy())
if not self._is_script: if self._is_script:
return {} 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
# ***************************************************** # *****************************************************

View file

@ -85,13 +85,13 @@ class TestInventory(unittest.TestCase):
inventory = self.simple_inventory() inventory = self.simple_inventory()
vars = inventory.get_variables('thor') vars = inventory.get_variables('thor')
assert vars == {} assert vars == {'group_names': ['norse']}
def test_simple_port(self): def test_simple_port(self):
inventory = self.simple_inventory() inventory = self.simple_inventory()
vars = inventory.get_variables('hera') vars = inventory.get_variables('hera')
assert vars == {'ansible_ssh_port': 3000} assert vars == {'ansible_ssh_port': 3000, 'group_names': ['greek']}
### Inventory API tests ### Inventory API tests
@ -146,7 +146,7 @@ class TestInventory(unittest.TestCase):
inventory = self.script_inventory() inventory = self.script_inventory()
vars = inventory.get_variables('thor') vars = inventory.get_variables('thor')
assert vars == {"hammer":True} assert vars == {"hammer":True, 'group_names': ['norse']}
### Tests for yaml inventory file ### Tests for yaml inventory file
@ -205,7 +205,7 @@ class TestInventory(unittest.TestCase):
inventory = self.yaml_inventory() inventory = self.yaml_inventory()
vars = inventory.get_variables('thor') vars = inventory.get_variables('thor')
assert vars == {"hammer":True} assert vars == {"hammer":True, 'group_names': ['norse']}
def test_yaml_change_vars(self): def test_yaml_change_vars(self):
inventory = self.yaml_inventory() inventory = self.yaml_inventory()
@ -214,19 +214,30 @@ class TestInventory(unittest.TestCase):
vars["hammer"] = False vars["hammer"] = False
vars = inventory.get_variables('thor') vars = inventory.get_variables('thor')
assert vars == {"hammer":True} assert vars == {"hammer":True, 'group_names': ['norse']}
def test_yaml_host_vars(self): def test_yaml_host_vars(self):
inventory = self.yaml_inventory() inventory = self.yaml_inventory()
vars = inventory.get_variables('saturn') 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): def test_yaml_port(self):
inventory = self.yaml_inventory() inventory = self.yaml_inventory()
vars = inventory.get_variables('hera') 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 ### Test Runner class method

View file

@ -25,6 +25,11 @@
- odin - odin
- loki - loki
- group: ruler
hosts:
- zeus
- odin
- group: multiple - group: multiple
hosts: hosts:
- saturn - saturn