From 2f10bdf0c7ec5825aa9bdb164de41a39e906bed6 Mon Sep 17 00:00:00 2001 From: Peter Sprygada Date: Sat, 18 Feb 2017 17:20:03 -0500 Subject: [PATCH] roll up of fixes in junos action plugin (#21624) * calls open_shell() or open_session() depending on connection type * closes shell after module completion * adds open_session() to netconf --- lib/ansible/plugins/action/junos.py | 28 ++++++++++++++--------- lib/ansible/plugins/connection/netconf.py | 3 +++ 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/lib/ansible/plugins/action/junos.py b/lib/ansible/plugins/action/junos.py index be57467b0a..12b4289960 100644 --- a/lib/ansible/plugins/action/junos.py +++ b/lib/ansible/plugins/action/junos.py @@ -65,25 +65,31 @@ class ActionModule(_ActionModule): pc.remote_user = provider['username'] or self._play_context.connection_user pc.password = provider['password'] or self._play_context.password pc.private_key_file = provider['ssh_keyfile'] or self._play_context.private_key_file + pc.timeout = provider['timeout'] or self._play_context.timeout + + display.vvv('using connection plugin %s' % pc.connection) + 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) - - if pc.connection == 'network_cli': - rc, out, err = connection.exec_command('show version') - display.vvv('%s %s %s' % (rc, out, err)) - if pc.connection == 'netconf': - # - req = new_ele('get-software-information') - connection.exec_command(to_xml(req)) + rc, out, err = connection.exec_command('open_session()') + else: + rc, out, err = connection.exec_command('open_shell()') + + if rc != 0: + return {'failed': True, 'msg': 'unable to connect to control socket'} task_vars['ansible_socket'] = socket_path - return super(ActionModule, self).run(tmp, task_vars) + 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 def _get_socket_path(self, play_context): ssh = connection_loader.get('ssh', class_only=True) diff --git a/lib/ansible/plugins/connection/netconf.py b/lib/ansible/plugins/connection/netconf.py index edc1c8b4ac..f323252921 100644 --- a/lib/ansible/plugins/connection/netconf.py +++ b/lib/ansible/plugins/connection/netconf.py @@ -101,6 +101,9 @@ class Connection(ConnectionBase): def exec_command(self, request): """Sends the request to the node and returns the reply """ + if request == 'open_session()': + return (0, 'ok', '') + req = to_ele(request) if req is None: return (1, '', 'unable to parse request')