From fd6429fbd1e0c471bd48481fa682fcfe06355f1a Mon Sep 17 00:00:00 2001 From: Ricardo Carrillo Cruz Date: Tue, 7 Mar 2017 16:57:19 +0100 Subject: [PATCH] Idempotency fix for iosxr_config (#22365) Multiple fixes to address idempotency issues, along with refactor of module_utils iosxr to look like ios, for code consistency. Fixes #22315 --- lib/ansible/module_utils/iosxr.py | 18 +++++++++++++++--- .../modules/network/iosxr/iosxr_system.py | 6 ++---- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/ansible/module_utils/iosxr.py b/lib/ansible/module_utils/iosxr.py index 432178d415..deed4cae79 100644 --- a/lib/ansible/module_utils/iosxr.py +++ b/lib/ansible/module_utils/iosxr.py @@ -27,7 +27,7 @@ # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # from ansible.module_utils.basic import env_fallback -from ansible.module_utils.network_common import to_list +from ansible.module_utils.network_common import to_list, ComplexList from ansible.module_utils.connection import exec_command _DEVICE_CONFIGS = {} @@ -64,9 +64,20 @@ def get_config(module, flags=[]): _DEVICE_CONFIGS[cmd] = cfg return cfg + +def to_commands(module, commands): + spec = { + 'command': dict(key=True), + 'prompt': dict(), + 'answer': dict() + } + transform = ComplexList(spec, module) + return transform(commands) + + def run_commands(module, commands, check_rc=True): responses = list() - + commands = to_commands(module, to_list(commands)) for cmd in to_list(commands): rc, out, err = exec_command(module, cmd) if check_rc and rc != 0: @@ -75,6 +86,7 @@ def run_commands(module, commands, check_rc=True): return responses def load_config(module, commands, commit=False, replace=False, comment=None): + rc, out, err = exec_command(module, 'configure terminal') if rc != 0: module.fail_json(msg='unable to enter configuration mode', err=err) @@ -82,7 +94,7 @@ def load_config(module, commands, commit=False, replace=False, comment=None): failed = False for command in to_list(commands): if command == 'end': - pass + continue rc, out, err = exec_command(module, command) if rc != 0: diff --git a/lib/ansible/modules/network/iosxr/iosxr_system.py b/lib/ansible/modules/network/iosxr/iosxr_system.py index 0e4ad5001a..619bfd9860 100644 --- a/lib/ansible/modules/network/iosxr/iosxr_system.py +++ b/lib/ansible/modules/network/iosxr/iosxr_system.py @@ -258,10 +258,8 @@ def main(): result['commands'] = commands if commands: - commit = not module.check_mode - response = load_config(module, commands, commit=commit) - if response.get('diff') and module._diff: - result['diff'] = {'prepared': response.get('diff')} + if not module.check_mode: + load_config(module, commands, commit=True) result['changed'] = True module.exit_json(**result)