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

Resolved issue with NetworkConfig parsing device configs with inconsistent indentation levels. (#51850)

This commit is contained in:
blandrew 2019-03-20 19:23:51 +11:00 committed by ansibot
parent 07774b4ccf
commit cfd869e898

View file

@ -214,8 +214,7 @@ class NetworkConfig(object):
ancestors = list() ancestors = list()
config = list() config = list()
curlevel = 0 indents = [0]
prevlevel = 0
for linenum, line in enumerate(to_native(lines, errors='surrogate_or_strict').split('\n')): for linenum, line in enumerate(to_native(lines, errors='surrogate_or_strict').split('\n')):
text = entry_reg.sub('', line).strip() text = entry_reg.sub('', line).strip()
@ -228,20 +227,21 @@ class NetworkConfig(object):
# handle top level commands # handle top level commands
if toplevel.match(line): if toplevel.match(line):
ancestors = [cfg] ancestors = [cfg]
prevlevel = curlevel indents = [0]
curlevel = 0
# handle sub level commands # handle sub level commands
else: else:
match = childline.match(line) match = childline.match(line)
line_indent = match.start(1) line_indent = match.start(1)
prevlevel = curlevel if line_indent < indents[-1]:
curlevel = int(line_indent / self._indent) while indents[-1] > line_indent:
indents.pop()
if (curlevel - 1) > prevlevel: if line_indent > indents[-1]:
curlevel = prevlevel + 1 indents.append(line_indent)
curlevel = len(indents) - 1
parent_level = curlevel - 1 parent_level = curlevel - 1
cfg._parents = ancestors[:curlevel] cfg._parents = ancestors[:curlevel]