From c9f6a2b740682aad98b6621e9e83cd2d1f60ac80 Mon Sep 17 00:00:00 2001 From: Peter Sprygada Date: Thu, 16 Feb 2017 22:11:32 -0500 Subject: [PATCH] fixes issue with cli shell left open (#21548) The nxos action plugin will now close the shell connection once the module has completely run --- lib/ansible/module_utils/nxos.py | 7 +++--- lib/ansible/plugins/action/nxos.py | 35 ++++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/lib/ansible/module_utils/nxos.py b/lib/ansible/module_utils/nxos.py index 1364599dd83..d6254832565 100644 --- a/lib/ansible/module_utils/nxos.py +++ b/lib/ansible/module_utils/nxos.py @@ -68,11 +68,12 @@ def get_connection(module): global _DEVICE_CONNECTION if not _DEVICE_CONNECTION: load_params(module) - transport = module.params['transport'] - if transport == 'cli': + if 'transport' not in module.params: conn = Cli(module) - elif transport == 'nxapi': + elif module.params['transport'] == 'nxapi': conn = Nxapi(module) + else: + conn = Cli(module) _DEVICE_CONNECTION = conn return _DEVICE_CONNECTION diff --git a/lib/ansible/plugins/action/nxos.py b/lib/ansible/plugins/action/nxos.py index c6c80a3ad03..dee35e0e831 100644 --- a/lib/ansible/plugins/action/nxos.py +++ b/lib/ansible/plugins/action/nxos.py @@ -31,14 +31,26 @@ from ansible.module_utils.nxos import nxos_argument_spec from ansible.module_utils.basic import AnsibleFallbackNotFound from ansible.module_utils._text import to_bytes +try: + from __main__ import display +except ImportError: + from ansible.utils.display import Display + display = Display() + class ActionModule(_ActionModule): def run(self, tmp=None, task_vars=None): + if self._play_context.connection != 'local': + return dict( + failed=True, + msg='invalid connection specified, expected connection=local, ' + 'got %s' % self._play_context.connection + ) provider = self.load_provider() - transport = provider['transport'] + transport = provider['transport'] or 'cli' - if not transport or 'cli' in transport: + if transport == 'cli': pc = copy.deepcopy(self._play_context) pc.connection = 'network_cli' pc.network_os = 'nxos' @@ -46,11 +58,15 @@ class ActionModule(_ActionModule): pc.remote_user = provider['username'] or self._play_context.connection_user pc.password = provider['password'] or self._play_context.password + connection = self._shared_loader_obj.connection_loader.get('persistent', pc, sys.stdin) + socket_path = self._get_socket_path(pc) if not os.path.exists(socket_path): # start the connection if it isn't started - connection = self._shared_loader_obj.connection_loader.get('persistent', pc, sys.stdin) - connection.exec_command('EXEC: show version') + rc, out, err = connection.exec_command('open_shell()') + display.vvv('open_shell() returned %s %s %s' % (rc, out, err)) + if rc != 0: + return {'failed': True, 'msg': 'unable to open shell', 'rc': rc} task_vars['ansible_socket'] = socket_path @@ -69,12 +85,13 @@ class ActionModule(_ActionModule): self._task.args['validate_certs'] = task_vars['nxapi_validate_certs'] - transport = self._task.args.get('transport') - if not transport: - transport = self._task.args.get('provider', {}).get('transport') - self._task.args['transport'] = transport or 'cli' + result = super(ActionModule, self).run(tmp, task_vars) - return 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 def _get_socket_path(self, play_context): ssh = connection_loader.get('ssh', class_only=True)