From 8e88667f82d7aa96e0a50e2075bbf939ff400035 Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Sat, 19 May 2012 17:24:54 -0400 Subject: [PATCH] Fix hosts being in multiple groups, and duplicate host references in the same inventory file. --- lib/ansible/inventory_parser.py | 8 +++++++- test/TestInventory.py | 4 ++-- test/complex_hosts | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/ansible/inventory_parser.py b/lib/ansible/inventory_parser.py index f7387050f9f..3a086af0781 100644 --- a/lib/ansible/inventory_parser.py +++ b/lib/ansible/inventory_parser.py @@ -37,6 +37,7 @@ class InventoryParser(object): fh = open(filename) self.lines = fh.readlines() self.groups = {} + self.hosts = {} self._parse() def _parse(self): @@ -81,7 +82,12 @@ class InventoryParser(object): tokens2 = hostname.split(":") hostname = tokens2[0] port = tokens2[1] - host = Host(name=hostname, port=port) + host = None + if hostname in self.hosts: + host = self.hosts[hostname] + else: + host = Host(name=hostname, port=port) + self.hosts[hostname] = host if len(tokens) > 1: for t in tokens[1:]: (k,v) = t.split("=") diff --git a/test/TestInventory.py b/test/TestInventory.py index 6d8d3bb2a4d..a88236e75d8 100644 --- a/test/TestInventory.py +++ b/test/TestInventory.py @@ -128,9 +128,9 @@ class TestInventory(unittest.TestCase): print vars 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', - group_names=[ 'eastcoast', 'nc', 'rtp', 'us' ] + group_names=[ 'eastcoast', 'nc', 'redundantgroup', 'redundantgroup2', 'redundantgroup3', 'rtp', 'us' ] ) print vars print expected diff --git a/test/complex_hosts b/test/complex_hosts index 24bdbf1b966..18b95b207c6 100644 --- a/test/complex_hosts +++ b/test/complex_hosts @@ -12,6 +12,24 @@ florida [us:children] eastcoast +[redundantgroup] +rtp_a + +[redundantgroup2] +rtp_a + +[redundantgroup3:children] +rtp + +[redundantgroup:vars] +rga=1 + +[redundantgroup2:vars] +rgb=2 + +[redundantgroup3:vars] +rgc=3 + [nc:vars] b=10000 c=10001