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): def __init__(self, filename=C.DEFAULT_HOST_LIST):
self.filename = filename self.filename = filename
with open(filename) as fh: with open(filename) as fh:
self.lines = fh.readlines() self.lines = fh.readlines()
self.groups = {} self.groups = {}
@ -90,7 +91,9 @@ class InventoryParser(object):
self.groups = dict(all=all, ungrouped=ungrouped) self.groups = dict(all=all, ungrouped=ungrouped)
active_group_name = 'ungrouped' active_group_name = 'ungrouped'
i = 0
for line in self.lines: for line in self.lines:
i += 1
line = self._before_comment(line).strip() line = self._before_comment(line).strip()
if line.startswith("[") and line.endswith("]"): if line.startswith("[") and line.endswith("]"):
active_group_name = line.replace("[","").replace("]","") active_group_name = line.replace("[","").replace("]","")
@ -104,7 +107,11 @@ class InventoryParser(object):
elif line.startswith(";") or line == '': elif line.startswith(";") or line == '':
pass pass
elif active_group_name: elif active_group_name:
tokens = shlex.split(line) 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: if len(tokens) == 0:
continue continue
hostname = tokens[0] hostname = tokens[0]