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

Fix hosts being in multiple groups, and duplicate host references in the same inventory file.

This commit is contained in:
Michael DeHaan 2012-05-19 17:24:54 -04:00
parent 7302b7bbc4
commit 8e88667f82
3 changed files with 27 additions and 3 deletions

View file

@ -37,6 +37,7 @@ class InventoryParser(object):
fh = open(filename) fh = open(filename)
self.lines = fh.readlines() self.lines = fh.readlines()
self.groups = {} self.groups = {}
self.hosts = {}
self._parse() self._parse()
def _parse(self): def _parse(self):
@ -81,7 +82,12 @@ class InventoryParser(object):
tokens2 = hostname.split(":") tokens2 = hostname.split(":")
hostname = tokens2[0] hostname = tokens2[0]
port = tokens2[1] port = tokens2[1]
host = None
if hostname in self.hosts:
host = self.hosts[hostname]
else:
host = Host(name=hostname, port=port) host = Host(name=hostname, port=port)
self.hosts[hostname] = 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("=")

View file

@ -128,9 +128,9 @@ class TestInventory(unittest.TestCase):
print vars print vars
expected = dict( expected = dict(
a='1', b='2', c='3', d='100002', a='1', b='2', c='3', d='100002', rga='1', rgb='2', rgc='3',
inventory_hostname='rtp_a', inventory_hostname='rtp_a',
group_names=[ 'eastcoast', 'nc', 'rtp', 'us' ] group_names=[ 'eastcoast', 'nc', 'redundantgroup', 'redundantgroup2', 'redundantgroup3', 'rtp', 'us' ]
) )
print vars print vars
print expected print expected

View file

@ -12,6 +12,24 @@ florida
[us:children] [us:children]
eastcoast eastcoast
[redundantgroup]
rtp_a
[redundantgroup2]
rtp_a
[redundantgroup3:children]
rtp
[redundantgroup:vars]
rga=1
[redundantgroup2:vars]
rgb=2
[redundantgroup3:vars]
rgc=3
[nc:vars] [nc:vars]
b=10000 b=10000
c=10001 c=10001