Skip to content

Commit

Permalink
Merge pull request ansible#9672 from quinot/topic/lineno-in-inventory…
Browse files Browse the repository at this point in the history
…-err

Report location (filename and line number) for inventory syntax errors
  • Loading branch information
bcoca committed Jan 29, 2015
2 parents 171a67c + 23d959d commit 751701c
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions lib/ansible/inventory/ini.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class InventoryParser(object):
def __init__(self, filename=C.DEFAULT_HOST_LIST):

with open(filename) as fh:
self.filename = filename
self.lines = fh.readlines()
self.groups = {}
self.hosts = {}
Expand Down Expand Up @@ -87,8 +88,8 @@ def _parse_base_groups(self):
self.groups = dict(all=all, ungrouped=ungrouped)
active_group_name = 'ungrouped'

for line in self.lines:
line = utils.before_comment(line).strip()
for lineno in range(len(self.lines)):
line = utils.before_comment(self.lines[lineno]).strip()
if line.startswith("[") and line.endswith("]"):
active_group_name = line.replace("[","").replace("]","")
if ":vars" in line or ":children" in line:
Expand Down Expand Up @@ -142,7 +143,7 @@ def _parse_base_groups(self):
try:
(k,v) = t.split("=", 1)
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))
self.groups[active_group_name].add_host(host)

Expand All @@ -153,8 +154,8 @@ def _parse_base_groups(self):
def _parse_group_children(self):
group = None

for line in self.lines:
line = line.strip()
for lineno in range(len(self.lines)):
line = self.lines[lineno].strip()
if line is None or line == '':
continue
if line.startswith("[") and ":children]" in line:
Expand All @@ -169,7 +170,7 @@ def _parse_group_children(self):
elif group:
kid_group = self.groups.get(line, 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:
group.add_child_group(kid_group)

Expand All @@ -180,13 +181,13 @@ def _parse_group_children(self):

def _parse_group_variables(self):
group = None
for line in self.lines:
line = line.strip()
for lineno in range(len(self.lines)):
line = self.lines[lineno].strip()
if line.startswith("[") and ":vars]" in line:
line = line.replace("[","").replace(":vars]","")
group = self.groups.get(line, 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(";"):
pass
elif line.startswith("["):
Expand All @@ -195,7 +196,7 @@ def _parse_group_variables(self):
pass
elif group:
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:
(k, v) = [e.strip() for e in line.split("=", 1)]
group.set_variable(k, self._parse_value(v))
Expand Down

0 comments on commit 751701c

Please sign in to comment.