From b14932465d768e634e500961434e6f05a033f2c8 Mon Sep 17 00:00:00 2001 From: Jesse Keating Date: Mon, 3 Mar 2014 13:23:27 -0800 Subject: [PATCH] Avoid range selection on empty groups This prevents a traceback when the group is empty. Fixes #6258 --- lib/ansible/inventory/__init__.py | 6 ++++++ test/units/TestInventory.py | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py index 67117919d06..8f74d5ea9e9 100644 --- a/lib/ansible/inventory/__init__.py +++ b/lib/ansible/inventory/__init__.py @@ -227,6 +227,12 @@ class Inventory(object): given a pattern like foo[0:5], where foo matches hosts, return the first 6 hosts """ + # If there are no hosts to select from, just return the + # empty set. This prevents trying to do selections on an empty set. + # issue#6258 + if not hosts: + return hosts + (loose_pattern, limits) = self._enumeration_info(pat) if not limits: return hosts diff --git a/test/units/TestInventory.py b/test/units/TestInventory.py index 2554d432041..2ae6256e62b 100644 --- a/test/units/TestInventory.py +++ b/test/units/TestInventory.py @@ -212,6 +212,11 @@ class TestInventory(unittest.TestCase): inventory.subset('greek[0-2];norse[0]') self.assertEqual(sorted(inventory.list_hosts()), sorted(['zeus','hera','thor'])) + def test_subet_range_empty_group(self): + inventory = self.simple_inventory() + inventory.subset('missing[0]') + self.assertEqual(sorted(inventory.list_hosts()), sorted([])) + def test_subset_filename(self): inventory = self.simple_inventory() inventory.subset('@' + os.path.join(self.test_dir, 'restrict_pattern'))