Export SSH port number as host variable.

pull/146/head
Jeroen Hoekx 13 years ago
parent f04041b37d
commit 54f4526160

@ -35,6 +35,7 @@ class Inventory(object):
def __init__(self, host_list=C.DEFAULT_HOST_LIST, extra_vars=None): def __init__(self, host_list=C.DEFAULT_HOST_LIST, extra_vars=None):
self._restriction = None self._restriction = None
self._variables = {}
if type(host_list) == list: if type(host_list) == list:
self.host_list = host_list self.host_list = host_list
@ -80,6 +81,9 @@ class Inventory(object):
def get_variables(self, host, extra_vars=None): def get_variables(self, host, extra_vars=None):
""" Return the variables associated with this host. """ """ Return the variables associated with this host. """
if host in self._variables:
return self._variables[host]
if not self._is_script: if not self._is_script:
return {} return {}
@ -108,6 +112,11 @@ class Inventory(object):
if ":" in item: if ":" in item:
# a port was specified # a port was specified
item, port = item.split(":") item, port = item.split(":")
try:
port = int(port)
except ValueError:
raise errors.AnsibleError("SSH port for %s in inventory (%s) should be numerical."%(item, port))
self._set_variable(item, "ansible_ssh_port", port)
groups[group_name].append(item) groups[group_name].append(item)
if not item in results: if not item in results:
results.append(item) results.append(item)
@ -170,6 +179,11 @@ class Inventory(object):
)) ))
return variables return variables
def _set_variable(self, host, key, value):
if not host in self._variables:
self._variables[host] = {}
self._variables[host][key] = value
def _matches(self, host_name, pattern): def _matches(self, host_name, pattern):
''' returns if a hostname is matched by the pattern ''' ''' returns if a hostname is matched by the pattern '''

@ -82,6 +82,12 @@ class TestInventory(unittest.TestCase):
assert vars == {} assert vars == {}
def test_simple_port(self):
inventory = self.simple_inventory()
vars = inventory.get_variables('hera')
assert vars == {'ansible_ssh_port': 3000}
### Inventory API tests ### Inventory API tests
def test_script(self): def test_script(self):

Loading…
Cancel
Save