Merge pull request #1778 from junegunn/fix-hostname-expansion

Fix hostname expansion bug in inventory parser
This commit is contained in:
Daniel Hokka Zakrisson 2012-12-15 09:20:35 -08:00
commit 92365d4740
3 changed files with 32 additions and 20 deletions

View file

@ -93,28 +93,24 @@ class InventoryParser(object):
hostname = tokens2[0] hostname = tokens2[0]
port = tokens2[1] port = tokens2[1]
host = None hostnames = []
_all_hosts = []
if hostname in self.hosts:
host = self.hosts[hostname]
_all_hosts.append(host)
else:
if detect_range(hostname): if detect_range(hostname):
_hosts = expand_hostname_range(hostname) hostnames = expand_hostname_range(hostname)
for _ in _hosts:
host = Host(name=_, port=port)
self.hosts[_] = host
_all_hosts.append(host)
else: else:
host = Host(name=hostname, port=port) hostnames = [hostname]
self.hosts[hostname] = host
_all_hosts.append(host) for hn in hostnames:
host = None
if hn in self.hosts:
host = self.hosts[hn]
else:
host = Host(name=hn, port=port)
self.hosts[hn] = host
if len(tokens) > 1: if len(tokens) > 1:
for t in tokens[1:]: for t in tokens[1:]:
(k,v) = t.split("=") (k,v) = t.split("=")
host.set_variable(k,v) host.set_variable(k,v)
for _ in _all_hosts: self.groups[active_group_name].add_host(host)
self.groups[active_group_name].add_host(_)
# [southeast:children] # [southeast:children]
# atlanta # atlanta

View file

@ -150,6 +150,17 @@ class TestInventory(unittest.TestCase):
print expected print expected
assert vars == expected assert vars == expected
def test_complex_group_names(self):
inventory = self.complex_inventory()
tests = {
'host1': [ 'role1' ],
'host2': [ 'role1', 'role2' ],
'host3': [ 'role2' ]
}
for host, roles in tests.iteritems():
group_names = inventory.get_variables(host)['group_names']
assert sorted(group_names) == sorted(roles)
def test_complex_exclude(self): def test_complex_exclude(self):
inventory = self.complex_inventory() inventory = self.complex_inventory()
hosts = inventory.list_hosts("nc:florida:!triangle:!orlando") hosts = inventory.list_hosts("nc:florida:!triangle:!orlando")

View file

@ -73,4 +73,9 @@ d=100002
[us:vars] [us:vars]
c=1000000 c=1000000
[role1]
host[1:2]
[role2]
host[2:3]