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

capture parsing exceptions and give nice error and location

This commit is contained in:
Brian Coca 2015-08-12 23:27:00 -04:00
parent 621b18c6ef
commit 5fec11f513

View file

@ -38,6 +38,7 @@ class InventoryParser(object):
def __init__(self, filename=C.DEFAULT_HOST_LIST):
self.filename = filename
with open(filename) as fh:
self.lines = fh.readlines()
self.groups = {}
@ -90,7 +91,9 @@ class InventoryParser(object):
self.groups = dict(all=all, ungrouped=ungrouped)
active_group_name = 'ungrouped'
i = 0
for line in self.lines:
i += 1
line = self._before_comment(line).strip()
if line.startswith("[") and line.endswith("]"):
active_group_name = line.replace("[","").replace("]","")
@ -104,7 +107,11 @@ class InventoryParser(object):
elif line.startswith(";") or line == '':
pass
elif active_group_name:
try:
tokens = shlex.split(line)
except ValueError as e:
raise AnsibleError("Error in %s, unable to parse L#%d: %s\n\n\t%s\n" % (self.filename, i, str(e), line))
if len(tokens) == 0:
continue
hostname = tokens[0]