From 2fcdb37e7bbf9dd01bc8e10d30a8ae673b94638a Mon Sep 17 00:00:00 2001 From: Abhijit Menon-Sen Date: Fri, 18 Sep 2015 22:28:34 +0530 Subject: [PATCH] =?UTF-8?q?Support=20=C2=ABhosts:=20groupname[1:]=C2=BB=20?= =?UTF-8?q?notation=20(~=3D=20'the=20rest=20of=20the=20group')?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docsite/rst/intro_patterns.rst | 5 +++-- lib/ansible/inventory/__init__.py | 10 +++++++--- test/units/inventory/test_inventory.py | 3 +++ 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/docsite/rst/intro_patterns.rst b/docsite/rst/intro_patterns.rst index 9238d92869..67d1302ffc 100644 --- a/docsite/rst/intro_patterns.rst +++ b/docsite/rst/intro_patterns.rst @@ -79,8 +79,9 @@ You can refer to hosts within the group by adding a subscript to the group name: webservers[0] # == cobweb webservers[-1] # == weber - webservers[0:1] # == webservers[0]:webservers[1] - # == cobweb:webbing + webservers[0:1] # == webservers[0],webservers[1] + # == cobweb,webbing + webservers[1:] # == webbing,weber Most people don't specify patterns as regular expressions, but you can. Just start the pattern with a '~':: diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py index 99e6729b89..cd787e6876 100644 --- a/lib/ansible/inventory/__init__.py +++ b/lib/ansible/inventory/__init__.py @@ -342,9 +342,9 @@ class Inventory(object): r'''^ (.+) # A pattern expression ending with... \[(?: # A [subscript] expression comprising: - (-?[0-9]+) # A single positive or negative number - | # Or a numeric range - ([0-9]+)([:-])([0-9]+) + (-?[0-9]+)| # A single positive or negative number + ([0-9]+)([:-]) # Or an x:y or x: range. + ([0-9]*) )\] $ ''', re.X @@ -357,6 +357,8 @@ class Inventory(object): if idx: subscript = (int(idx), None) else: + if not end: + end = -1 subscript = (int(start), int(end)) if sep == '-': display.deprecated("Use [x:y] inclusive subscripts instead of [x-y]", version=2.0, removed=True) @@ -375,6 +377,8 @@ class Inventory(object): (start, end) = subscript if end: + if end == -1: + end = len(hosts)-1 return hosts[start:end+1] else: return [ hosts[start] ] diff --git a/test/units/inventory/test_inventory.py b/test/units/inventory/test_inventory.py index 72c593db35..e7bcceb85d 100644 --- a/test/units/inventory/test_inventory.py +++ b/test/units/inventory/test_inventory.py @@ -66,6 +66,9 @@ class TestInventory(unittest.TestCase): 'a[2:3]': [('a', (2, 3)), ['c', 'd']], 'a[-1]': [('a', (-1, None)), ['Z']], 'a[-2]': [('a', (-2, None)), ['Y']], + 'a[48:]': [('a', (48, -1)), ['W', 'X', 'Y', 'Z']], + 'a[49:]': [('a', (49, -1)), ['X', 'Y', 'Z']], + 'a[1:]': [('a', (1, -1)), list(string.ascii_letters[1:])], } def setUp(self):