From 141349629289024fea108b73c9aed2e8c82066dc Mon Sep 17 00:00:00 2001 From: Abhijit Menon-Sen Date: Fri, 31 Jul 2015 12:09:18 +0530 Subject: [PATCH 1/4] Implement step for alphabetic ranges: [a:e:2] => a,c,e --- lib/ansible/inventory/expand_hosts.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/ansible/inventory/expand_hosts.py b/lib/ansible/inventory/expand_hosts.py index b5a957c53fe..d2b827ac750 100644 --- a/lib/ansible/inventory/expand_hosts.py +++ b/lib/ansible/inventory/expand_hosts.py @@ -75,7 +75,6 @@ def expand_hostname_range(line = None): # of hosts and then repeat until none left. # - also add an optional third parameter which contains the step. (Default: 1) # so range can be [01:10:2] -> 01 03 05 07 09 - # FIXME: make this work for alphabetic sequences too. (head, nrange, tail) = line.replace('[','|',1).replace(']','|',1).split('|') bounds = nrange.split(":") @@ -104,7 +103,7 @@ def expand_hostname_range(line = None): i_end = string.ascii_letters.index(end) if i_beg > i_end: raise errors.AnsibleError("host range format incorrectly specified!") - seq = string.ascii_letters[i_beg:i_end+1] + seq = [string.ascii_letters[i] for i in range(i_beg, i_end+1, int(step))] except ValueError: # not an alpha range seq = range(int(beg), int(end)+1, int(step)) From bfe708a1899fe927303a21246929263280d84398 Mon Sep 17 00:00:00 2001 From: Abhijit Menon-Sen Date: Fri, 31 Jul 2015 12:18:11 +0530 Subject: [PATCH 2/4] Make host range parsing errors issue better messages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Now we always say "host range must …specific thing…" --- lib/ansible/inventory/expand_hosts.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/ansible/inventory/expand_hosts.py b/lib/ansible/inventory/expand_hosts.py index d2b827ac750..71f6a6536cf 100644 --- a/lib/ansible/inventory/expand_hosts.py +++ b/lib/ansible/inventory/expand_hosts.py @@ -79,7 +79,7 @@ def expand_hostname_range(line = None): (head, nrange, tail) = line.replace('[','|',1).replace(']','|',1).split('|') bounds = nrange.split(":") if len(bounds) != 2 and len(bounds) != 3: - raise errors.AnsibleError("host range incorrectly specified") + raise errors.AnsibleError("host range must be begin:end or begin:end:step") beg = bounds[0] end = bounds[1] if len(bounds) == 2: @@ -89,11 +89,11 @@ def expand_hostname_range(line = None): if not beg: beg = "0" if not end: - raise errors.AnsibleError("host range end value missing") + raise errors.AnsibleError("host range must specify end value") if beg[0] == '0' and len(beg) > 1: rlen = len(beg) # range length formatting hint if rlen != len(end): - raise errors.AnsibleError("host range format incorrectly specified!") + raise errors.AnsibleError("host range must specify equal-length begin and end formats") fill = lambda _: str(_).zfill(rlen) # range sequence else: fill = str @@ -102,7 +102,7 @@ def expand_hostname_range(line = None): i_beg = string.ascii_letters.index(beg) i_end = string.ascii_letters.index(end) if i_beg > i_end: - raise errors.AnsibleError("host range format incorrectly specified!") + raise errors.AnsibleError("host range must have begin <= end") seq = [string.ascii_letters[i] for i in range(i_beg, i_end+1, int(step))] except ValueError: # not an alpha range seq = range(int(beg), int(end)+1, int(step)) From 5f13d40a2b4a8bac601464cde3ae3d8694e8a7cd Mon Sep 17 00:00:00 2001 From: Abhijit Menon-Sen Date: Fri, 31 Jul 2015 12:19:25 +0530 Subject: [PATCH 3/4] Mention x:y ranges in pattern documentation --- docsite/rst/intro_patterns.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docsite/rst/intro_patterns.rst b/docsite/rst/intro_patterns.rst index 579276a3af7..07160b21828 100644 --- a/docsite/rst/intro_patterns.rst +++ b/docsite/rst/intro_patterns.rst @@ -72,9 +72,9 @@ As an advanced usage, you can also select the numbered server in a group:: webservers[0] -Or a portion of servers in a group:: +Or a range of servers in a group:: - webservers[0-25] + webservers[0:25] Most people don't specify patterns as regular expressions, but you can. Just start the pattern with a '~':: From 7d9689c1c5223020c6f082e67aab3b2072ed955e Mon Sep 17 00:00:00 2001 From: Abhijit Menon-Sen Date: Fri, 31 Jul 2015 12:36:48 +0530 Subject: [PATCH 4/4] Slice ascii_letters directly instead of using 'for x in range()' --- lib/ansible/inventory/expand_hosts.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ansible/inventory/expand_hosts.py b/lib/ansible/inventory/expand_hosts.py index 71f6a6536cf..0d63ba08bbc 100644 --- a/lib/ansible/inventory/expand_hosts.py +++ b/lib/ansible/inventory/expand_hosts.py @@ -103,7 +103,7 @@ def expand_hostname_range(line = None): i_end = string.ascii_letters.index(end) if i_beg > i_end: raise errors.AnsibleError("host range must have begin <= end") - seq = [string.ascii_letters[i] for i in range(i_beg, i_end+1, int(step))] + seq = list(string.ascii_letters[i_beg:i_end+1:int(step)]) except ValueError: # not an alpha range seq = range(int(beg), int(end)+1, int(step))