
    Vh<                        d dl mZmZmZ eZdZd dlZd dlZd dl	Z	d dl
mZmZ d dlmZmZmZ d dlmZ d dlmZmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dl m!Z! d dl"m#Z# 	 d dl$m%Z& d dl$m'Z' d dl(m)Z) d dl*m+Z+m,Z, d dl-m.Z.m/Z/ d dl0m1Z1 dZ2dZ3 ejl                  d      jo                  ejp                          G d de!      Z9y# e4$ rZ5dZ2e5Z3Y dZ5[5EdZ5[5ww xY w)    )absolute_importdivisionprint_functiona  
author:
 - Ansible Networking Team (@ansible-network)
name: netconf
short_description: Provides a persistent connection using the netconf protocol
description:
- This connection plugin provides a connection to remote devices over the SSH NETCONF
  subsystem.  This connection plugin is typically used by network devices for sending
  and receiving RPC calls over NETCONF.
- Note this connection plugin requires ncclient to be installed on the local Ansible
  controller.
version_added: 1.0.0
requirements:
- ncclient
extends_documentation_fragment:
- ansible.netcommon.connection_persistent
options:
  host:
    description:
    - Specifies the remote device FQDN or IP address to establish the SSH connection
      to.
    default: inventory_hostname
    type: string
    vars:
    - name: inventory_hostname
    - name: ansible_host
  port:
    type: int
    description:
    - Specifies the port on the remote device that listens for connections when establishing
      the SSH connection.
    default: 830
    ini:
    - section: defaults
      key: remote_port
    env:
    - name: ANSIBLE_REMOTE_PORT
    vars:
    - name: ansible_port
  network_os:
    description:
    - Configures the device platform network operating system.  This value is used
      to load a device specific netconf plugin.  If this option is not configured
      (or set to C(auto)), then Ansible will attempt to guess the correct network_os
      to use. If it can not guess a network_os correctly it will use C(default).
    type: string
    vars:
    - name: ansible_network_os
  remote_user:
    description:
    - The username used to authenticate to the remote device when the SSH connection
      is first established.  If the remote_user is not specified, the connection will
      use the username of the logged in user.
    - Can be configured from the CLI via the C(--user) or C(-u) options.
    type: string
    ini:
    - section: defaults
      key: remote_user
    env:
    - name: ANSIBLE_REMOTE_USER
    vars:
    - name: ansible_user
  password:
    description:
    - Configures the user password used to authenticate to the remote device when
      first establishing the SSH connection.
    type: string
    vars:
    - name: ansible_password
    - name: ansible_ssh_pass
    - name: ansible_ssh_password
    - name: ansible_netconf_password
  private_key_file:
    description:
    - The private SSH key or certificate file used to authenticate to the remote device
      when first establishing the SSH connection.
    type: string
    ini:
    - section: defaults
      key: private_key_file
    env:
    - name: ANSIBLE_PRIVATE_KEY_FILE
    vars:
    - name: ansible_private_key_file
  look_for_keys:
    default: true
    description:
    - Enables looking for ssh keys in the usual locations for ssh keys (e.g. :file:`~/.ssh/id_*`).
    env:
    - name: ANSIBLE_PARAMIKO_LOOK_FOR_KEYS
    ini:
    - section: paramiko_connection
      key: look_for_keys
    type: boolean
  host_key_checking:
    description: Set this to "False" if you want to avoid host key checking by the
      underlying tools Ansible uses to connect to the host
    type: boolean
    default: true
    env:
    - name: ANSIBLE_HOST_KEY_CHECKING
    - name: ANSIBLE_SSH_HOST_KEY_CHECKING
    - name: ANSIBLE_NETCONF_HOST_KEY_CHECKING
    ini:
    - section: defaults
      key: host_key_checking
    - section: paramiko_connection
      key: host_key_checking
    vars:
    - name: ansible_host_key_checking
    - name: ansible_ssh_host_key_checking
    - name: ansible_netconf_host_key_checking
  proxy_command:
    default: ''
    description:
      - Proxy information for running the connection via a jumphost.
      - This requires ncclient >= 0.6.10 to be installed on the controller.
    type: string
    env:
      - name: ANSIBLE_NETCONF_PROXY_COMMAND
    ini:
      - {key: proxy_command, section: paramiko_connection}
    vars:
      - name: ansible_paramiko_proxy_command
      - name: ansible_netconf_proxy_command
  netconf_ssh_config:
    description:
    - This variable is used to enable bastion/jump host with netconf connection. If
      set to True the bastion/jump host ssh settings should be present in ~/.ssh/config
      file, alternatively it can be set to custom ssh configuration file path to read
      the bastion/jump host settings.
    type: string
    ini:
    - section: netconf_connection
      key: ssh_config
    env:
    - name: ANSIBLE_NETCONF_SSH_CONFIG
    vars:
    - name: ansible_netconf_ssh_config
N)AnsibleConnectionFailureAnsibleError)to_bytes	to_nativeto_text)missing_required_lib)BOOLEANS_FALSEBOOLEANS_TRUE)PY3)cPickle)PlayContext)ensure_connect)netconf_loader)NetworkConnectionBase)Version)__version__)manager)RPCError)AuthenticationErrorSSHUnknownHostError)to_eleto_xml)ProxyCommandTFncclientc                   r     e Zd ZdZdZdZ fdZd fd	Zd Ze	e
d               ZddZ fd	Z fd
Z xZS )
ConnectionzNetConf connectionszansible.netcommon.netconfFc           	         t        t        | 
  ||g|i | | j                  xs d| _        t	        j
                  | j                  |       | _        | j                  rwd| j                  j                  | j                  d| _        | j                  dd| j                  j                  d| j                  j                  d| j                         nOt	        j
                  d|       | _        dd| j                  d| _        | j                  dd	| j                  z         | j                  d
d| j                  z         d | _        d | _        d | _        y )Nautonetconf)typenameobjvvvvzloaded netconf plugin z from path z for network_os defaultzOunable to load netconf plugin for network_os %s, falling back to default pluginlogznetwork_os is set to %s)superr   __init___network_osr   getr"   
_load_name_sub_pluginqueue_message_original_path_managerkey_filename_ssh_config)selfplay_context	new_stdinargskwargs	__class__s        p/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/ansible/netcommon/plugins/connection/netconf.pyr*   zConnection.__init__   s8   j$(yR4R6R  ++5v%))$*:*:DA<<!//|| D
  LL++LL//$$	 *--i>DL!!|| D
 a""# 	5";d>N>N"NO     c                 ~   | j                   rAt        t        |d            }|y	 | j                   j                  |      }|j                  S t        t        | ;  |||      S # t        $ rO}| j                  t        t        |j                        d            }t        j                  |      cY d}~S d}~ww xY w)a  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.
        surrogate_or_stricterrorsNzunable to parse request)data)r1   r   r	   rpcr   internal_errorr
   r   xmljsondumpsdata_xmlr)   r   exec_command)	r4   cmdin_datasudoablerequestreplyexcerrorr9   s	           r:   rG   zConnection.exec_command   s     ==Ys3HIJG0)))'2 >>!T7WhOO  )++ 9NO ,  zz%((	)s   A$ $	B<-AB71B<7B<c                     t        |      }t        rt        j                  |d      }nt        j                  |      }t	               }|j                  |       || _        y)z7Updates the play context information for the connectionbytes)encodingN)r   r   r   loadsr   deserialize_play_context)r4   pc_datar5   s      r:   update_play_contextzConnection.update_play_context  sJ    7#mmGg>GmmG,G"}  ))r;   c                     | j                   S N)r1   )r4   s    r:   r   zConnection.manager  s     }}r;   c                 T   d }| j                  d      }d }|rt        t              dk  rt        dt        z        | j                  j
                  || j                  j                  d}|j                         D ]   \  }}|j                  |t        |            }" t        |      }|S )Nproxy_commandz0.6.10zConfiguring jumphost settings through ProxyCommand is unsupported in ncclient version %s. Please upgrade to ncclient 0.6.10 or newer.)z%hz%pz%r)
get_optionr   NCCLIENT_VERSIONr   rT   remote_addrremote_useritemsreplacestrr   )r4   portrZ   sock	replacersfindr`   s          r:   _get_proxy_commandzConnection._get_proxy_command  s     8'(83"BDTU  ((44((44I "+!2 Jg - 5 5dCL IJ.Dr;   c                    t         s&t        t        d      dt        t                    | j                  dd       d}| j                  j                  d}t        | j                  d|       | j                  j                  xs | j                  d      | _        | j                  r7t        t        j                  j                  | j                              | _        | j                  d	      | _        | j                   t"        v rd| _        n| j                   t$        v rd | _        | j&                  d
k(  rKt)        j*                  d      D ]2  }|j-                  |       }|s| j                  dd|z         || _        4 | j&                  d
k(  r| j                  dd       d| _        	 | j.                  j                  d      }| j                  dd|z         d|i}	 | j                  j2                  xs d}| j                  dd| j                  j4                  d|d| j                  j6                  d| j                          t9        | j                  j6                  || j                  j4                  | j                  j                  | j                  | j                  d      | j                  d      || j                  j:                  | j                  d      | j                         }| j=                  |      }|r||d<   t?        j@                  d'i || _!        | j                  d      | jB                  _"        | jB                  jR                  sy!| j                  dd"       d| _*        tV        tX        |           d#t]        | jB                  j^                  d$%      d&fS # t0        $ r d}Y w xY w# tF        $ r}	tI        t        |	            d }	~	wtJ        $ rD}	t        |	      jM                  d      r$t        djO                  | j                               d }	~	wtP        $ r% t        d jO                  | j&                              w xY w)(Nr   z: r(   z&ssh connection done, starting ncclientTFallow_agentprivate_key_filenetconf_ssh_configr!   )
class_onlyvvvzdiscovered network_os %sz7Unable to discover network_os. Falling back to default.r'   ncclient_device_handlerz'identified ncclient device handler: %s.r$   i>  z+ESTABLISH NETCONF SSH CONNECTION FOR USER: z	 on PORT z TO z WITH SSH_CONFIG = host_key_checkinglook_for_keyspersistent_connect_timeout)hostrb   usernamepasswordr2   hostkey_verifyro   device_paramsrh   timeout
ssh_configrc   persistent_command_timeoutFileNotFoundErrorzBEncountered FileNotFoundError in ncclient connect. Does {0} exist?z*connection=netconf is not supported on {0})   r;   s   not connectedz,ncclient manager object created successfullyr   r=   r>   r;    )0HAS_NCCLIENTr   r   r	   NCCLIENT_IMP_ERRr/   rT   rs   setattrri   r[   r2   ra   ospath
expanduserr3   r   r   r+   r   allguess_network_osr"   KeyErrorrb   r^   r]   dictrh   rf   r   connectr1   _timeoutr   r   r   
startswithformatImportError	connected
_connectedr)   r   _connectr   
session_id)r4   rh   cls
network_osrm   ru   rb   paramsrc   rM   r9   s             r:   r   zConnection._connect3  s    )4./  	5"JK&&2K""M;? ..?? 
4??D
  #BGG$6$6t7H7H$I JD??+?@},#D/#D v%%))T: 2 11$7
&&u.H:.UV'1D$	2 v%I  )D	0&*ll&=&=>W&X# 	58OO	
  !893	%%**1cD &&22&&22$$	 ''33++77++44!..#/BC"ooo>+ ..::(DE++F$ **40D!%v#OO5f5DM%)__5Q%RDMM"  }}&&+5"PQj$(* T]]--6KL
 	
M  	0&/#	0\ # 	;*9S>::" 	3x""#67"X__)) 
  	<CCDDTDTU 	s7   .N, #E#N> ,N;:N;>	QOQ'?P&&1Qc                 v    | j                   r| j                   j                          t        t        |           y rX   )r1   close_sessionr)   r   close)r4   r9   s    r:   r   zConnection.close  s'    ==MM'')j$%'r;   )NT)   )__name__
__module____qualname____doc__	transporthas_pipeliningr*   rG   rV   propertyr   r   rf   r   r   __classcell__)r9   s   @r:   r   r      sS    +IN' RP2	*   6{
z( (r;   r   ):
__future__r   r   r   r#   __metaclass__DOCUMENTATIONrD   loggingr   ansible.errorsr   r   ansible.module_utils._textr   r	   r
   ansible.module_utils.basicr   )ansible.module_utils.parsing.convert_boolr   r   ansible.module_utils.sixr   ansible.module_utils.six.movesr   ansible.playbook.play_contextr   ansible.plugins.connectionr   ansible.plugins.loaderr   Jansible_collections.ansible.netcommon.plugins.plugin_utils.connection_baser   Bansible_collections.ansible.netcommon.plugins.plugin_utils.versionr   r   r   r\   r   ncclient.operationsr   ncclient.transport.errorsr   r   ncclient.xml_r   r   paramikor   r|   r}   	Exceptionerr	getLoggersetLevelINFOr   r{   r;   r:   <module>r      s    A @ KZ   	 A C C ; S ( 2 5 5 1 W8 ,R,%L   *  & &w|| 4s(& s(  Ls   &,C	 	CCC