
    Vhk                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
Z
d dlmZ d dlmZmZmZmZmZmZmZ d Z G d	 d
e      Z G d de      Z G d de      Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functionaf  
module: iosxr_system
author:
- Peter Sprygada (@privateip)
- Kedar Kekan (@kedarX)
short_description: Module to manage the system attributes.
description:
- This module provides declarative management of node system attributes on Cisco IOS
  XR devices. It provides an option to configure host system parameters or remove
  those parameters from the device active configuration.
version_added: 1.0.0
requirements:
- ncclient >= 0.5.3 when using netconf
- lxml >= 4.1.1 when using netconf
extends_documentation_fragment:
- cisco.iosxr.iosxr
notes:
- This module works with connection C(network_cli) and C(netconf). See L(the IOS-XR
  Platform Options,../network/user_guide/platform_iosxr.html).
- name-servers I(state=absent) operation with C(netconf) transport is a success, but
  with rpc-error. This is due to XR platform issue. Recommended to use I(ignore_errors)
  option with the task as a workaround.
options:
  hostname:
    description:
    - Configure the device hostname parameter. This option takes an ASCII string value.
    type: str
  vrf:
    description:
    - VRF name for domain services
    type: str
    default: "default"
  domain_name:
    description:
    - Configure the IP domain name on the remote device to the provided value. Value
      should be in the dotted name form and will be appended to the C(hostname) to
      create a fully-qualified domain name.
    type: str
  domain_search:
    description:
    - Provides the list of domain suffixes to append to the hostname for the purpose
      of doing name resolution. This argument accepts a list of names and will be
      reconciled with the current active configuration on the running node.
    type: list
    elements: str
  lookup_source:
    description:
    - The C(lookup_source) argument provides one or more source interfaces to use
      for performing DNS lookups.  The interface provided in C(lookup_source) must
      be a valid interface configured on the device.
    type: str
  lookup_enabled:
    description:
    - Provides administrative control for enabling or disabling DNS lookups.  When
      this argument is set to True, lookups are performed and when it is set to False,
      lookups are not performed.
    type: bool
    default: true
  name_servers:
    description:
    - The C(name_serves) argument accepts a list of DNS name servers by way of either
      FQDN or IP address to use to perform name resolution lookups.  This argument
      accepts wither a list of DNS servers See examples.
    type: list
    elements: str
  state:
    description:
    - State of the configuration values in the device's current active configuration.  When
      set to I(present), the values should be configured in the device active configuration
      and when set to I(absent) the values should not be in the device active configuration
    default: present
    choices:
    - present
    - absent
    type: str
a  
- name: configure hostname and domain-name (default vrf=default)
  cisco.iosxr.iosxr_system:
    hostname: iosxr01
    domain_name: test.example.com
    domain_search:
      - ansible.com
      - redhat.com
      - cisco.com
- name: remove configuration
  cisco.iosxr.iosxr_system:
    hostname: iosxr01
    domain_name: test.example.com
    domain_search:
      - ansible.com
      - redhat.com
      - cisco.com
    state: absent
- name: configure hostname and domain-name with vrf
  cisco.iosxr.iosxr_system:
    hostname: iosxr01
    vrf: nondefault
    domain_name: test.example.com
    domain_search:
      - ansible.com
      - redhat.com
      - cisco.com
- name: configure DNS lookup sources
  cisco.iosxr.iosxr_system:
    lookup_source: MgmtEth0/0/CPU0/0
    lookup_enabled: true
- name: configure name servers
  cisco.iosxr.iosxr_system:
    name_servers:
      - 8.8.8.8
      - 8.8.4.4
a  
commands:
  description: The list of configuration mode commands to send to the device
  returned: always
  type: list
  sample:
    - hostname iosxr01
    - ip domain-name test.example.com
xml:
  description: NetConf rpc xml sent to device with transport C(netconf)
  returned: always (empty list when no xml rpc to send)
  type: list
  sample:
    - '<config xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
    <ip-domain xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ip-domain-cfg">
    <vrfs>
    <vrf>
    <vrf-name>default</vrf-name>
    <lists>
    <list xc:operation="merge">
    <order>0</order>
    <list-name>redhat.com</list-name>
    </list>
    </lists>
    </vrf>
    </vrfs>
    </ip-domain>
    </config>'
N)AnsibleModule)	build_xml
etree_findetree_findall
get_config
is_cliconf
is_netconfload_configc                 r    t        |       j                  |      }t        |      j                  |       }||fS N)set
difference)wanthaveaddsremovess       l/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/cisco/iosxr/plugins/modules/iosxr_system.py	diff_listr      s4    t9%D$i""4(G'?    c                       e Zd Zd Zd Zy)
ConfigBasec                 b    || _         dg d| _        t               | _        t               | _        y )NF)changedwarnings)_module_resultdict_want_have)selfmodules     r   __init__zConfigBase.__init__   s'    #(b9V
V
r   c           
         | j                   j                  | j                  j                  d   | j                  j                  d   | j                  j                  d   | j                  j                  d   | j                  j                  d   | j                  j                  d   | j                  j                  d   d       y )	Nhostnamevrfdomain_namedomain_searchlookup_sourcelookup_enabledname_servers)r'   r(   r)   r*   r+   r,   r-   )r!   updater   paramsr#   s    r   map_params_to_objzConfigBase.map_params_to_obj   s    

 LL//
;||**51#||22=A!%!4!4_!E!%!4!4_!E"&,,"5"56F"G $ 3 3N C
	
r   N)__name__
__module____qualname__r%   r1    r   r   r   r      s    
r   r   c                   B     e Zd Z fdZd Zd Zd Zd Zd Zd Z	 xZ
S )CliConfigurationc                 ,    t         t        |   |       y r   )superr7   r%   r#   r$   	__class__s     r   r%   zCliConfiguration.__init__   s    .v6r   c                 v    t               } j                  j                  d   } fd}|dk(  r	 j                  d   dk7  r|j	                  d        j                  d   r|j	                  d        j                  d	   r-|j	                  d
j                   j                  d	                 j                  d   s|j	                  d        j                  d   D ]"  }|j	                  dj                  |             $  j                  d   D ]"  }|j	                  dj                  |             $ n|dk(  r |d      r-|j	                  dj                   j                  d                 |d      r-|j	                  dj                   j                  d                 |d	      r-|j	                  dj                   j                  d	                d } j                  d   s j                  d   rd}n  j                  d   r j                  d   sd}||j	                  |        j                  d   wt         j                  d    j                  d         \  }}|D ]"  }|j	                  dj                  |             $ |D ]"  }|j	                  dj                  |             $  j                  d   wt         j                  d    j                  d         \  }}|D ]"  }|j	                  dj                  |             $ |D ]"  }|j	                  dj                  |             $ g  j                  d<   |ri j                  j                   }t         j                  ||      }	|	rt        |	       j                  d<   | j                  d<   d j                  d<   y y )Nstatec                     j                   j                  |       xr7 j                   j                  |       j                  j                  |       k7  S r   )r!   getr"   )xr#   s    r   needs_updatez:CliConfiguration.map_obj_to_commands.<locals>.needs_update   s:    ::>>!$Q$**..*;tzz~~a?P*PQr   absentr'   ioszno hostnamer)   zno domain namer+   z'no domain lookup source-interface {0!s}r,   zno domain lookup disabler-   zno domain name-server {0!s}r*   zno domain list {0!s}presentzhostname {0!s}zdomain name {0!s}z$domain lookup source-interface {0!s}domain lookup disablezdomain name-server {0!s}zdomain list {0!s}commands)commitprepareddiffTr   )listr   r/   r"   appendformatr!   r   r   
check_moder   r    )
r#   rF   r=   rA   itemcmdr   r   rG   rJ   s
   `         r   map_obj_to_commandsz$CliConfiguration.map_obj_to_commands   sg   6##G,	R Hzz*%..zz-( 01zz/*=DDTZZP_E`a ::./ :;

>2 L = D DT JKL

?3 E 6 = =d CDE iJ' 0 7 7

:8N OPM* 3 : :4::m;T UVO,:AA$**_B]^ C::./DJJ?O4P-,-djjAQ6R0$zz.)5 )$**^*DdjjQ_F` ag  MDOO$>$E$Ed$KLM# PDOO$A$H$H$NOP zz/*6 )$**_*EtzzRaGb cg  FDOO$7$>$>t$DEF# IDOO$:$A$A$$GHI $&Z 000Ft||XfED'+T':V$'/DLL$&*DLL# r   c                 t    t        j                  d|t         j                        }|r|j                  d      S y )Nz^hostname (\S+)   researchMgroupr#   configmatchs      r   parse_hostnamezCliConfiguration.parse_hostname  s/    		,fbdd;;;q>! r   c                 t    t        j                  d|t         j                        }|r|j                  d      S y )Nz^domain name (\S+)rS   rT   rY   s      r   parse_domain_namez"CliConfiguration.parse_domain_name  s/    		/>;;q>! r   c                 t    t        j                  d|t         j                        }|r|j                  d      S y )Nz%^domain lookup source-interface (\S+)rS   rT   rY   s      r   parse_lookup_sourcez$CliConfiguration.parse_lookup_source  s/    		BFBDDQ;;q>! r   c                 \   t        | j                        }| j                  j                  | j	                  |      | j                  |      t        j                  d|t        j                        | j                  |      d|vt        j                  d|t        j                        d       y )Nz^domain list (\S+)rE   z^domain name-server (\S+))r'   r)   r*   r+   r,   r-   )
r
   r   r"   r.   r\   r^   rU   findallrW   r`   )r#   rZ   s     r   map_config_to_objz"CliConfiguration.map_config_to_obj  s    DLL)

 //7#55f=!#,A6244!P!%!9!9&!A"9"G "

+GQSQUQU V		
r   c                 z    | j                          | j                          | j                          | j                  S r   )r1   rc   rQ   r   r0   s    r   runzCliConfiguration.run*  s0        "||r   )r2   r3   r4   r%   rQ   r\   r^   r`   rc   re   __classcell__r;   s   @r   r7   r7      s(    7A+F"
"
"

r   r7   c                   *     e Zd Z fdZd Zd Z xZS )NCConfigurationc                 X   t         t        |   |       t        j                         | _        t        j                         | _        t        j                         | _        t        j                         | _        t        j                         | _	        t        j                         | _
        y r   )r9   ri   r%   collectionsOrderedDict_system_meta_system_domain_meta_system_server_meta_hostname_meta_lookup_source_meta_lookup_metar:   s     r   r%   zNCConfiguration.__init__3  sx    ot-f5'335#.#:#:#< #.#:#:#< )557#.#:#:#< '335r   c                    ,  j                   j                  dddddfdddddfdd	dd
fdddddfg        j                  j                  dddddfdddddfdd	dd
fdddddfddddddfdddd
fdddd
fg        j                  j                  dddddfdddddfdd	dd
fdddddfddddddfdddd
fdddd
fg        j                  j                  dd dddfg        j
                  j                  dddddfdddddfdd	dd
fd!d"dddfg        j                  j                  dddddfdddddfdd	dd
fd#d$ddddfg        j                  j                  d%   }t        d&d'(      }t         j                  d)|*      }t        d+d'(      }t         j                  d)|*      }t        |d,      }||j                  nd }t        |d      }i }|D ]  }	t               }
t               }t        |	d-      }||j                  nd }t        |	d.      }||j                  nd }t        |	d/      }|D ]  }|j                  |j                          t        |	d0      }|D ]  }|
j                  |j                          t        |	d1      }||j                  nd }t        |	d#      d2nd}|||
||d3||<    d }i }i }i }t               }t               }t               }t               }t               }t               } i }!	 | j                   d      ,|d4k(  rrd5} ,fd6}" |"d7      r j                   d    j                   d7   d8} |"d9      rd9|i} j                   d:   s,d:   s j                   d   |d<    |"d;      r$ j                   d   |!d<    j                   d;   |!d;<    j                   d<   rki }# j                   d7   |#d7<    j                   d   |#d<   d=|#d><    j                   d<   D ].  }|,d<   v s||#d<<   |j                  |#j%                                0  j                   d?   ri }$ j                   d   |$d<   d=|$d><    j                   d?   D ].  }|,d?   v s||$d?<   |j                  |$j%                                0 n%|d@k(  rdA} ,fdB}" |"d7      r j                   d    j                   d7   d8} j                   d9   # j                   d9   |k7  rd9 j                   d9   i} j                   d:   s,d:   r j                   d   |d<    |"d;      r$ j                   d   |!d<    j                   d;   |!d;<    j                   d<   rt'         j                   d<   ,d<         \  }%}&i }# j                   d7   |#d7<    j                   d   |#d<   d=|#d><   |%D ].  }|,d<   vs||#d<<   |j                  |#j%                                0 |&D ].  }|,d<   v s||#d<<   |j                  |#j%                                0  j                   d?   rt'         j                   d?   ,d?         \  }'}(i }$ j                   d   |$d<   d=|$d><   |'D ].  }|,d?   vs||$d?<   |j                  |$j%                                0 |(D ].  }|,d?   v s||$d?<   | j                  |$j%                                0 g  j(                  dC<   t               })|r|r(|)j                  t        d+ j                  ||D             |r(|)j                  t        d& j                   ||D             |!r(|)j                  t        d& j
                  |!|D             |r(|)j                  t        d& j                  ||D             |d5k(  rw|r(|)j                  t        d& j                  ||D             |r|)j                  t        d& j                  ||D              j                   d   dEk7  rdFg j(                  dG<   n|dAk(  r|r(|)j                  t        d& j                  ||D             |r(|)j                  t        d& j                  |d5D             |r(|)j                  t        d& j                  ||D             | r(|)j                  t        d& j                  | d5D             d }*|)r1 j                  j*                   }+t-         j                  |)|+||H      }*|*rN j                  j.                  rt1        |*I       j(                  dJ<   |) j(                  dC<   d j(                  dK<   y y # t"        $ r d g g d dd3,Y $w xY w)LNvrfszip-domain/vrfsTedit)xpathtag	operationr(   zip-domain/vrfs/vrfza:vrfzip-domain/vrfs/vrf/vrf-name)rv   rx   za:domain_namezip-domain/vrfs/vrf/namerx   )rv   rx   attriblistszip-domain/vrfs/vrf/listsrK   zip-domain/vrfs/vrf/lists/list)rv   rw   rx   ry   za:orderz#ip-domain/vrfs/vrf/lists/list/orderza:domain_searchz'ip-domain/vrfs/vrf/lists/list/list-nameserverszip-domain/vrfs/vrf/serversserverz!ip-domain/vrfs/vrf/servers/serverz'ip-domain/vrfs/vrf/servers/server/orderza:name_serversz0ip-domain/vrfs/vrf/servers/server/server-addressz
a:hostnamezhost-names/host-nameza:lookup_sourcez#ip-domain/vrfs/vrf/source-interfacelookupzip-domain/vrfs/vrf/lookupr=   z	ip-domainfilter)opcoderunning)sourceconfig_filterz
host-namesz	host-namezvrf-namenamez	list-namezserver-addresszsource-interfaceF)r)   r*   r-   r+   r,   rB   deletec                 T    j                   |    d uxr j                   |    |    k(  S r   r!   r@   r#   sys_nodes    r   rA   z8NCConfiguration.map_obj_to_xml_rpc.<locals>.needs_update  s,    zz!}D0QTZZ]hqk5QQr   r)   )r(   r)   r'   r,   r+   r*   0orderr-   rD   mergec                 x    j                   |    d uxr' |    d u xs |    d uxr j                   |    |    k7  S r   r   r   s    r   rA   z8NCConfiguration.map_obj_to_xml_rpc.<locals>.needs_update  sQ    zz!}D0 QK4' R 4/PDJJqMXa[4Pr   xml)xmapr/   r   defaultzname-servers delete operation with non-default vrf is a success, but with rpc-error. Recommended to use 'ignore_errors' option with the task as a workaroundr   )rG   r   nc_get_filterrH   rJ   r   )rm   r.   rn   ro   rp   rq   rr   r   r/   r   r
   r   textr	   rK   rL   r!   KeyErrorcopyr   r   rN   r   _diffr    )-r#   r=   _get_filterr   hostname_runnhostname_eler'   vrf_elevrf_mapr(   name_server_listdomain_listvrf_name_elevrf_namedomain_name_eler)   
domain_eledomain
server_eler|   lookup_source_eler+   r,   r   hostname_paramlookup_paramsystem_paramsys_server_paramssys_domain_paramsadd_domain_paramsdel_domain_paramsadd_server_paramsdel_server_paramslookup_source_paramsrA   domain_paramserver_paramdomain_addsdomain_removesserver_addsserver_removes_edit_filter_listrJ   rG   r   s-   `                                           @r   map_obj_to_xml_rpcz"NCConfiguration.map_obj_to_xml_rpc<  sf     #3DvVW"6tRXYZ$APVWX#!:%+"-		
  	  ''#3DvVW"6tRXYZ$APVWX$>tZ`ab!@#%+"-	 &KZ`ab%GV\]	
. 	  ''#3DvVW"6tRXYZ$APVWX:4V\]
 !D#%+"-	 GV\]
 %!S%++	
@ 	"" !46U`a	
 	  ''#3DvVW"6tRXYZ$APVWX%!F%+"-		
  	  #3DvVW"6tRXYZ$APVWX!<#%+"-			
" ##G,H=T\\);WX>"4<<	Q\]!-=(4(@<$$d/ 	C#v&K%c:6L,8,D|(($H(f5O2A2M/..SWK&sK8J$ 0""6;;/0 's,<=J$ 5 ''45 !+30B C6G6S-22Y]M&0h&?&KUQUN  +!, 0!."0!GH-	<  F F F F F F!		tzz%01H HFR M*'+zz%'8TaIbcJ'",h!7::./AQ8R&*jj&7U#O,.2jj.?$U+8<

?8S$_5zz/*!.2jj.G]+&*jj&7U#(+W%"jj9 FF/!::8>_5)001B1B1DEF
 zz.)!&*jj&7U#(+W%"jj8 FF.!997=^4)001B1B1DEF
 iF M*'+zz%'8TaIbczz*%1djj6LPX6X",djj.D!E::./H=M4N&*jj&7U#O,.2jj.?$U+8<

?8S$_5zz/*.7JJ/_-/+^  ".2jj.G]+&*jj&7U#(+W%) FFXo%>>8>_5)001B1B1DEF - FF/!::8>_5)001B1B1DEF
 zz.).7JJ~.^,/+^  "&*jj&7U#(+W%) FFXn%==7=^4)001B1B1DEF - FF.!997=^4)001B1B1DEF
 !U F!(($!00-%	 !((#!..+%	 $!((#!553%	 !((#!..+%	 !$%,,!'!%!9!9#4#)	 %%,,!'!%!9!9#4#)	 zz%(I5z4Z0 7"$%,,!'!%!9!9#4#)	 %%,,!'!%!9!9#4#+	 %%,,!'!%!9!9#4#)	 %%,,!'!%!9!9#4#+	 000F!)D ||!!'+T':V$"3DLL&*DLL# y  	#!# "!%"&H	s   d8 8eec                 Z    | j                          | j                          | j                  S r   )r1   r   r   r0   s    r   re   zNCConfiguration.run  s$     !||r   )r2   r3   r4   r%   r   re   rf   rg   s   @r   ri   ri   2  s    6J+Xr   ri   c                     t        t        d      t        dd      t        d      t        dd      t        dd      t        d      t        dd      t        d	d
gd	            } t        | d      }d}t        |      rt        |      }nt	        |      rt        |      }d}|r|j                         } |j                  di | y)z-Main entry point for Ansible module executionstr)typer   )r   r   rK   )r   elementsboolTrD   rB   )choicesr   )r'   r(   r)   r*   r-   r+   r,   r=   )argument_specsupports_check_modeNr5   )r    r   r   r7   r   ri   re   	exit_json)r   r$   config_objectresults       r   mainr     s    5!eY/e$7v6&6Ix0)D	M DQFM&(0	F	'/F""$Fvr   __main__)
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURNrk   rU   ansible.module_utils.basicr   Hansible_collections.cisco.iosxr.plugins.module_utils.network.iosxr.iosxrr   r   r	   r
   r   r   r   r   objectr   r7   ri   r   r2   r5   r   r   <module>r      s   
 A @ KZ$L
<  	 4  
 
*hz hVZj Zz8 zF r   