a )gB @s ddlmZmZmZeZddlmZddlm Z ddl m Z ddl m Z ddlmZddlmZz,ddlmZdd lmZmZmZd Zd ZWn,eyZzd ZeZWYd Z[n d Z[00zdd lmZmZmZm Z Wn,e!ydd l"mZmZmZm Z Yn0ddZ#Gddde Z$d S))absolute_importdivisionprint_function)abstractmethodwraps) AnsibleError) AnsiblePlugin) to_native)missing_required_lib)RPCError)to_xmlto_ele NCElementTNF)Element SubElementtostring fromstringcstfdd}|S)Ncs2tstdtdttf|g|Ri|S)Nz%s: %sZncclient) HAS_NCCLIENTrr r NCCLIENT_IMP_ERR)selfargskwargsfuncD/usr/lib/python3.9/site-packages/ansible/plugins/netconf/__init__.pywrapped1sz ensure_ncclient..wrappedr)rrrrrensure_ncclient0srcseZdZdZgdZfddZeddZddZd3d d Z d4d d Z d5ddZ d6ddZ ddZ d7ddZd8ddZd9ddZddZd:dd Zd;d!d"Zd#d$Zd%d&Zed'd(Zed)d*Zd+d,Zd-d.Zd/d0Zd1d2ZZS)< NetconfBasea] A base class for implementing Netconf connections .. note:: Unlike most of Ansible, nearly all strings in :class:`TerminalBase` plugins are byte strings. This is because of how close to the underlying platform these plugins operate. Remember to mark literal strings as byte string (``b"string"``) and to use :func:`~ansible.module_utils._text.to_bytes` and :func:`~ansible.module_utils._text.to_text` to avoid unexpected problems. List of supported rpc's: :get: Retrieves running configuration and device state information :get_config: Retrieves the specified configuration from the device :edit_config: Loads the specified commands into the remote device :commit: Load configuration from candidate to running :discard_changes: Discard changes to candidate datastore :validate: Validate the contents of the specified configuration. :lock: Allows the client to lock the configuration system of a device. :unlock: Release a configuration lock, previously obtained with the lock operation. :copy_config: create or replace an entire configuration datastore with the contents of another complete configuration datastore. :get-schema: Retrieves the required schema from the device :get_capabilities: Retrieves device information and supported rpc methods For JUNOS: :execute_rpc: RPC to be execute on remote device :load_configuration: Loads given configuration on device Note: rpc support depends on the capabilities of remote device. :returns: Returns output received from remote device as byte string Note: the 'result' or 'error' from response should to be converted to object of ElementTree using 'fromstring' to parse output as xml doc 'get_capabilities()' returns 'result' as a json string. Usage: from ansible.module_utils.connection import Connection conn = Connection() data = conn.execute_rpc(rpc) reply = fromstring(reply) data = conn.get_capabilities() json.loads(data) conn.load_configuration(config=[''set system ntp server 1.1.1.1''], action='set', format='text') )rpc get_configget edit_configvalidate copy_configdispatchlockunlockdiscard_changescommit get_schema delete_configget_device_operationscstt|||_dSN)superr__init__ _connection)rZ connection __class__rrr0oszNetconfBase.__init__cCs|jjSr.)r1Zmanagerrrrrmssz NetconfBase.mc Cshz,t|}|j|}t|dr&|jn|jWStyb}z|j}tt|WYd}~n d}~00dS)z RPC to be execute on remote device :param name: Name of rpc in string format :return: Received rpc response from remote host data_xmlN) rr5r hasattrr6xmlr Exceptionr )rnameobjrespexcmsgrrrr ws zNetconfBase.rpcNcCs@t|trt|}|sd}|jj||d}t|dr:|jS|jS)a Retrieve all or part of a specified configuration (by default entire configuration is retrieved). :param source: Name of the configuration datastore being queried, defaults to running datastore :param filter: This argument specifies the portion of the configuration data to retrieve :return: Returns xml string containing the RPC response received from remote host runningsourcefilterr6) isinstancelisttupler5r!r7r6r8)rrArBr<rrrr!s  zNetconfBase.get_configcCs<t|trt|}|jj||d}t|dr2|jn|j}|S)a Retrieve device configuration and state information. :param filter: This argument specifies the portion of the state data to retrieve (by default entire state data is retrieved) :param with_defaults: defines an explicit method of retrieving default values from the configuration :return: Returns xml string containing the RPC response received from remote host )rB with_defaultsr6)rCrDrEr5r"r7r6r8)rrBrFr<Zresponserrrr"s zNetconfBase.getr8 candidatecCs>|durtd|jj||||||d}t|dr8|jS|jS)a Loads all or part of the specified *config* to the *target* configuration datastore. :param config: Is the configuration, which must be rooted in the `config` element. It can be specified either as a string or an :class:`~xml.etree.ElementTree.Element`. :param format: The format of configuration eg. xml, text :param target: Is the name of the configuration datastore being edited :param default_operation: If specified must be one of { `"merge"`, `"replace"`, or `"none"` } :param test_option: If specified must be one of { `"test_then_set"`, `"set"` } :param error_option: If specified must be one of { `"stop-on-error"`, `"continue-on-error"`, `"rollback-on-error"` } The `"rollback-on-error"` *error_option* depends on the `:rollback-on-error` capability. :return: Returns xml string containing the RPC response received from remote host Nzconfig value must be provided)formattargetdefault_operation test_option error_optionr6) ValueErrorr5r#r7r6r8)rZconfigrHrIrJrKrLr<rrrr#s zNetconfBase.edit_configcCs$|jj|d}t|dr|jS|jS)aS Validate the contents of the specified configuration. :param source: Is the name of the configuration datastore being validated or `config` element containing the configuration subtree to be validated :return: Returns xml string containing the RPC response received from remote host )rAr6)r5r$r7r6r8)rrAr<rrrr$szNetconfBase.validatecCs$|j||}t|dr|jS|jS)a Create or replace an entire configuration datastore with the contents of another complete configuration datastore. :param source: Is the name of the configuration datastore to use as the source of the copy operation or `config` element containing the configuration subtree to copy :param target: Is the name of the configuration datastore to use as the destination of the copy operation :return: Returns xml string containing the RPC response received from remote host r6)r5r%r7r6r8)rrArIr<rrrr%szNetconfBase.copy_configcCsZ|durtd|jjt|||d}t|tr8|j}nt|drP|jrP|j}n|j }|S)a Execute rpc on the remote device eg. dispatch('clear-arp-table') :param rpc_command: specifies rpc command to be dispatched either in plain text or in xml element format (depending on command) :param source: name of the configuration datastore being queried :param filter: specifies the portion of the configuration to retrieve (by default entire configuration is retrieved) :return: Returns xml string containing the RPC response received from remote host Nz"rpc_command value must be providedr@data_ele) rMr5r&rrCrr6r7rNr8)rZ rpc_commandrArBr<resultrrrr&s zNetconfBase.dispatchcCs$|jj|d}t|dr|jS|jS)a. Allows the client to lock the configuration system of a device. :param target: is the name of the configuration datastore to lock, defaults to candidate datastore :return: Returns xml string containing the RPC response received from remote host rIr6)r5r'r7r6r8rrIr<rrrr'szNetconfBase.lockcCs$|jj|d}t|dr|jS|jS)a: Release a configuration lock, previously obtained with the lock operation. :param target: is the name of the configuration datastore to unlock, defaults to candidate datastore :return: Returns xml string containing the RPC response received from remote host rPr6)r5r(r7r6r8rQrrrr(szNetconfBase.unlockcCs |j}t|dr|jS|jS)z Revert the candidate configuration to the currently running configuration. Any uncommitted changes are discarded. :return: Returns xml string containing the RPC response received from remote host r6)r5r)r7r6r8)rr<rrrr)s zNetconfBase.discard_changesFcCs(|jj|||d}t|dr"|jS|jS)az Commit the candidate configuration as the device's new current configuration. Depends on the `:candidate` capability. A confirmed commit (i.e. if *confirmed* is `True`) is reverted if there is no followup commit within the *timeout* interval. If no timeout is specified the confirm timeout defaults to 600 seconds (10 minutes). A confirming commit may have the *confirmed* parameter but this is not required. Depends on the `:confirmed-commit` capability. :param confirmed: whether this is a confirmed commit :param timeout: specifies the confirm timeout in seconds :param persist: make the confirmed commit survive a session termination, and set a token on the ongoing confirmed commit :return: Returns xml string containing the RPC response received from remote host ) confirmedtimeoutpersistr6)r5r*r7r6r8)rrRrSrTr<rrrr*szNetconfBase.commitcCs(|jj|||d}t|dr"|jS|jS)ae Retrieve a named schema, with optional revision and type. :param identifier: name of the schema to be retrieved :param version: version of schema to get :param format: format of the schema to be retrieved, yang is the default :return: Returns xml string containing the RPC response received from remote host )versionrHr6)r5r+r7r6r8)rZ identifierrUrHr<rrrr+szNetconfBase.get_schemacCs"|j|}t|dr|jS|jS)z delete a configuration datastore :param target: specifies the name or URL of configuration datastore to delete :return: Returns xml string containing the RPC response received from remote host r6)r5r,r7r6r8rQrrrr,!s zNetconfBase.delete_configcCs |j|Sr.)r5locked)rrIrrrrV*szNetconfBase.lockedcCsdS)z Retrieves device information and supported rpc methods by device platform and return result as a string :return: Netconf session capability Nrr4rrrget_capabilities-szNetconfBase.get_capabilitiescCsdS)z Identifies the operating system of network device. :param obj: ncclient manager connection instance :return: The name of network operating system. Nr)r;rrrguess_network_os7szNetconfBase.guess_network_oscCs|jS)zs Returns list of base rpc method supported by remote device :return: List of RPC supported )__rpc__r4rrr get_base_rpc@szNetconfBase.get_base_rpccCsdS)z Copies file to remote host :param source: Source location of file :param destination: Destination file path :return: Returns xml string containing the RPC response received from remote host NrrrAZ destinationrrrput_fileGszNetconfBase.put_filecCsdS)z Fetch file from remote host :param source: Source location of file :param destination: Source location of file :return: Returns xml string containing the RPC response received from remote host Nrr[rrr fetch_filePszNetconfBase.fetch_filecCsi}d|}d|v|d<d|v|d<d|v|d<d|v|d <d |v|d <d |v|d <d|v|d<g|d<|d r|dd|dr|dd|d r|ddt|d|d<|S)z Retrieve remote host capability from Netconf server hello message. :param server_capabilities: Server capabilities received during Netconf session initialization :return: Remote host capabilities in dictionary format  z :candidateZsupports_commitz:with-defaultsZsupports_defaultsz:confirmed-commitZsupports_confirm_commitz:startupZsupports_startupz:xpathZsupports_xpathz:writable-runningZsupports_writable_runningz :validateZsupports_validateZlock_datastorer?rGZstartupZ supports_lock)joinappendbool)rZserver_capabilitiesZ operationsZ capabilitiesrrrr-Ys$        z!NetconfBase.get_device_operations)NN)NN)Nr8rGNNN)rG)NNN)rG)rG)FNN)NNN)__name__ __module__ __qualname____doc__rYr0propertyr5r r!r"r#r$r%r&r'r(r)r*r+r,rVrrW staticmethodrXrZr\r]r- __classcell__rrr2rr9s62               r)%Z __future__rrrtypeZ __metaclass__abcr functoolsrZansible.errorsrZansible.pluginsr Zansible.module_utils._textr Zansible.module_utils.basicr Zncclient.operationsr Z ncclient.xml_r rrrrr9errZ lxml.etreerrrr ImportErrorZxml.etree.ElementTreerrrrrrs*