B
    `3                 @   s   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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add Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Z e!d kre   dS )!    )absolute_importdivisionprint_functiona0	  
module: nxos_system
extends_documentation_fragment:
- cisco.nxos.nxos
author: Peter Sprygada (@privateip)
short_description: Manage the system attributes on Cisco NXOS devices
description:
- This module provides declarative management of node system attributes on Cisco NXOS
  devices.  It provides an option to configure host system parameters or remove those
  parameters from the device active configuration.
version_added: 1.0.0
options:
  hostname:
    description:
    - Configure the device hostname parameter. This option takes an ASCII string value
      or keyword 'default'
    type: str
  domain_name:
    description:
    - Configures the default domain name suffix to be used when referencing this node
      by its FQDN.  This argument accepts either a list of domain names or a list
      of dicts that configure the domain name and VRF name or keyword 'default'. See
      examples.
    type: list
    elements: raw
  domain_lookup:
    description:
    - Enables or disables the DNS lookup feature in Cisco NXOS.  This argument accepts
      boolean values.  When enabled, the system will try to resolve hostnames using
      DNS and when disabled, hostnames will not be resolved.
    type: bool
  domain_search:
    description:
    - Configures a list of domain name suffixes to search when performing DNS name
      resolution. This argument accepts either a list of domain names or a list of
      dicts that configure the domain name and VRF name or keyword 'default'. See
      examples.
    type: list
    elements: raw
  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 or a list of hashes that configure
      the name server and VRF name or keyword 'default'. See examples.
    type: list
    elements: raw
  system_mtu:
    description:
    - Specifies the mtu, must be an integer or keyword 'default'.
    type: 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
  cisco.nxos.nxos_system:
    hostname: nxos01
    domain_name: test.example.com

- name: remove configuration
  cisco.nxos.nxos_system:
    state: absent

- name: configure name servers
  cisco.nxos.nxos_system:
    name_servers:
    - 8.8.8.8
    - 8.8.4.4

- name: configure name servers with VRF support
  cisco.nxos.nxos_system:
    name_servers:
    - {server: 8.8.8.8, vrf: mgmt}
    - {server: 8.8.4.4, vrf: mgmt}
z
commands:
  description: The list of configuration mode commands to send to the device
  returned: always
  type: list
  sample:
    - hostname nxos01
    - ip domain-name test.example.com
N)
get_configload_config)nxos_argument_spec)AnsibleModule)	iteritems)NetworkConfig)ComplexListc             C   s,   t d k	r|t kS t| }td|a |t kS )Nzvrf context (\S+))_CONFIGURED_VRFSr   refindall)modulevrfconfig r   i/home/dcms/DCMS/lib/python3.7/site-packages/ansible_collections/cisco/nxos/plugins/modules/nxos_system.pyhas_vrf   s
    r   c       
         s   t  }jd } fdd}dd }d$ddd%fdd		}|d
kr d rZ|d x, d D ] }d|d  }	|	||d  qdW x, d D ] }d|d  }	|	||d  qW x, d D ] }d|d  }	|	||d  qW  d r|d |dkr|dr@d dkr. d r@|d n|dd   dd k	r ddkrd}	d dkrd|	 }	||	 d rPdd d dkr d rPx d D ]"}d|d  }	|	||d  qW nhx2| dD ]"}d|d  }	|	||d  qW x2| dD ]"}d |d  }	||	||d  q*W d rdd d dkr d rx d D ]"}d|d  }	|	||d  qW nhx2| dD ]"}d|d  }	|	||d  qW x2| dD ]"}d!|d  }	||	||d  qW d r܈dd d dkrt d rx d D ]"}d|d  }	|	||d  qLW nhx2| dD ]"}d|d  }	|	||d  qW x2| dD ]"}d"|d  }	||	||d  qW |drd dkr
 d r|d n|d#d   |S )&Nstatec                s    | o |   | kS )N)get)x)havewantr   r   needs_update   s    z)map_obj_to_commands.<locals>.needs_updatec                s    fdd|  D S )Nc                s   g | ]}|  kr|qS r   r   ).0item)yzr   r   
<listcomp>   s    z;map_obj_to_commands.<locals>.difference.<locals>.<listcomp>r   )r   r   r   r   )r   r   r   
difference   s    z'map_obj_to_commands.<locals>.differencec             S   s.   |r| d|  | |  |r*| d d S )Nzvrf context %sexit)append)cmdcommandsr   r   r   r   remove   s
    
z#map_obj_to_commands.<locals>.removec                s*   |rt  |s jd| d | ||S )Nzinvalid vrf name %s)msg)r   Z	fail_json)r#   r$   r   )r   r%   r   r   add   s    
z map_obj_to_commands.<locals>.addabsenthostnamezno hostnamedomain_namezno ip domain-name %snamer   domain_searchzno ip domain-list %sname_serverszno ip name-server %sserver
system_mtuzno system jumbomtupresentdefaultzhostname %sdomain_lookupzip domain-lookupFzno %sr   zip domain-name %szip domain-list %szip name-server %szsystem jumbomtu %s)N)N)listparamsr"   r   )
r   r   r   r$   r   r   r    r'   r   r#   r   )r   r   r%   r   r   map_obj_to_commands   s    















r5   c             C   s"   t d| t j}|r|dS d S )Nz^hostname (\S+)   )r   searchMgroup)r   matchr   r   r   parse_hostname   s    r;   c             C   st   t  }td| tj}|r0||dd d x>t|D ]2\}}td|tj}|r:||d|d q:W |S )Nz^ip domain-name (\S+)r6   )r+   r   zip domain-name (\S+))r3   r   r7   r8   r"   r9   r	   )r   
vrf_configobjectsr:   r   cfgr   r   r   parse_domain_name  s    r?   c             C   st   t  }x(td| tjD ]}||d d qW x>t|D ]2\}}x(td|tjD ]}|||d qTW q:W |S )Nz^ip domain-list (\S+))r+   r   zip domain-list (\S+))r3   r   r   r8   r"   r	   )r   r<   r=   r   r   r>   r   r   r   parse_domain_search  s    r@   c       	      C   s   t  }td| tj}|rRd|dkrRx(|ddD ]}||d d q:W xRt|D ]F\}}td|tj}|r\x(|ddD ]}|||d qW q\W |S )Nz^ip name-server (.+)$zuse-vrfr6    )r.   r   zip name-server (.+))r3   r   r7   r8   r9   splitr"   r	   )	r   r<   vrfsr=   r:   addrr   r>   Z	vrf_matchr   r   r   parse_name_servers!  s    rE   c             C   s"   t d| t j}|r|dS d S )Nz^system jumbomtu (\d+)r6   )r   r7   r8   r9   )r   r:   r   r   r   parse_system_mtu2  s    rF   c             C   s   t | }td|d}i }td|tj}x&|D ]}|jd| gd}|||< q.W t|d|kt||t||t	|||t
|dS )N   )indentcontentsz^vrf context (\S+)$zvrf context %s)pathzno ip domain-lookup)r)   r2   r*   r,   r-   r/   )r   r
   r   r   r8   Zget_block_configr;   r?   r@   rE   rF   )r   r   Z	configobjr<   rC   r   Zconfig_datar   r   r   map_config_to_obj8  s    

rK   c             C   s   | j d | j d | j d d}tttddt d| }tttddt d| }tttddt d| }xLd	|fd
|fd|fgD ]2\}}| j | d k	r|| j | ||< qd ||< qW |S )Nr)   r2   r/   )r)   r2   r/   T)key)r+   r   )r.   r   r*   r,   r-   )r4   r   dict)r   objr*   r,   r-   argcastr   r   r   map_params_to_objM  s    rQ   c              C   s   t t  t ddt dddt dddt dddt ddt dddgd	d
} | t t| dd}t }ddi}|rx||d< t|}t|}t|||}||d< |r|jst	|| d|d< |j
f | dS )z+ main entry point for module execution
    bool)typer3   raw)rS   elementsstrr0   r(   )r1   choices)r)   r2   r*   r,   r-   r/   r   T)argument_specZsupports_check_modechangedFwarningsr$   N)rM   updater   r   r3   rQ   rK   r5   Z
check_moder   Z	exit_json)rX   r   rZ   resultr   r   r$   r   r   r   maing  s0    





r]   __main__)"
__future__r   r   r   rS   __metaclass__ZDOCUMENTATIONZEXAMPLESZRETURNr   ZEansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxosr   r   r   Zansible.module_utils.basicr   Zansible.module_utils.sixr	   ZPansible_collections.ansible.netcommon.plugins.module_utils.network.common.configr
   ZOansible_collections.ansible.netcommon.plugins.module_utils.network.common.utilsr   r   r   r5   r;   r?   r@   rE   rF   rK   rQ   r]   __name__r   r   r   r   <module>   s0   ?
	m*