mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Merge pull request #1778 from junegunn/fix-hostname-expansion
Fix hostname expansion bug in inventory parser
This commit is contained in:
commit
92365d4740
3 changed files with 32 additions and 20 deletions
|
@ -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 detect_range(hostname):
|
||||||
if hostname in self.hosts:
|
hostnames = expand_hostname_range(hostname)
|
||||||
host = self.hosts[hostname]
|
|
||||||
_all_hosts.append(host)
|
|
||||||
else:
|
else:
|
||||||
if detect_range(hostname):
|
hostnames = [hostname]
|
||||||
_hosts = expand_hostname_range(hostname)
|
|
||||||
for _ in _hosts:
|
for hn in hostnames:
|
||||||
host = Host(name=_, port=port)
|
host = None
|
||||||
self.hosts[_] = host
|
if hn in self.hosts:
|
||||||
_all_hosts.append(host)
|
host = self.hosts[hn]
|
||||||
else:
|
else:
|
||||||
host = Host(name=hostname, port=port)
|
host = Host(name=hn, port=port)
|
||||||
self.hosts[hostname] = host
|
self.hosts[hn] = host
|
||||||
_all_hosts.append(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)
|
self.groups[active_group_name].add_host(host)
|
||||||
for _ in _all_hosts:
|
|
||||||
self.groups[active_group_name].add_host(_)
|
|
||||||
|
|
||||||
# [southeast:children]
|
# [southeast:children]
|
||||||
# atlanta
|
# atlanta
|
||||||
|
|
|
@ -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")
|
||||||
|
|
|
@ -73,4 +73,9 @@ d=100002
|
||||||
[us:vars]
|
[us:vars]
|
||||||
c=1000000
|
c=1000000
|
||||||
|
|
||||||
|
[role1]
|
||||||
|
host[1:2]
|
||||||
|
|
||||||
|
[role2]
|
||||||
|
host[2:3]
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue