Skip to content

Commit

Permalink
make vars only group declarations an error
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoca committed Nov 21, 2017
1 parent e45c763 commit 3456bba
Showing 1 changed file with 17 additions and 25 deletions.
42 changes: 17 additions & 25 deletions lib/ansible/plugins/inventory/ini.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ def _parse(self, path, lines):
pending_declarations = {}
groupname = 'ungrouped'
state = 'hosts'

self.lineno = 0
for line in lines:
self.lineno += 1
Expand All @@ -176,24 +175,21 @@ def _parse(self, path, lines):
self._raise_error("Section [%s] has unknown type: %s" % (title, state))

# If we haven't seen this group before, we add a new Group.
#
# Either [groupname] or [groupname:children] is sufficient to
# declare a group, but [groupname:vars] is allowed only if the
# group is declared elsewhere (not necessarily earlier). We add
# the group anyway, but make a note in pending_declarations to
# check at the end.

self.inventory.add_group(groupname)

if state == 'vars':
pending_declarations[groupname] = dict(line=self.lineno, state=state, name=groupname)
if groupname not in self.inventory.groups:
# Either [groupname] or [groupname:children] is sufficient to declare a group,
# but [groupname:vars] is allowed only if the # group is declared elsewhere.
# We add the group anyway, but make a note in pending_declarations to check at the end.
if state == 'vars':
pending_declarations[groupname] = dict(line=self.lineno, state=state, name=groupname)

# When we see a declaration that we've been waiting for, we can
# delete the note.
self.inventory.add_group(groupname)

# When we see a declaration that we've been waiting for, we process and delete.
if groupname in pending_declarations and state != 'vars':
if pending_declarations[groupname]['state'] == 'children':
self._add_pending_children(groupname, pending_declarations)
elif pending_declarations[groupname]['state'] == 'vars':
del pending_declarations[groupname]

continue
elif line.startswith('[') and line.endswith(']'):
Expand Down Expand Up @@ -229,22 +225,18 @@ def _parse(self, path, lines):
pending_declarations[child]['parents'].append(groupname)
else:
self.inventory.add_child(groupname, child)

# This is a fencepost. It can happen only if the state checker
# accepts a state that isn't handled above.
else:
# This can happen only if the state checker accepts a state that isn't handled above.
self._raise_error("Entered unhandled state: %s" % (state))

# Any entries in pending_declarations not removed by a group declaration above mean that there was an unresolved reference.
# We report only the first such error here.

for g in pending_declarations:
if g not in self.inventory.groups:
decl = pending_declarations[g]
if decl['state'] == 'vars':
raise AnsibleError("%s:%d: Section [%s:vars] not valid for undefined group: %s" % (path, decl['line'], decl['name'], decl['name']))
elif decl['state'] == 'children':
raise AnsibleError("%s:%d: Section [%s:children] includes undefined group: %s" % (path, decl['line'], decl['parents'].pop(), decl['name']))
decl = pending_declarations[g]
if decl['state'] == 'vars':
raise AnsibleError("%s:%d: Section [%s:vars] not valid for undefined group: %s" % (path, decl['line'], decl['name'], decl['name']))
elif decl['state'] == 'children':
raise AnsibleError("%s:%d: Section [%s:children] includes undefined group: %s" % (path, decl['line'], decl['parents'].pop(), decl['name']))

def _add_pending_children(self, group, pending):
for parent in pending[group]['parents']:
Expand Down Expand Up @@ -326,7 +318,7 @@ def _expand_hostpattern(self, hostpattern):

try:
(pattern, port) = parse_address(hostpattern, allow_ranges=True)
except:
except Exception:
# not a recognizable host pattern
pattern = hostpattern
port = None
Expand Down

0 comments on commit 3456bba

Please sign in to comment.