
    Vh9                         d dl mZmZmZ eZdZdZd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mZ d d
lmZmZmZ 	 d dlZdZd Zedk(  r e        yy# e$ r	 d dlm
Z
 Y Kw xY w# e$ r dZY +w xY w)    )absolute_importdivisionprint_functiona^  
module: netconf_get
author:
  - Ganesh Nalawade (@ganeshrn)
  - Sven Wisotzky (@wisotzky)
short_description: Fetch configuration/state data from NETCONF enabled network devices.
description:
  - NETCONF is a network management protocol developed and standardized by the IETF.
    It is documented in RFC 6241.
  - This module allows the user to fetch configuration and state data from NETCONF enabled
    network devices.
version_added: 1.0.0
extends_documentation_fragment:
  - ansible.netcommon.network_agnostic
options:
  source:
    description:
      - This argument specifies the datastore from which configuration data should be
        fetched. Valid values are I(running), I(candidate) and I(startup). If the C(source)
        value is not set both configuration and state information are returned in response
        from running datastore.
    type: str
    choices:
      - running
      - candidate
      - startup
  filter:
    description:
      - This argument specifies the string which acts as a filter to restrict the
        portions of the data to be are retrieved from the remote device. If this option
        is not specified entire configuration or state data is returned in result depending
        on the value of C(source) option. The C(filter) value can be either XML string
        or XPath or JSON string or native python dictionary, if the filter is in XPath
        format the NETCONF server running on remote host should support xpath capability
        else it will result in an error. If the filter is in JSON format the xmltodict library
        should be installed on the control node for JSON to XML conversion.
    type: raw
  display:
    description:
      - Encoding scheme to use when serializing output from the device. The option I(json)
        will serialize the output as JSON data. If the option value is I(json) it requires
        jxmlease to be installed on control node. The option I(pretty) is similar to
        received XML response but is using human readable format (spaces, new lines).
        The option value I(xml) is similar to received XML response but removes all
        XML namespaces.
    type: str
    choices:
      - json
      - pretty
      - xml
      - native
  lock:
    description:
      - Instructs the module to explicitly lock the datastore specified as C(source).
        If no I(source) is defined, the I(running) datastore will be locked. By setting
        the option value I(always) is will explicitly lock the datastore mentioned in
        C(source) option. By setting the option value I(never) it will not lock the
        C(source) datastore. The value I(if-supported) allows better interworking with
        NETCONF servers, which do not support the (un)lock operation for all supported
        datastores.
    type: str
    default: never
    choices:
      - never
      - always
      - if-supported
requirements:
  - ncclient (>=v0.5.2)
  - jxmlease (for display=json)
  - xmltodict (for display=native)
notes:
  - This module requires the NETCONF system service be enabled on the remote device
    being managed.
  - This module supports the use of connection=netconf
a  
- name: Get running configuration and state data
  ansible.netcommon.netconf_get:

- name: Get configuration and state data from startup datastore
  ansible.netcommon.netconf_get:
    source: startup

- name: Get system configuration data from running datastore state (junos)
  ansible.netcommon.netconf_get:
    source: running
    filter: <configuration><system></system></configuration>

- name: Get configuration and state data in JSON format
  ansible.netcommon.netconf_get:
    display: json

- name: get schema list using subtree w/ namespaces
  ansible.netcommon.netconf_get:
    display: json
    filter: <netconf-state xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring"><schemas><schema/></schemas></netconf-state>
    lock: never

- name: get schema list using xpath
  ansible.netcommon.netconf_get:
    display: xml
    filter: /netconf-state/schemas/schema

- name: get interface configuration with filter (iosxr)
  ansible.netcommon.netconf_get:
    display: pretty
    filter: <interface-configurations xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg"></interface-configurations>
    lock: if-supported

- name: Get system configuration data from running datastore state (junos)
  ansible.netcommon.netconf_get:
    source: running
    filter: <configuration><system></system></configuration>
    lock: if-supported

- name: Get complete configuration data from running datastore (SROS)
  ansible.netcommon.netconf_get:
    source: running
    filter: <configure xmlns="urn:nokia.com:sros:ns:yang:sr:conf"/>

- name: Get complete state data (SROS)
  ansible.netcommon.netconf_get:
    filter: <state xmlns="urn:nokia.com:sros:ns:yang:sr:state"/>

- name: "get configuration with json filter string and native output (using xmltodict)"
  netconf_get:
    filter: |
      {
          "interface-configurations": {
              "@xmlns": "http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg",
              "interface-configuration": null
          }
      }
    display: native

- name: Define the Cisco IOSXR interface filter
  set_fact:
    filter:
      interface-configurations:
        "@xmlns": "http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg"
        interface-configuration: null

- name: "get configuration with native filter type using set_facts"
  ansible.netcommon.netconf_get:
    filter: "{{ filter }}"
    display: native
  register: result

- name: "get configuration with direct native filter type"
  ansible.netcommon.netconf_get:
    filter:
      {
        "interface-configurations":
          {
            "@xmlns": "http://cisco.com/ns/yang/Cisco-IOS-XR-ifmgr-cfg",
            "interface-configuration": null,
          },
      }
    display: native
  register: result

# Make a round-trip interface description change, diff the before and after
# this demonstrates the use of the native display format and several utilities
# from the ansible.utils collection

- name: Define the openconfig interface filter
  set_fact:
    filter:
      interfaces:
        "@xmlns": "http://openconfig.net/yang/interfaces"
        interface:
          name: Ethernet2

- name: Get the pre-change config using the filter
  ansible.netcommon.netconf_get:
    source: running
    filter: "{{ filter }}"
    display: native
  register: pre

- name: Update the description
  ansible.utils.update_fact:
    updates:
      - path: pre.output.data.interfaces.interface.config.description
        value: "Configured by ansible {{ 100 | random }}"
  register: updated

- name: Apply the new configuration
  ansible.netcommon.netconf_config:
    content:
      config:
        interfaces: "{{ updated.pre.output.data.interfaces }}"

- name: Get the post-change config using the filter
  ansible.netcommon.netconf_get:
    source: running
    filter: "{{ filter }}"
    display: native
  register: post

- name: Show the differences between the pre and post configurations
  ansible.utils.fact_diff:
    before: "{{ pre.output.data|ansible.utils.to_paths }}"
    after: "{{ post.output.data|ansible.utils.to_paths }}"
# TASK [Show the differences between the pre and post configurations] ********
# --- before
# +++ after
# @@ -1,11 +1,11 @@
#  {
# -    "@time-modified": "2020-10-23T12:27:17.462332477Z",
# +    "@time-modified": "2020-10-23T12:27:21.744541708Z",
#      "@xmlns": "urn:ietf:params:xml:ns:netconf:base:1.0",
#      "interfaces.interface.aggregation.config['fallback-timeout']['#text']": "90",
#      "interfaces.interface.aggregation.config['fallback-timeout']['@xmlns']": "http://arista.com/yang/openconfig/interfaces/augments",
#      "interfaces.interface.aggregation.config['min-links']": "0",
#      "interfaces.interface.aggregation['@xmlns']": "http://openconfig.net/yang/interfaces/aggregate",
# -    "interfaces.interface.config.description": "Configured by ansible 56",
# +    "interfaces.interface.config.description": "Configured by ansible 67",
#      "interfaces.interface.config.enabled": "true",
#      "interfaces.interface.config.mtu": "0",
#      "interfaces.interface.config.name": "Ethernet2",
a  
stdout:
  description: The raw XML string containing configuration or state data
    received from the underlying ncclient library.
  returned: always apart from low-level errors (such as action plugin)
  type: str
  sample: "..."
stdout_lines:
  description: The value of stdout split into a list
  returned: always apart from low-level errors (such as action plugin)
  type: list
  sample: ["...", "..."]
output:
  description:
    Based on the value of display option will return either the set of
    transformed XML to JSON format from the RPC response with type dict
    or pretty XML string response (human-readable) or response with
    namespace removed from XML string.
  returned:
    If the display format is selected as I(json) it is returned as dict type
    and the conversion is done using jxmlease python library. If the display
    format is selected as I(native) it is returned as dict type and the conversion
    is done using xmltodict python library. If the display format is xml or pretty
    it is returned as a string apart from low-level errors (such as action plugin).
  type: complex
  contains:
    formatted_output:
      description:
        - Contains formatted response received from remote host as per the value in display format.
      type: str
)tostring)to_text)AnsibleModule)remove_namespaces)getget_capabilities
get_config)dict_to_xmlvalidate_and_normalize_dataxml_to_dictNTFc            
      d   t        t        g d      t        d      t        g d      t        dg d      	      } t        | d
      }t        |      }|d   }|j                  d   }|j                  d   }	 t	        |      \  }}dk(  rd}nM|dk(  r	 t              }d}n9|dk(  rn3|d}|j                  d       nn|r|j                  d|d|       |j                  d   }	|j                  d   }
|dk(  r$|j                  dd      s|j                  d       |dk(  r$|j                  dd      s|j                  d        |dk(  r'|j                  d!d      s|j                  d"|z         |	dk(  rd}n8|xs d#|j                  d$g       v rd
}n|j                  d%|xs d#z         |	d&k(  }|
dk(  rt        s|j                  d'       |r||fnd}|t        ||||      }nt        |||      }t        t        |            }d}|
dk(  rt        |      }nI|
dk(  r	 t        j                   |      }n-|
d(k(  rt        t        |d
)            }n|
d*k(  r	 t%        |      }||d+} |j&                  d,i | y# t
        $ r&}|j                  t        |             Y d}~&d}~ww xY w# t
        $ r&}|j                  t        |             Y d}~?d}~ww xY w# t
        $ r t#        |      w xY w# t
        $ r%}|j                  t        |             Y d}~d}~ww xY w)-z entry point for module execution)running	candidatestartup)choicesraw)type)jsonprettyxmlnativenever)r   alwayszif-supported)defaultr   )sourcefilterdisplaylockT)argument_specsupports_check_modedevice_operationsr   r   )msgNr   subtreer   xpathzUThe data format of filter option value couldn't be identified, hence set to 'subtree'zInvalid filter type detected z for filter value r!   r    r   supports_commitFz0candidate source is not supported on this devicer   supports_startupz.startup source is not supported on this devicesupports_xpathz?filter value '%s' of type xpath is not supported on this devicer   lock_datastorez=lock operation on '%s' source is not supported on this devicer   zjxmlease is required to display response in json formatbut does not appear to be installed. It can be installed using `pip install jxmlease`r   )pretty_printr   )stdoutoutput )dictr   r   paramsr   	Exception	fail_jsonr   r   warnr
   HAS_JXMLEASEr   r   r	   jxmleaseparse
ValueErrorr   	exit_json)r"   modulecapabilities
operationsr   r   filter_datafilter_typeexcr!   r    execute_lockfilter_specresponsexml_respr.   results                    q/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/ansible/netcommon/plugins/modules/netconf_get.pymainrF   ,  sw   ?@@A'+NO	M DQF#F+L12J]]8$F]]8$F+#>v#F [ e			/ -F  				" $KKKg 	ITV\] 	 	
 == DmmI&GZ^^4Eu%MOP:>>2De#LMNgjnn5Eu&MQTZZ 	 	
 w

I*..1A2"F	F 	KvObYbc	
 x'&? 	 	
 ,7;'DKffk<Hv{L9x)*HF%"8,	F		'^^H-F 
H	(>?	H		/ *F !F3FFvy  +WS\**+  	/..	/P  	'X&&	'  	/..	/sT   ;J J7 *K) "L 	J4J//J47	K& K!!K&)K>	L/
L**L/__main__)
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURN
lxml.etreer   ImportErrorxml.etree.ElementTreeansible.module_utils._textr   ansible.module_utils.basicr   Qansible_collections.ansible.netcommon.plugins.module_utils.network.common.netconfr	   Ransible_collections.ansible.netcommon.plugins.module_utils.network.netconf.netconfr
   r   r   Eansible_collections.ansible.netcommon.plugins.module_utils.utils.datar   r   r   r6   r5   rF   __name__r/       rE   <module>rW      s    A @ JXRh
>/# / 4 
 L
od zF _  /./2  Ls#   A A) A&%A&)A32A3