From 8472ed640b39ede8b078e6700c280c02ffdb6c75 Mon Sep 17 00:00:00 2001 From: Peter Sprygada Date: Sun, 19 Feb 2017 17:13:14 -0500 Subject: [PATCH] fixes minor bugs in nxos action (#21641) * checks cli context is correct * adds display messages --- lib/ansible/plugins/action/nxos.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/ansible/plugins/action/nxos.py b/lib/ansible/plugins/action/nxos.py index 6c13aaf7a3..5637360808 100644 --- a/lib/ansible/plugins/action/nxos.py +++ b/lib/ansible/plugins/action/nxos.py @@ -50,6 +50,8 @@ class ActionModule(_ActionModule): provider = self.load_provider() transport = provider['transport'] or 'cli' + display.vvvv('connection transport is %s' % transport, self._play_context.remote_addr) + if transport == 'cli': pc = copy.deepcopy(self._play_context) pc.connection = 'network_cli' @@ -63,12 +65,23 @@ class ActionModule(_ActionModule): connection = self._shared_loader_obj.connection_loader.get('persistent', pc, sys.stdin) socket_path = self._get_socket_path(pc) + display.vvvv('socket_path: %s' % socket_path, pc.remote_addr) + if not os.path.exists(socket_path): # start the connection if it isn't started rc, out, err = connection.exec_command('open_shell()') - display.vvv('open_shell() returned %s %s %s' % (rc, out, err)) + display.vvvv('open_shell() returned %s %s %s' % (rc, out, err)) if rc != 0: return {'failed': True, 'msg': 'unable to open shell', 'rc': rc} + else: + # make sure we are in the right cli context which should be + # enable mode and not config module + rc, out, err = connection.exec_command('prompt()') + while str(out).strip().endswith(')#'): + display.debug('wrong context, sending exit to device', self._play_context.remote_addr) + connection.exec_command('exit') + rc, out, err = connection.exec_command('prompt()') + task_vars['ansible_socket'] = socket_path @@ -84,14 +97,7 @@ class ActionModule(_ActionModule): } self._task.args['provider'] = provider_arg - - result = super(ActionModule, self).run(tmp, task_vars) - - if transport == 'cli': - display.vvv('closing cli shell', self._play_context.remote_addr) - connection.exec_command('close_shell()') - - return result + return super(ActionModule, self).run(tmp, task_vars) def _get_socket_path(self, play_context): ssh = connection_loader.get('ssh', class_only=True)