From bd036c15e0734e55838a0becb83362336aaec0a4 Mon Sep 17 00:00:00 2001 From: Peter Sprygada Date: Sun, 19 Feb 2017 18:04:00 -0500 Subject: [PATCH] minor fixes to junos action (#21643) * adds instrumentation messages * checks cli context is not in config mode --- lib/ansible/plugins/action/junos.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/ansible/plugins/action/junos.py b/lib/ansible/plugins/action/junos.py index 12b4289960..676b20da03 100644 --- a/lib/ansible/plugins/action/junos.py +++ b/lib/ansible/plugins/action/junos.py @@ -71,6 +71,8 @@ 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 if pc.connection == 'netconf': @@ -80,16 +82,19 @@ class ActionModule(_ActionModule): if rc != 0: return {'failed': True, 'msg': 'unable to connect to control socket'} + elif pc.connection == 'network_cli': + # 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 - result = super(ActionModule, self).run(tmp, task_vars) - - if pc.connection == 'network_cli': - display.vvv('closing cli shell connection', self._play_context.remote_addr) - rc, out, err = 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)