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

Define 'inventory_hostname' variable for hosts.

This commit is contained in:
Jeroen Hoekx 2012-05-02 09:56:58 +02:00
parent 594084bc9f
commit 03541baba7
2 changed files with 23 additions and 9 deletions

View file

@ -81,7 +81,10 @@ class Inventory(object):
def get_variables(self, host):
""" Return the variables associated with this host. """
variables = {}
variables = {
'inventory_hostname': host,
}
if host in self._variables:
variables.update(self._variables[host].copy())

View file

@ -85,13 +85,16 @@ class TestInventory(unittest.TestCase):
inventory = self.simple_inventory()
vars = inventory.get_variables('thor')
assert vars == {'group_names': ['norse']}
assert vars == {'group_names': ['norse'],
'inventory_hostname': 'thor'}
def test_simple_port(self):
inventory = self.simple_inventory()
vars = inventory.get_variables('hera')
assert vars == {'ansible_ssh_port': 3000, 'group_names': ['greek']}
assert vars == {'ansible_ssh_port': 3000,
'group_names': ['greek'],
'inventory_hostname': 'hera'}
### Inventory API tests
@ -146,7 +149,9 @@ class TestInventory(unittest.TestCase):
inventory = self.script_inventory()
vars = inventory.get_variables('thor')
assert vars == {"hammer":True, 'group_names': ['norse']}
assert vars == {'hammer':True,
'group_names': ['norse'],
'inventory_hostname': 'thor'}
### Tests for yaml inventory file
@ -204,8 +209,10 @@ class TestInventory(unittest.TestCase):
def test_yaml_vars(self):
inventory = self.yaml_inventory()
vars = inventory.get_variables('thor')
assert vars == {"hammer":True, 'group_names': ['norse']}
print vars
assert vars == {'group_names': ['norse'],
'hammer':True,
'inventory_hostname': 'thor'}
def test_yaml_change_vars(self):
inventory = self.yaml_inventory()
@ -214,14 +221,17 @@ class TestInventory(unittest.TestCase):
vars["hammer"] = False
vars = inventory.get_variables('thor')
assert vars == {"hammer":True, 'group_names': ['norse']}
assert vars == {'hammer':True,
'inventory_hostname': 'thor',
'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 == {'inventory_hostname': 'saturn',
'moon': 'titan',
'moon2': 'enceladus',
'group_names': ['multiple']}
def test_yaml_port(self):
@ -229,6 +239,7 @@ class TestInventory(unittest.TestCase):
vars = inventory.get_variables('hera')
assert vars == {'ansible_ssh_port': 3000,
'inventory_hostname': 'hera',
'ntp_server': 'olympus.example.com',
'group_names': ['greek']}