From 7fbb7e079a839cb811709cdfd12235b9169849fa Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Sun, 16 Dec 2012 01:49:12 +0900 Subject: [PATCH] Fix hostname expansion bug in inventory parser --- lib/ansible/inventory/ini.py | 36 ++++++++++++++++-------------------- test/TestInventory.py | 11 +++++++++++ test/complex_hosts | 5 +++++ 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/lib/ansible/inventory/ini.py b/lib/ansible/inventory/ini.py index 4b1607d8844..e570dee9be8 100644 --- a/lib/ansible/inventory/ini.py +++ b/lib/ansible/inventory/ini.py @@ -93,28 +93,24 @@ class InventoryParser(object): hostname = tokens2[0] port = tokens2[1] - host = None - _all_hosts = [] - if hostname in self.hosts: - host = self.hosts[hostname] - _all_hosts.append(host) + hostnames = [] + if detect_range(hostname): + hostnames = expand_hostname_range(hostname) else: - if detect_range(hostname): - _hosts = expand_hostname_range(hostname) - for _ in _hosts: - host = Host(name=_, port=port) - self.hosts[_] = host - _all_hosts.append(host) + hostnames = [hostname] + + for hn in hostnames: + host = None + if hn in self.hosts: + host = self.hosts[hn] else: - host = Host(name=hostname, port=port) - self.hosts[hostname] = host - _all_hosts.append(host) - if len(tokens) > 1: - for t in tokens[1:]: - (k,v) = t.split("=") - host.set_variable(k,v) - for _ in _all_hosts: - self.groups[active_group_name].add_host(_) + host = Host(name=hn, port=port) + self.hosts[hn] = host + if len(tokens) > 1: + for t in tokens[1:]: + (k,v) = t.split("=") + host.set_variable(k,v) + self.groups[active_group_name].add_host(host) # [southeast:children] # atlanta diff --git a/test/TestInventory.py b/test/TestInventory.py index a089aa1a749..50a4c3a728d 100644 --- a/test/TestInventory.py +++ b/test/TestInventory.py @@ -150,6 +150,17 @@ class TestInventory(unittest.TestCase): print 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): inventory = self.complex_inventory() hosts = inventory.list_hosts("nc:florida:!triangle:!orlando") diff --git a/test/complex_hosts b/test/complex_hosts index 8c2c726f3be..c6e5e98cf0b 100644 --- a/test/complex_hosts +++ b/test/complex_hosts @@ -73,4 +73,9 @@ d=100002 [us:vars] c=1000000 +[role1] +host[1:2] + +[role2] +host[2:3]