
    Vh3?                         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 d d	lmZ d d
lmZmZ d dlmZmZmZ g 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: nxos_user
extends_documentation_fragment:
- cisco.nxos.nxos
author: Peter Sprygada (@privateip)
notes:
- Limited Support for Cisco MDS
short_description: Manage the collection of local users on Nexus devices
description:
- This module provides declarative management of the local usernames configured on
  Cisco Nexus devices.  It allows playbooks to manage either individual usernames
  or the collection of usernames in the current running config.  It also supports
  purging usernames from the configuration that are not explicitly defined.
version_added: 1.0.0
options:
  aggregate:
    description:
    - The set of username objects to be configured on the remote Cisco Nexus device.  The
      list entries can either be the username or a hash of username and properties.  This
      argument is mutually exclusive with the C(name) argument.
    aliases:
    - users
    - collection
    type: list
    elements: dict
    suboptions:
      name:
        description:
        - The username to be configured on the remote Cisco Nexus device.  This argument
          accepts a string value and is mutually exclusive with the C(aggregate) argument.
        type: str
      configured_password:
        description:
        - The password to be configured on the network device. The password needs to be
          provided in cleartext and it will be encrypted on the device.
        type: str
      hashed_password:
        description:
        - The hashed password to be configured on the network device. The password needs to
          already be encrypted.
        type: str
      update_password:
        description:
        - Since passwords are encrypted in the device running config, this argument will
          instruct the module when to change the password.  When set to C(always), the
          password will always be updated in the device and when set to C(on_create) the
          password will be updated only if the username is created.
        choices:
        - on_create
        - always
        type: str
      roles:
        description:
        - The C(role) argument configures the role for the username in the device running
          configuration.  The argument accepts a string value defining the role name.  This
          argument does not check if the role has been configured on the device.
        aliases:
        - role
        type: list
        elements: str
      sshkey:
        description:
        - The C(sshkey) argument defines the SSH public key to configure for the username.  This
          argument accepts a valid SSH key value.
        type: str
      state:
        description:
        - The C(state) argument configures the state of the username definition as it
         relates to the device operational configuration.  When set to I(present), the
         username(s) should be configured in the device active configuration and when
         set to I(absent) the username(s) should not be in the device active configuration
        choices:
        - present
        - absent
        type: str
  name:
    description:
    - The username to be configured on the remote Cisco Nexus device.  This argument
      accepts a string value and is mutually exclusive with the C(aggregate) argument.
    type: str
  configured_password:
    description:
    - The password to be configured on the network device. The password needs to be
      provided in cleartext and it will be encrypted on the device.
    type: str
  hashed_password:
    description:
    - The hashed password to be configured on the network device. The password needs to
      already be encrypted.
    type: str
  update_password:
    description:
    - Since passwords are encrypted in the device running config, this argument will
      instruct the module when to change the password.  When set to C(always), the
      password will always be updated in the device and when set to C(on_create) the
      password will be updated only if the username is created.
    default: always
    choices:
    - on_create
    - always
    type: str
  roles:
    description:
    - The C(role) argument configures the role for the username in the device running
      configuration.  The argument accepts a string value defining the role name.  This
      argument does not check if the role has been configured on the device.
    aliases:
    - role
    type: list
    elements: str
  sshkey:
    description:
    - The C(sshkey) argument defines the SSH public key to configure for the username.  This
      argument accepts a valid SSH key value.
    type: str
  purge:
    description:
    - The C(purge) argument instructs the module to consider the resource definition
      absolute.  It will remove any previously configured usernames on the device
      with the exception of the `admin` user which cannot be deleted per nxos constraints.
    type: bool
    default: false
  state:
    description:
    - The C(state) argument configures the state of the username definition as it
      relates to the device operational configuration.  When set to I(present), the
      username(s) should be configured in the device active configuration and when
      set to I(absent) the username(s) should not be in the device active configuration
    default: present
    choices:
    - present
    - absent
    type: str
a|  
- name: create a new user
  cisco.nxos.nxos_user:
    name: ansible
    sshkey: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
    state: present

- name: remove all users except admin
  cisco.nxos.nxos_user:
    purge: true

- name: set multiple users role
  cisco.nxos.nxos_user:
    aggregate:
      - name: netop
      - name: netend
    role: network-operator
  state: present
z
commands:
  description: The list of configuration mode commands to send to the device
  returned: always
  type: list
  sample:
    - name ansible
    - name ansible password password
N)deepcopy)partial)AnsibleModule)	iteritems)remove_default_specto_list)
get_configload_configrun_commands)znetwork-adminznetwork-operatorz	vdc-adminzvdc-operatorzpriv-15zpriv-14zpriv-13zpriv-12zpriv-11zpriv-10zpriv-9zpriv-8zpriv-7zpriv-6zpriv-5zpriv-4zpriv-3zpriv-2zpriv-1zpriv-0zdev-opsc                 d    t        j                  dt        | dg      t         j                        S )Nz^role name (\S+)z| include '^role name')flags)refindallr   M)modules    h/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/cisco/nxos/plugins/modules/nxos_user.pyget_custom_rolesr      s+    ::6":!;<
     c                 d    t         t        |      z   }| D ]  }||vs|j                  d        y )Nzinvalid role specifiedmsg)BUILTIN_ROLESr   	fail_json)valuer   valid_rolesitems       r   validate_rolesr       s;    "26"::K ;{"!9:;r   c                   	
 t               |j                  d   }| D ]  }|\  		fd}fdfd
	
fd}d   dk(  rj                  dd	   z         Bd
}d   dk(  r"	s  |       }|sj                  dd	   z          |d      r|dk(  s	s dd   z          |d      r|dk(  s	s dd   z          |d      r dd   z         |r |         S )Nupdate_passwordc                 p    j                  |       xr# j                  |       j                  |       k7  S )Nget)xhavewants    r   needs_updatez)map_obj_to_commands.<locals>.needs_update   s+    88A;?DHHQK488A;$>?r   c                 8    j                  dd   d|       S )N	username name appendr&   commandsr(   s    r   addz map_obj_to_commands.<locals>.add  s    ??tF|Q#GHHr   c                 8    j                  dd   d|       S )Nzno username r,   r-   r.   r0   s    r   removez#map_obj_to_commands.<locals>.remove  s    ??$v,#JKKr   c                     d   ryrat        d         j                  d         D ]  }  d| z          t        d         j                  d         D ]  }  d| z          yd   D ]  }  d| z          yy)Nroleszrole %sTF)set
difference)r   r2   r'   r4   r(   s    r   configure_rolesz,map_obj_to_commands.<locals>.configure_roles  s    G} #DM 2 = =d7m L 1y4/01 !$DM 2 = =d7m L .I,-.  !%W .I,-. r   stateabsentno username %sr,   Fpresentzusername %sconfigured_passwordalwayszpassword %shashed_passwordzpassword 5 %ssshkeyz	sshkey %s)listparamsr/   )updatesr   r"   updater)   r9   roles_configuredr2   r1   r'   r4   r(   s          @@@@@r   map_obj_to_commandsrG      s   vHmm$56O 1
d	@	I	L	 =H$OO,tF|;< =I%d.0#V <=-.(*$MD)>$??@)*(*$Od+<&==>!d8n,-c1f Or   c                 (    | j                  d      syy )Nremote_loginz
<PASSWORD>r$   )datas    r   parse_passwordrK   1  s    88N# $r   c                     d }d| v r| j                  d      d   }t               }|r$t        |      D ]  }|j                  |d           |S )N
TABLE_roleROW_rolerole)r%   rB   r   r/   )rJ   configured_rolesr6   r   s       r   parse_rolesrQ   6  sX    t88L1*=FE,- 	'DLLf&	'Lr   c           
          t        | dddg      }|d   }t               }t        |d   d         D ]G  }|j                  |d   t	        |      t	        |      |j                  d      t        |      d	d
       I |S )Nzshow user-accountjsoncommandoutputr   TABLE_templateROW_templateusr_namesshkey_infor=   )r,   r>   r@   rA   r6   r:   )r   rB   r   r/   rK   r%   rQ   )r   outrJ   objectsr   s        r   map_config_to_objr]   B  s    
v,?6 RS
TCq6DfG-.~>? 

Z('5d';#1$#7((=1$T*"		


 Nr   c                     |j                  |       s|j                  |    }|S |j                  |    j                  dd      }|j                  |   } |||           ||    }|S )Ntypestr)r%   rC   argument_spec _CHECK_ARGUMENT_TYPES_DISPATCHER)keyr   r   r   
value_typetype_checkers         r   get_param_valuerf   V  sk    88C=c" L ))#.2265A
>>zJT#YS	Lr   c           
          | j                   d   }|s]| j                   d   s| j                   d   r
t               S | j                   d   s| j                  d       npd| j                   d   ig}n]t               }|D ]N  }t        |t              s|j                  d|i       'd|vr| j                  d       >|j                  |       P t               }D ]  }t        t        ||       }|j                   |d       |d	       |d
       |d       |d      d       t        |      D ];  \  }}|s	t               j                  d|z        }t        ||f      s3 |||        = |j                  |        |S )N	aggregater,   purgezusername is requiredr   zname is required)r   r   r>   r@   rA   r6   r:   )r>   r@   rA   r6   r:   zvalidate_%s)rC   rB   r   
