From cfd869e8983d9c26758e35b3a8b6f3a41d2f953a Mon Sep 17 00:00:00 2001 From: blandrew <47349309+blandrew@users.noreply.github.com> Date: Wed, 20 Mar 2019 19:23:51 +1100 Subject: [PATCH] Resolved issue with NetworkConfig parsing device configs with inconsistent indentation levels. (#51850) --- .../module_utils/network/common/config.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/ansible/module_utils/network/common/config.py b/lib/ansible/module_utils/network/common/config.py index 5e6420c1993..d5c13bacb02 100644 --- a/lib/ansible/module_utils/network/common/config.py +++ b/lib/ansible/module_utils/network/common/config.py @@ -214,8 +214,7 @@ class NetworkConfig(object): ancestors = list() config = list() - curlevel = 0 - prevlevel = 0 + indents = [0] for linenum, line in enumerate(to_native(lines, errors='surrogate_or_strict').split('\n')): text = entry_reg.sub('', line).strip() @@ -228,20 +227,21 @@ class NetworkConfig(object): # handle top level commands if toplevel.match(line): ancestors = [cfg] - prevlevel = curlevel - curlevel = 0 + indents = [0] # handle sub level commands else: match = childline.match(line) line_indent = match.start(1) - prevlevel = curlevel - curlevel = int(line_indent / self._indent) + if line_indent < indents[-1]: + while indents[-1] > line_indent: + indents.pop() - if (curlevel - 1) > prevlevel: - curlevel = prevlevel + 1 + if line_indent > indents[-1]: + indents.append(line_indent) + curlevel = len(indents) - 1 parent_level = curlevel - 1 cfg._parents = ancestors[:curlevel]