관리-도구
편집 파일: __init__.cpython-39.pyc
a �)g�B � @ s d dl mZmZmZ eZd dlmZ d dlm Z d dl mZ d dlm Z d dlmZ d dlmZ z,d dlmZ d d lmZmZmZ d ZdZW n, ey� Z zdZeZW Y dZ[n dZ[0 0 zd d lmZmZmZm Z W n, e!�y d d l"mZmZmZm Z Y n0 dd� Z#G dd� de �Z$dS )� )�absolute_import�division�print_function)�abstractmethod��wraps)�AnsibleError)� AnsiblePlugin)� to_native)�missing_required_lib)�RPCError)�to_xml�to_ele� NCElementTNF)�Element� SubElement�tostring� fromstringc s t � �� fdd��}|S )Nc s2 t stdtd�tt�f ��� | g|�R i |��S )Nz%s: %sZncclient)�HAS_NCCLIENTr r r �NCCLIENT_IMP_ERR)�self�args�kwargs��func� �D/usr/lib/python3.9/site-packages/ansible/plugins/netconf/__init__.py�wrapped1 s z ensure_ncclient.<locals>.wrappedr )r r r r r �ensure_ncclient0 s r c s� e Zd ZdZg d�Z� fdd�Zedd� �Zdd� Zd3d d�Z d4dd �Z d5dd�Zd6dd�Zdd� Z d7dd�Zd8dd�Zd9dd�Zdd� Zd:dd �Zd;d!d"�Zd#d$� Zd%d&� Zed'd(� �Zed)d*� �Zd+d,� Zd-d.� Zd/d0� Zd1d2� Z� ZS )<�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_config�get�edit_config�validate�copy_config�dispatch�lock�unlock�discard_changes�commit� get_schema� delete_config�get_device_operationsc s t t| ��� || _d S �N)�superr �__init__�_connection)r Z connection�� __class__r r r0 o s zNetconfBase.__init__c C s | j jS r. )r1 Zmanager�r r r r �ms s z NetconfBase.mc C sh z,t |�}| j�|�}t|d�r&|jn|jW S tyb } z|j}tt|���W Y d}~n d}~0 0 dS )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) r r5 r �hasattrr6 �xmlr � Exceptionr )r �name�obj�resp�exc�msgr r r r w s zNetconfBase.rpcNc C s@ t |t�rt|�}|sd}| jj||d�}t|d�r:|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 �running��source�filterr6 )� isinstance�list�tupler5 r! r7 r6 r8 )r rA rB r<