From 6cca2b06fe74c31cacfe047571e0621d34ac3627 Mon Sep 17 00:00:00 2001 From: Peter Sprygada Date: Thu, 8 Sep 2016 15:18:00 -0400 Subject: [PATCH] fixes bug where nxos_config wasn't handling checkpoints correctly --- .../modules/network/nxos/nxos_config.py | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/ansible/modules/network/nxos/nxos_config.py b/lib/ansible/modules/network/nxos/nxos_config.py index 0a49460255a..33ced743b75 100644 --- a/lib/ansible/modules/network/nxos/nxos_config.py +++ b/lib/ansible/modules/network/nxos/nxos_config.py @@ -245,7 +245,7 @@ def get_config(module, result): contents = module.config.get_config(include_defaults=defaults) result[key] = contents - return NetworkConfig(indent=1, contents=contents) + return NetworkConfig(indent=2, contents=contents) def backup_config(module, result): if '__config__' not in result: @@ -254,15 +254,13 @@ def backup_config(module, result): def load_config(module, commands, result): if not module.check_mode: - checkpoint = 'ansible_%s' % int(time.time()) - module.cli(['checkpoint %s' % checkpoint], output='text') - result['__checkpoint__'] = checkpoint module.config.load_config(commands) result['changed'] = True def load_checkpoint(module, result): try: checkpoint = result['__checkpoint__'] + module.log('load checkpoint %s' % checkpoint) module.cli(['rollback running-config checkpoint %s' % checkpoint, 'no checkpoint %s' % checkpoint], output='text') except KeyError: @@ -281,7 +279,9 @@ def run(module, result): if match != 'none': config = get_config(module, result) - configobjs = candidate.difference(config, match=match, replace=replace) + path = module.params['parents'] + configobjs = candidate.difference(config, path=path, match=match, + replace=replace) else: config = None configobjs = candidate.items @@ -291,7 +291,6 @@ def run(module, result): if configobjs: commands = dumps(configobjs, 'commands').split('\n') - result['updates'] = commands if module.params['before']: commands[:0] = module.params['before'] @@ -299,6 +298,15 @@ def run(module, result): if module.params['after']: commands.extend(module.params['after']) + result['updates'] = commands + + # create a checkpoint of the current running config in case + # there is a problem loading the candidate config + checkpoint = 'ansible_%s' % int(time.time()) + module.cli(['checkpoint %s' % checkpoint], output='text') + result['__checkpoint__'] = checkpoint + module.log('create checkpoint %s' % checkpoint) + # if the update mode is set to check just return # and do not try to load into the system if update != 'check': @@ -307,7 +315,8 @@ def run(module, result): # remove the checkpoint file used to restore the config # in case of an error if not module.check_mode: - module.cli('no checkpoint %s' % result['__checkpoint__']) + module.log('remove checkpoint %s' % checkpoint) + module.cli('no checkpoint %s' % checkpoint, output='text') if module.params['save'] and not module.check_mode: module.config.save_config()