mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-27 23:21:22 -07:00
Handle comment on inventory group line.
This commit is contained in:
parent
44279ce34f
commit
7ecb5fbc9c
3 changed files with 12 additions and 4 deletions
|
@ -65,8 +65,9 @@ class InventoryParser(object):
|
||||||
active_group_name = 'ungrouped'
|
active_group_name = 'ungrouped'
|
||||||
|
|
||||||
for line in self.lines:
|
for line in self.lines:
|
||||||
if line.startswith("[") and line.strip().endswith("]"):
|
line = line.split("#")[0].strip()
|
||||||
active_group_name = line.split(" #")[0].replace("[","").replace("]","").strip()
|
if line.startswith("[") and line.endswith("]"):
|
||||||
|
active_group_name = line.replace("[","").replace("]","")
|
||||||
if line.find(":vars") != -1 or line.find(":children") != -1:
|
if line.find(":vars") != -1 or line.find(":children") != -1:
|
||||||
active_group_name = active_group_name.rsplit(":", 1)[0]
|
active_group_name = active_group_name.rsplit(":", 1)[0]
|
||||||
if active_group_name not in self.groups:
|
if active_group_name not in self.groups:
|
||||||
|
@ -76,10 +77,10 @@ class InventoryParser(object):
|
||||||
elif active_group_name not in self.groups:
|
elif active_group_name not in self.groups:
|
||||||
new_group = self.groups[active_group_name] = Group(name=active_group_name)
|
new_group = self.groups[active_group_name] = Group(name=active_group_name)
|
||||||
all.add_child_group(new_group)
|
all.add_child_group(new_group)
|
||||||
elif line.startswith("#") or line.startswith(";") or line == '':
|
elif line.startswith(";") or line == '':
|
||||||
pass
|
pass
|
||||||
elif active_group_name:
|
elif active_group_name:
|
||||||
tokens = shlex.split(line.split(" #")[0])
|
tokens = shlex.split(line)
|
||||||
if len(tokens) == 0:
|
if len(tokens) == 0:
|
||||||
continue
|
continue
|
||||||
hostname = tokens[0]
|
hostname = tokens[0]
|
||||||
|
|
|
@ -300,6 +300,10 @@ class TestInventory(unittest.TestCase):
|
||||||
expected_hosts=['1.host','2.host','A.host','B.host']
|
expected_hosts=['1.host','2.host','A.host','B.host']
|
||||||
assert sorted(hosts) == sorted(expected_hosts)
|
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
|
### Inventory API tests
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
[test]
|
[test]
|
||||||
[1:2].host
|
[1:2].host
|
||||||
[A:B].host
|
[A:B].host
|
||||||
|
|
||||||
|
[test2] # comment
|
||||||
|
[1:3].host
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue