From cc7e09451a14b50ee9d9f0d17bd8ccb3bd5617a1 Mon Sep 17 00:00:00 2001 From: Ricardo Carrillo Cruz Date: Wed, 29 Mar 2017 16:21:23 +0200 Subject: [PATCH] Fall-back to show configuration on old IOSXR devices (#22900) In old IOSXR versions, 'show commit changes diff' does not work. Fall-back to 'show configuration' if that command fails so execution can move forward. Fixes #22235 --- lib/ansible/module_utils/iosxr.py | 9 ++++++++- lib/ansible/modules/network/iosxr/_iosxr_template.py | 2 +- lib/ansible/modules/network/iosxr/iosxr_config.py | 4 ++-- lib/ansible/modules/network/iosxr/iosxr_system.py | 2 +- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/ansible/module_utils/iosxr.py b/lib/ansible/module_utils/iosxr.py index deed4cae793..53ea7db7df7 100644 --- a/lib/ansible/module_utils/iosxr.py +++ b/lib/ansible/module_utils/iosxr.py @@ -85,7 +85,7 @@ def run_commands(module, commands, check_rc=True): responses.append(out) return responses -def load_config(module, commands, commit=False, replace=False, comment=None): +def load_config(module, commands, warnings, commit=False, replace=False, comment=None): rc, out, err = exec_command(module, 'configure terminal') if rc != 0: @@ -106,6 +106,13 @@ def load_config(module, commands, commit=False, replace=False, comment=None): module.fail_json(msg=err, commands=commands, rc=rc) rc, diff, err = exec_command(module, 'show commit changes diff') + if rc != 0: + # If we failed, maybe we are in an old version so + # we run show configuration instead + rc, diff, err = exec_command(module, 'show configuration') + if module._diff: + warnings.append('device platform does not support config diff') + if commit: cmd = 'commit' if comment: diff --git a/lib/ansible/modules/network/iosxr/_iosxr_template.py b/lib/ansible/modules/network/iosxr/_iosxr_template.py index 98bba282cdb..e4aea396029 100644 --- a/lib/ansible/modules/network/iosxr/_iosxr_template.py +++ b/lib/ansible/modules/network/iosxr/_iosxr_template.py @@ -146,7 +146,7 @@ def main(): commands = [c.strip() for c in str(candidate).split('\n')] if commands: - load_config(module, commands, not module.check_mode) + load_config(module, commands, result['warnings'], not module.check_mode) result['changed'] = not module.check_mode result['updates'] = commands diff --git a/lib/ansible/modules/network/iosxr/iosxr_config.py b/lib/ansible/modules/network/iosxr/iosxr_config.py index 6232022e70e..7a0d3ac5ae8 100644 --- a/lib/ansible/modules/network/iosxr/iosxr_config.py +++ b/lib/ansible/modules/network/iosxr/iosxr_config.py @@ -240,8 +240,8 @@ def run(module, result): result['commands'] = commands - diff = load_config(module, commands, not check_mode, - replace_config, comment) + diff = load_config(module, commands, result['warnings'], + not check_mode, replace_config, comment) if diff: result['diff'] = dict(prepared=diff) result['changed'] = True diff --git a/lib/ansible/modules/network/iosxr/iosxr_system.py b/lib/ansible/modules/network/iosxr/iosxr_system.py index 92f70c1eb2b..e79460b7980 100644 --- a/lib/ansible/modules/network/iosxr/iosxr_system.py +++ b/lib/ansible/modules/network/iosxr/iosxr_system.py @@ -243,7 +243,7 @@ def main(): if commands: if not module.check_mode: - load_config(module, commands, commit=True) + load_config(module, commands, result['warnings'], commit=True) result['changed'] = True module.exit_json(**result)