isinstancedictr/   r   rf   rE   r	   globalsr%   all)	r   rh   
collectionr   r\   	get_valuerc   r   	validators	            r   map_params_to_objrq   e  s}   k*I}}V$w)?6Mv&!78!6==#89:JV
 	(DdD)!!64.1t#  %7 8!!$'	( fG O$vF	'01F'G#,->#?#H-"7+"7+	
 $D/ 	-JC#IMM-#*=>	y)*eV,	- 	t'* Nr   c                    t               }| D ]u  t        fd|D        d       }t        |d u d   dk(  f      r|j                  i f       A|sDt	              D ]$  \  }}|s	|||   k7  s|j                  |f       & w |S )Nc              3   :   K   | ]  }|d    d    k(  s|  yw)r,   N ).0ientrys     r   	<genexpr>z!update_objects.<locals>.<genexpr>  s      C1&	U6](BQCs   r:   r=   )rB   nextrm   r/   r	   )r(   r'   rD   r   rc   r   rw   s         @r   update_objectsrz     s    fG 2CCTJeGn	9:;NNE2;''. 2
UUd3i/NNE4=122 Nr   c                     t        | dddg      }t               }|d   j                         D ]8  }|j                  d      s|j	                         d   }|j                  |       : |S )Nz'show running-config | section ^usernametextrT   r   r+      )r   r7   
splitlines
startswithsplitr2   )r   config_output	usernameslineusernames        r   get_configured_usernamesr     so     >&	QRM Ia ++- $??;'zz|AHMM(#$ r   c                     t        t               t        d      t        d      t        dddg      t        ddgd	      t        d
      t        dddg            } t        |       }t        |       t        t        dd|ddg      t        dd
            }|j                  |        ddg}t	        ||d      }d
g d}t        |      }t        |      }t        t        ||      |      }|j                  d   rrt        |D 	cg c]  }	|	d   	 c}	      }
t        |      }t        |      j                  |
      D ].  }|dk7  s	|j                  dd      }|j                  d|z         0 ||d<   d |v r|j                  d!"       |j                  d#   5t!        j"                  d%|j                  d#         s|j                  d&"       |r|j$                  st'        ||      }|D ]y  }|j)                         j+                  d'      r|j                  |"       5|d(   j-                  |j/                         D 	cg c]  }	|	j+                  d)      s|	d*d$  c}	       { d|d+<    |j0                  d,i | y$c c}	w c c}	w )-z%main entry point for module executionT)no_logr?   	on_create)defaultchoicesrB   rO   r`   )r_   aliaseselementsFr=   r;   )r,   r>   r@   r"   r6   rA   r:   rk   rn   users)r_   r   optionsr   bool)r_   r   )rh   ri   )r,   rh   )r>   r@   )ra   mutually_exclusivesupports_check_mode)changedwarningsri   r,   admin\z\\r<   r1   zno username adminz!cannot delete the `admin` accountr   r@   Nz^\$5\$......\$.*$zProvided hash is not validzwrong passwordr   z	WARNING: 	   r   rt   )rk   r   r
   rE   r   rq   r]   rG   rz   rC   r7   r   r8   replacer/   r   r   match
check_moder   lowerr   extendr~   	exit_json)element_specaggregate_specra   r   r   resultr(   r'   r1   r&   
want_users
have_usersr   	responsesresps                  r   mainr     s   V -D)XX7NO5A5!9y(.CDL l+N '"!7+	
 .M &/1[\#- F B/FV$DV$D">$#=vFH}}WT2!F)23
-f5

O..z: 	9Dw||D&1 04 78	9
 "F: h&@A }}&'3xx,fmm<M.NO!=>  #FH5I! ::<**+;<$$$.:&--(,(9W1Q\\+=V12W	 !yFvC 38 Xs   	JJ
J
__main__)&
__future__r   r   r   r_   __metaclass__DOCUMENTATIONEXAMPLESRETURNr   copyr   	functoolsr   ansible.module_utils.basicr   ansible.module_utils.sixr	   Oansible_collections.ansible.netcommon.plugins.module_utils.network.common.utilsr
   r   Eansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxosr   r   r   r   r   r    rG   rK   rQ   r]   rf   rq   rz   r   r   __name__rt   r   r   <module>r      s   $ A @ EN(
 
   4 .
 2;7t
	(*Z

Nb zF r   