Merge pull request #9672 from quinot/topic/lineno-in-inventory-err
Report location (filename and line number) for inventory syntax errors
This commit is contained in:
commit
751701c6f2
1 changed files with 11 additions and 10 deletions
|
@ -36,6 +36,7 @@ class InventoryParser(object):
|
||||||
def __init__(self, filename=C.DEFAULT_HOST_LIST):
|
def __init__(self, filename=C.DEFAULT_HOST_LIST):
|
||||||
|
|
||||||
with open(filename) as fh:
|
with open(filename) as fh:
|
||||||
|
self.filename = filename
|
||||||
self.lines = fh.readlines()
|
self.lines = fh.readlines()
|
||||||
self.groups = {}
|
self.groups = {}
|
||||||
self.hosts = {}
|
self.hosts = {}
|
||||||
|
@ -87,8 +88,8 @@ 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'
|
||||||
|
|
||||||
for line in self.lines:
|
for lineno in range(len(self.lines)):
|
||||||
line = utils.before_comment(line).strip()
|
line = utils.before_comment(self.lines[lineno]).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("]","")
|
||||||
if ":vars" in line or ":children" in line:
|
if ":vars" in line or ":children" in line:
|
||||||
|
@ -142,7 +143,7 @@ class InventoryParser(object):
|
||||||
try:
|
try:
|
||||||
(k,v) = t.split("=", 1)
|
(k,v) = t.split("=", 1)
|
||||||
except ValueError, e:
|
except ValueError, e:
|
||||||
raise errors.AnsibleError("Invalid ini entry: %s - %s" % (t, str(e)))
|
raise errors.AnsibleError("%s:%s: Invalid ini entry: %s - %s" % (self.filename, lineno + 1, t, str(e)))
|
||||||
host.set_variable(k, self._parse_value(v))
|
host.set_variable(k, self._parse_value(v))
|
||||||
self.groups[active_group_name].add_host(host)
|
self.groups[active_group_name].add_host(host)
|
||||||
|
|
||||||
|
@ -153,8 +154,8 @@ class InventoryParser(object):
|
||||||
def _parse_group_children(self):
|
def _parse_group_children(self):
|
||||||
group = None
|
group = None
|
||||||
|
|
||||||
for line in self.lines:
|
for lineno in range(len(self.lines)):
|
||||||
line = line.strip()
|
line = self.lines[lineno].strip()
|
||||||
if line is None or line == '':
|
if line is None or line == '':
|
||||||
continue
|
continue
|
||||||
if line.startswith("[") and ":children]" in line:
|
if line.startswith("[") and ":children]" in line:
|
||||||
|
@ -169,7 +170,7 @@ class InventoryParser(object):
|
||||||
elif group:
|
elif group:
|
||||||
kid_group = self.groups.get(line, None)
|
kid_group = self.groups.get(line, None)
|
||||||
if kid_group is None:
|
if kid_group is None:
|
||||||
raise errors.AnsibleError("child group is not defined: (%s)" % line)
|
raise errors.AnsibleError("%s:%d: child group is not defined: (%s)" % (self.filename, lineno + 1, line))
|
||||||
else:
|
else:
|
||||||
group.add_child_group(kid_group)
|
group.add_child_group(kid_group)
|
||||||
|
|
||||||
|
@ -180,13 +181,13 @@ class InventoryParser(object):
|
||||||
|
|
||||||
def _parse_group_variables(self):
|
def _parse_group_variables(self):
|
||||||
group = None
|
group = None
|
||||||
for line in self.lines:
|
for lineno in range(len(self.lines)):
|
||||||
line = line.strip()
|
line = self.lines[lineno].strip()
|
||||||
if line.startswith("[") and ":vars]" in line:
|
if line.startswith("[") and ":vars]" in line:
|
||||||
line = line.replace("[","").replace(":vars]","")
|
line = line.replace("[","").replace(":vars]","")
|
||||||
group = self.groups.get(line, None)
|
group = self.groups.get(line, None)
|
||||||
if group is None:
|
if group is None:
|
||||||
raise errors.AnsibleError("can't add vars to undefined group: %s" % line)
|
raise errors.AnsibleError("%s:%d: can't add vars to undefined group: %s" % (self.filename, lineno + 1, line))
|
||||||
elif line.startswith("#") or line.startswith(";"):
|
elif line.startswith("#") or line.startswith(";"):
|
||||||
pass
|
pass
|
||||||
elif line.startswith("["):
|
elif line.startswith("["):
|
||||||
|
@ -195,7 +196,7 @@ class InventoryParser(object):
|
||||||
pass
|
pass
|
||||||
elif group:
|
elif group:
|
||||||
if "=" not in line:
|
if "=" not in line:
|
||||||
raise errors.AnsibleError("variables assigned to group must be in key=value form")
|
raise errors.AnsibleError("%s:%d: variables assigned to group must be in key=value form" % (self.filename, lineno + 1))
|
||||||
else:
|
else:
|
||||||
(k, v) = [e.strip() for e in line.split("=", 1)]
|
(k, v) = [e.strip() for e in line.split("=", 1)]
|
||||||
group.set_variable(k, self._parse_value(v))
|
group.set_variable(k, self._parse_value(v))
|
||||||
|
|
Loading…
Reference in a new issue