From df359e036c095c67eca66bbc20d067e2fe5382b1 Mon Sep 17 00:00:00 2001 From: Nathaniel Case Date: Mon, 11 Feb 2019 14:22:07 -0500 Subject: [PATCH] [stable-2.7] ios retry config if section filter fails (#49485) (#51871) * [stable-2.7] ios retry config if section filter fails (#49485) * Attempt to work around devices that don't understand | section * Fix case of no flags (cherry picked from commit 6caed0c) Co-authored-by: Nathaniel Case * Add changelog --- changelogs/fragments/49485-ios-retry-section.yaml | 2 ++ lib/ansible/module_utils/network/ios/ios.py | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/49485-ios-retry-section.yaml diff --git a/changelogs/fragments/49485-ios-retry-section.yaml b/changelogs/fragments/49485-ios-retry-section.yaml new file mode 100644 index 00000000000..2c159e71b57 --- /dev/null +++ b/changelogs/fragments/49485-ios-retry-section.yaml @@ -0,0 +1,2 @@ +bugfixes: +- If an ios module uses a section filter on a device which does not support it, retry the command without the filter. diff --git a/lib/ansible/module_utils/network/ios/ios.py b/lib/ansible/module_utils/network/ios/ios.py index ac79357033d..98c12bf80c0 100644 --- a/lib/ansible/module_utils/network/ios/ios.py +++ b/lib/ansible/module_utils/network/ios/ios.py @@ -104,7 +104,13 @@ def get_defaults_flag(module): def get_config(module, flags=None): - flag_str = ' '.join(to_list(flags)) + flags = to_list(flags) + + section_filter = False + if flags and 'section' in flags[-1]: + section_filter = True + + flag_str = ' '.join(flags) try: return _DEVICE_CONFIGS[flag_str] @@ -113,7 +119,11 @@ def get_config(module, flags=None): try: out = connection.get_config(flags=flags) except ConnectionError as exc: - module.fail_json(msg=to_text(exc, errors='surrogate_then_replace')) + if section_filter: + # Some ios devices don't understand `| section foo` + out = get_config(module, flags=flags[:-1]) + else: + module.fail_json(msg=to_text(exc, errors='surrogate_then_replace')) cfg = to_text(out, errors='surrogate_then_replace').strip() _DEVICE_CONFIGS[flag_str] = cfg return cfg