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

Merge pull request #294 from jhoekx/inventory-hostname

Define 'inventory_hostname' variable for hosts.
This commit is contained in:
Michael DeHaan 2012-05-02 04:37:45 -07:00
commit df61a65344
2 changed files with 23 additions and 9 deletions

View file

@ -81,7 +81,10 @@ 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 = {} variables = {
'inventory_hostname': host,
}
if host in self._variables: if host in self._variables:
variables.update(self._variables[host].copy()) variables.update(self._variables[host].copy())

View file

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