mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-10-05 16:04:09 -07:00
Add support for cliconf and netconf plugin (#25093)
* ansible-connection refactor and action plugin changes * Add cliconf plugin for eos, ios, iosxr, junos, nxos, vyos * Add netconf plugin for junos * Add jsonrpc support * Modify network_cli and netconf connection plugin * Fix py3 unit test failure * Fix review comment * Minor fixes * Fix ansible-connection review comments * Fix CI issue * platform_agnostic related changes
This commit is contained in:
parent
c20285782d
commit
6215922889
32 changed files with 1542 additions and 585 deletions
|
@ -20,10 +20,14 @@ __metaclass__ = type
|
|||
|
||||
import os
|
||||
import logging
|
||||
import json
|
||||
|
||||
from ansible import constants as C
|
||||
from ansible.errors import AnsibleConnectionFailure, AnsibleError
|
||||
from ansible.module_utils._text import to_bytes, to_native, to_text
|
||||
from ansible.plugins import netconf_loader
|
||||
from ansible.plugins.connection import ConnectionBase, ensure_connect
|
||||
from ansible.utils.jsonrpc import Rpc
|
||||
|
||||
try:
|
||||
from ncclient import manager
|
||||
|
@ -42,8 +46,8 @@ except ImportError:
|
|||
logging.getLogger('ncclient').setLevel(logging.INFO)
|
||||
|
||||
|
||||
class Connection(ConnectionBase):
|
||||
''' NetConf connections '''
|
||||
class Connection(Rpc, ConnectionBase):
|
||||
"""NetConf connections"""
|
||||
|
||||
transport = 'netconf'
|
||||
has_pipelining = False
|
||||
|
@ -90,12 +94,20 @@ class Connection(ConnectionBase):
|
|||
raise AnsibleConnectionFailure(str(exc))
|
||||
|
||||
if not self._manager.connected:
|
||||
return (1, '', 'not connected')
|
||||
return 1, b'', b'not connected'
|
||||
|
||||
display.display('ncclient manager object created successfully', log_only=True)
|
||||
|
||||
self._connected = True
|
||||
return (0, self._manager.session_id, '')
|
||||
|
||||
self._netconf = netconf_loader.get(self._network_os, self)
|
||||
if self._netconf:
|
||||
self._rpc.add(self._netconf)
|
||||
display.display('loaded netconf plugin for network_os %s' % self._network_os, log_only=True)
|
||||
else:
|
||||
display.display('unable to load netconf for network_os %s' % self._network_os)
|
||||
|
||||
return 0, to_bytes(self._manager.session_id, errors='surrogate_or_strict'), b''
|
||||
|
||||
def close(self):
|
||||
if self._manager:
|
||||
|
@ -106,20 +118,37 @@ class Connection(ConnectionBase):
|
|||
@ensure_connect
|
||||
def exec_command(self, request):
|
||||
"""Sends the request to the node and returns the reply
|
||||
The method accepts two forms of request. The first form is as a byte
|
||||
string that represents xml string be send over netconf session.
|
||||
The second form is a json-rpc (2.0) byte string.
|
||||
"""
|
||||
if request == 'open_session()':
|
||||
return (0, 'ok', '')
|
||||
try:
|
||||
obj = json.loads(to_text(request, errors='surrogate_or_strict'))
|
||||
|
||||
if 'jsonrpc' in obj:
|
||||
if self._netconf:
|
||||
out = self._exec_rpc(obj)
|
||||
else:
|
||||
out = self.internal_error("netconf plugin is not supported for network_os %s" % self._play_context.network_os)
|
||||
return 0, to_bytes(out, errors='surrogate_or_strict'), b''
|
||||
else:
|
||||
err = self.invalid_request(obj)
|
||||
return 1, b'', to_bytes(err, errors='surrogate_or_strict')
|
||||
|
||||
except (ValueError, TypeError):
|
||||
# to_ele operates on native strings
|
||||
request = to_native(request, errors='surrogate_or_strict')
|
||||
|
||||
req = to_ele(request)
|
||||
if req is None:
|
||||
return (1, '', 'unable to parse request')
|
||||
return 1, b'', b'unable to parse request'
|
||||
|
||||
try:
|
||||
reply = self._manager.rpc(req)
|
||||
except RPCError as exc:
|
||||
return (1, '', to_xml(exc.xml))
|
||||
return 1, b'', to_bytes(to_xml(exc.xml), errors='surrogate_or_strict')
|
||||
|
||||
return (0, reply.data_xml, '')
|
||||
return 0, to_bytes(reply.data_xml, errors='surrogate_or_strict'), b''
|
||||
|
||||
def put_file(self, in_path, out_path):
|
||||
"""Transfer a file from local to remote"""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue