fixes issue with cli shell left open (#21548)

The nxos action plugin will now close the shell connection once the
module has completely run
This commit is contained in:
Peter Sprygada 2017-02-16 22:11:32 -05:00 committed by GitHub
parent 20c5a1adc1
commit c9f6a2b740
2 changed files with 30 additions and 12 deletions

View file

@ -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

View file

@ -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)