From d4dec5f577b7bda8104eea838fafaedb93aaf735 Mon Sep 17 00:00:00 2001 From: Jasper Capel Date: Wed, 25 Sep 2013 14:59:11 +0200 Subject: [PATCH] Allow creating empty inventory Instantiating the Inventory class with host_list=None now results in an empty inventory instead of an error. --- lib/ansible/inventory/__init__.py | 4 +++- test/TestInventory.py | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py index 3a8bce6d776..16d11246ffa 100644 --- a/lib/ansible/inventory/__init__.py +++ b/lib/ansible/inventory/__init__.py @@ -70,7 +70,9 @@ class Inventory(object): host_list = host_list.split(",") host_list = [ h for h in host_list if h and h.strip() ] - if isinstance(host_list, list): + if host_list is None: + self.parser = None + elif isinstance(host_list, list): self.parser = None all = Group('all') self.groups = [ all ] diff --git a/test/TestInventory.py b/test/TestInventory.py index 3a1cae1e4f6..8194ce3e4e6 100644 --- a/test/TestInventory.py +++ b/test/TestInventory.py @@ -30,6 +30,9 @@ class TestInventory(unittest.TestCase): print right assert left == right + def empty_inventory(self): + return Inventory(None) + def simple_inventory(self): return Inventory(self.inventory_file) @@ -54,6 +57,14 @@ class TestInventory(unittest.TestCase): 'Hotep-a', 'Hotep-b', 'Hotep-c', 'BastC', 'BastD', ] + ##################################### + ### Empty inventory format tests + + def test_empty(self): + inventory = self.empty_inventory() + hosts = inventory.list_hosts() + self.assertEqual(hosts, []) + ##################################### ### Simple inventory format tests