1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Support «hosts: groupname[1:]» notation (~= 'the rest of the group')

This commit is contained in:
Abhijit Menon-Sen 2015-09-18 22:28:34 +05:30
parent 21142f5723
commit 2fcdb37e7b
3 changed files with 13 additions and 5 deletions

View file

@ -79,8 +79,9 @@ You can refer to hosts within the group by adding a subscript to the group name:
webservers[0] # == cobweb webservers[0] # == cobweb
webservers[-1] # == weber webservers[-1] # == weber
webservers[0:1] # == webservers[0]:webservers[1] webservers[0:1] # == webservers[0],webservers[1]
# == cobweb:webbing # == cobweb,webbing
webservers[1:] # == webbing,weber
Most people don't specify patterns as regular expressions, but you can. Just start the pattern with a '~':: Most people don't specify patterns as regular expressions, but you can. Just start the pattern with a '~'::

View file

@ -342,9 +342,9 @@ class Inventory(object):
r'''^ r'''^
(.+) # A pattern expression ending with... (.+) # A pattern expression ending with...
\[(?: # A [subscript] expression comprising: \[(?: # A [subscript] expression comprising:
(-?[0-9]+) # A single positive or negative number (-?[0-9]+)| # A single positive or negative number
| # Or a numeric range ([0-9]+)([:-]) # Or an x:y or x: range.
([0-9]+)([:-])([0-9]+) ([0-9]*)
)\] )\]
$ $
''', re.X ''', re.X
@ -357,6 +357,8 @@ class Inventory(object):
if idx: if idx:
subscript = (int(idx), None) subscript = (int(idx), None)
else: else:
if not end:
end = -1
subscript = (int(start), int(end)) subscript = (int(start), int(end))
if sep == '-': if sep == '-':
display.deprecated("Use [x:y] inclusive subscripts instead of [x-y]", version=2.0, removed=True) 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 (start, end) = subscript
if end: if end:
if end == -1:
end = len(hosts)-1
return hosts[start:end+1] return hosts[start:end+1]
else: else:
return [ hosts[start] ] return [ hosts[start] ]

View file

@ -66,6 +66,9 @@ class TestInventory(unittest.TestCase):
'a[2:3]': [('a', (2, 3)), ['c', 'd']], 'a[2:3]': [('a', (2, 3)), ['c', 'd']],
'a[-1]': [('a', (-1, None)), ['Z']], 'a[-1]': [('a', (-1, None)), ['Z']],
'a[-2]': [('a', (-2, None)), ['Y']], '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): def setUp(self):