From 06f76a2741eb4216ae1b2a348bbb0f66c05f1e5c Mon Sep 17 00:00:00 2001 From: Sander Steffann Date: Tue, 30 May 2017 08:25:25 +0200 Subject: [PATCH] Fix IndexError when junos_config contains multiple delete lines (#25139) * Prevent IndexError when deleting multiple lines The old code will raise `IndexError: list assignment index out of range` when deleting multiple lines because the indexes of the original and the copy get out of sync. Solved by deleting from the high indexes first so the lower ones remain stable. * Don't load configuration if nothing to load Instead of sending an empty candidate config (for example because the candidate only consisted of `delete` lines, and all of them were filtered out by `filter_delete_statements`) just return. JunOS seems to get confused by empty changes, and if the candidate config is empty then it's a no-op anyway. --- lib/ansible/module_utils/junos.py | 3 +++ lib/ansible/modules/network/junos/junos_config.py | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/ansible/module_utils/junos.py b/lib/ansible/module_utils/junos.py index 94e41e8d8a9..bf4e797cbe4 100644 --- a/lib/ansible/module_utils/junos.py +++ b/lib/ansible/module_utils/junos.py @@ -169,6 +169,9 @@ def get_diff(module): def load_config(module, candidate, warnings, action='merge', commit=False, format='xml', comment=None, confirm=False, confirm_timeout=None): + if not candidate: + return + with locked_config(module): if isinstance(candidate, list): candidate = '\n'.join(candidate) diff --git a/lib/ansible/modules/network/junos/junos_config.py b/lib/ansible/modules/network/junos/junos_config.py index a0783b79f05..16a62d719e2 100644 --- a/lib/ansible/modules/network/junos/junos_config.py +++ b/lib/ansible/modules/network/junos/junos_config.py @@ -242,7 +242,7 @@ def filter_delete_statements(module, candidate): config = to_native(match.text, encoding='latin1') modified_candidate = candidate[:] - for index, line in enumerate(candidate): + for index, line in reversed(list(enumerate(candidate))): if line.startswith('delete'): newline = re.sub('^delete', 'set', line) if newline not in config: