
    Vh/                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
mZ d dlmZ d dlmZmZ dad	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona	  
module: ios_system
author: Peter Sprygada (@privateip)
short_description: Module to manage the system attributes.
description:
  - This module provides declarative management of node system attributes on Cisco IOS
    devices.  It provides an option to configure host system parameters or remove those
    parameters from the device active configuration.
version_added: 1.0.0
extends_documentation_fragment:
  - cisco.ios.ios
notes:
  - Tested against Cisco IOSXE Version 17.3 on CML.
  - This module works with connection C(network_cli).
    See U(https://docs.ansible.com/ansible/latest/network/user_guide/platform_ios.html)
options:
  hostname:
    description:
      - Configure the device hostname parameter. This option takes an ASCII string value.
    type: str
  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: list
    elements: raw
  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: raw
  lookup_source:
    description:
      - 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:
      - 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
  name_servers:
    description:
      - List of DNS name servers by IP address to use to perform name resolution lookups.  This
        argument accepts either a list of DNS servers See examples.
    type: list
    elements: raw
  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
  cisco.ios.ios_system:
    hostname: ios01
    domain_name: test.example.com
    domain_search:
      - ansible.com
      - redhat.com
      - cisco.com

- name: Remove configuration
  cisco.ios.ios_system:
    state: absent

- name: Configure DNS lookup sources
  cisco.ios.ios_system:
    lookup_source: MgmtEth0/0/CPU0/0
    lookup_enabled: true

- name: Configure name servers
  cisco.ios.ios_system:
    name_servers:
      - 8.8.8.8
      - 8.8.4.4
z
commands:
  description: The list of configuration mode commands to send to the device
  returned: always
  type: list
  sample:
    - hostname ios01
    - ip domain name test.example.com
N)AnsibleModule)ComplexList)
get_configload_configc                 p    t         |t         v S t        |       }t        j                  d|      a |t         v S )Nzvrf definition (\S+))_CONFIGURED_VRFSr   refindall)modulevrfconfigs      h/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/cisco/ios/plugins/modules/ios_system.pyhas_vrfr      s<    #&&&Fzz"96B"""    c                 H    t        | |      s| j                  d|z         y y )Nzvrf %s is not configured)msg)r   	fail_json)r   r   s     r   requires_vrfr      s(    637#=>  r   c                 n    | D cg c]	  }||vs| }}|D cg c]	  }|| vs| }}||fS c c}w c c}w N )wanthavewaddshremovess         r   	diff_listr!      sF    -!q}A-D-0Q!4-q0G0= .0s   	--	22c                 |    t               }|j                  d   } fd}|dk(  rd   dk7  r|j                  d       d   r|j                  dd   z         d	   d
u r|j                  d       t               }d   D ]a  }|d   r3|d   |vr,|j	                  |d          |j                  d|d   z         ;d |vs@|j	                  d        |j                  d       c t               }d   D ]a  }|d   r3|d   |vr,|j	                  |d          |j                  d|d   z         ;d |vs@|j	                  d        |j                  d       c t               }d   D ]a  }|d   r3|d   |vr,|j	                  |d          |j                  d|d   z         ;d |vs@|j	                  d        |j                  d       c |S |dk(  rf |d      r|j                  d d   z          |d      r|j                  d d   z          |d	      rd} d	   d
u rd|z  }|j                  |        d   rt         d   d         \  }	}
|
D ]<  }|d   r|j                  d|d   d|d          &|j                  d|d   z         > |	D ]K  }|d   r-t        ||d          |j                  d|d   d|d          5|j                  d|d   z         M  d   rt         d   d         \  }	}
|
D ]<  }|d   r|j                  d |d   d|d          &|j                  d!|d   z         > |	D ]K  }|d   r-t        ||d          |j                  d"|d   d|d          5|j                  d#|d   z         M  d   rt         d   d         \  }	}
|
D ]<  }|d   r|j                  d$|d   d|d%          &|j                  d&|d%   z         > |	D ]K  }|d   r-t        ||d          |j                  d'|d   d|d%          5|j                  d(|d%   z         M |S ))Nstatec                 t    j                  |       d uxr# j                  |       j                  |       k7  S r   )get)xr   r   s    r   needs_updatez)map_obj_to_commands.<locals>.needs_update   s0    xx{$&E488A;$((1++EEr   absenthostnameRouterzno hostnamelookup_sourcez'no ip domain lookup source-interface %slookup_enabledFzip domain lookupdomain_namer   zno ip domain name vrf %szno ip domain namedomain_searchzno ip domain list vrf %szno ip domain listname_serverszno ip name-server vrf %szno ip name-serverpresentzhostname %sz$ip domain lookup source-interface %szno %szno ip domain name vrf  namezno ip domain name %szip domain name vrf zip domain name %szno ip domain list vrf zno ip domain list %szip domain list vrf zip domain list %szno ip name-server vrf serverzno ip name-server %szip name-server vrf zip name-server %s)listparamsappendsetaddr!   r   )r   r   r   commandsr#   r'   vrfsitemcmdr   r    s   ``         r   map_obj_to_commandsr=      s   vHMM'"EF 
x'OOM* OOE_H]]^ !U*OO./u' 	5DE{tE{$6e% :T%[ HIT! 34	5 u) 	5DE{tE{$6e% :T%[ HIT! 34	5 u( 	5DE{tE{$6e% :T%[ HIT! 34	5p Oc 
)	
#OOMD,<<=(OOBT/EZZ[()$C$%.mOOC %d=&94;NOMD' K;OOT%[RVW]R^$_`OO$:T&\$IJ	K
  H; e5OO$u+tTZ|$\]OO$7$v,$FGH  %d?&;T/=RSMD' K;OOT%[RVW]R^$_`OO$:T&\$IJ	K
  H; e5OO$u+tTZ|$\]OO$7$v,$FGH %d>&:D<PQMD' M;OOT%[RVW_R`$abOO$:T(^$KL	M
  J; e5OO$u+tT\~$^_OO$7$x.$HIJ Or   c                 n    t        j                  d| t         j                        }|j                  d      S )Nz^hostname (\S+)   r   searchMgroupr   matchs     r   parse_hostnamerF      s'    II(&"$$7E;;q>r   c                     t        j                  d| t         j                        }t               }|D ]  \  }}|sd }|j	                  ||d        |S )Nz'^ip domain[- ]name (?:vrf (\S+) )*(\S+)r2   r   r   r   rB   r4   r6   r   rE   matchesr   r2   s        r   parse_domain_namerL      S    JJBFBDDQEfG 3	TCS123 Nr   c                     t        j                  d| t         j                        }t               }|D ]  \  }}|sd }|j	                  ||d        |S )Nz'^ip domain[- ]list (?:vrf (\S+) )*(\S+)rH   rI   rJ   s        r   parse_domain_searchrO     rM   r   c                     t        j                  d| t         j                        }t               }|D ]2  \  }}|sd }|j	                         D ]  }|j                  ||d        4 |S )Nz#^ip name-server (?:vrf (\S+) )*(.*)r3   r   )r   r   rB   r4   splitr6   )r   rE   rK   r   serversr3   s         r   parse_name_serversrT     sh    JJ=vrttLEfG ;WCmmo 	;FNNfS9:	;;
 Nr   c                 t    t        j                  d| t         j                        }|r|j                  d      S y )Nz*ip domain[- ]lookup source-interface (\S+)r?   r@   rD   s     r   parse_lookup_sourcerV     s/    IICVRTTRE{{1~ r   c                     t        |       }t        |      t        |      t        |      t	        |      d|vxr d|vt        |      dS )Nzno ip domain lookupzno ip domain-lookup)r)   r-   r.   r+   r,   r/   )r   rF   rL   rO   rV   rT   )r   r   s     r   map_config_to_objrX   "  sS    F"6*(0,V4,V4/v= 0!/*62 r   c                    | j                   d   | j                   d   | j                   d   d}t        t        t        d      t                     |       }t        t        t        d      t                     |       }t        t        t        d      t                     |       }d	|fd
|fd|ffD ]2  \  }}| j                   |   r || j                   |         ||<   .d ||<   4 |S )Nr)   r+   r,   )r)   r+   r,   T)keyrH   rQ   r-   r.   r/   )r5   r   dict)r   objr-   r.   r/   argcasts          r   map_params_to_objr_   /  s    MM*-7 --(89C
 dDFCVLK$4.df EvNMt4D>tvFOL	$	-(	& 	T
 ==FMM#./CHCH Jr   c                     t        t               t        dd      t        dd      t        dd      t               t        d      t        ddgd      	      } t        | d
      }ddi}t               }||d<   t        |      }t	        |      }t        |||      }||d<   |r|j                  st        ||       d
|d<    |j                  di | y)z-Main entry point for Ansible module executionr4   raw)typeelementsbool)rb   r0   r(   )choicesdefault)r)   r-   r.   r/   r+   r,   r#   T)argument_specsupports_check_modechangedFwarningsr9   Nr   )	r[   r   r4   r_   rX   r=   
check_moder	   	exit_json)rg   r   resultrj   r   r   r9   s          r   mainrn   D  s    fu57v6f(Ix0)DM DQFFvH!F:V$DV$D"4v6H!F:  ) yFvr   __main__)
__future__r   r   r   rb   __metaclass__DOCUMENTATIONEXAMPLESRETURNr   ansible.module_utils.basicr   Oansible_collections.ansible.netcommon.plugins.module_utils.network.common.utilsr   Bansible_collections.cisco.ios.plugins.module_utils.network.ios.iosr   r	   r   r   r   r!   r=   rF   rL   rO   rT   rV   rX   r_   rn   __name__r   r   r   <module>ry      s   $ A @ >@4
 
 4  #?
Wt

*4 zF r   