mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Merge pull request #4351 from 2m/leading-range-fix
Allow leading ranges in the inventory host entries.
This commit is contained in:
commit
9a7765daf7
4 changed files with 22 additions and 6 deletions
|
@ -41,8 +41,7 @@ def detect_range(line = None):
|
|||
|
||||
Returnes True if the given line contains a pattern, else False.
|
||||
'''
|
||||
if (not line.startswith("[") and
|
||||
line.find("[") != -1 and
|
||||
if (line.find("[") != -1 and
|
||||
line.find(":") != -1 and
|
||||
line.find("]") != -1 and
|
||||
line.index("[") < line.index(":") < line.index("]")):
|
||||
|
|
|
@ -65,8 +65,9 @@ class InventoryParser(object):
|
|||
active_group_name = 'ungrouped'
|
||||
|
||||
for line in self.lines:
|
||||
if line.startswith("["):
|
||||
active_group_name = line.split(" #")[0].replace("[","").replace("]","").strip()
|
||||
line = line.split("#")[0].strip()
|
||||
if line.startswith("[") and line.endswith("]"):
|
||||
active_group_name = line.replace("[","").replace("]","")
|
||||
if line.find(":vars") != -1 or line.find(":children") != -1:
|
||||
active_group_name = active_group_name.rsplit(":", 1)[0]
|
||||
if active_group_name not in self.groups:
|
||||
|
@ -76,10 +77,10 @@ class InventoryParser(object):
|
|||
elif active_group_name not in self.groups:
|
||||
new_group = self.groups[active_group_name] = Group(name=active_group_name)
|
||||
all.add_child_group(new_group)
|
||||
elif line.startswith("#") or line.startswith(";") or line == '':
|
||||
elif line.startswith(";") or line == '':
|
||||
pass
|
||||
elif active_group_name:
|
||||
tokens = shlex.split(line.split(" #")[0])
|
||||
tokens = shlex.split(line)
|
||||
if len(tokens) == 0:
|
||||
continue
|
||||
hostname = tokens[0]
|
||||
|
|
|
@ -294,6 +294,16 @@ class TestInventory(unittest.TestCase):
|
|||
expected_hosts=['host1A','host2A','host1B','host2B']
|
||||
assert sorted(hosts) == sorted(expected_hosts)
|
||||
|
||||
def test_leading_range(self):
|
||||
i = Inventory(os.path.join(self.test_dir, 'inventory','test_leading_range'))
|
||||
hosts = i.list_hosts('test')
|
||||
expected_hosts=['1.host','2.host','A.host','B.host']
|
||||
assert sorted(hosts) == sorted(expected_hosts)
|
||||
|
||||
hosts2 = i.list_hosts('test2')
|
||||
expected_hosts2=['1.host','2.host','3.host']
|
||||
assert sorted(hosts2) == sorted(expected_hosts2)
|
||||
|
||||
###################################################
|
||||
### Inventory API tests
|
||||
|
||||
|
|
6
test/inventory/test_leading_range
Normal file
6
test/inventory/test_leading_range
Normal file
|
@ -0,0 +1,6 @@
|
|||
[test]
|
||||
[1:2].host
|
||||
[A:B].host
|
||||
|
||||
[test2] # comment
|
||||
[1:3].host
|
Loading…
Reference in a new issue