
    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 d d	lmZ d d
lmZ d dlmZmZm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: eos_user
author: Peter Sprygada (@privateip)
short_description: Manage the collection of local users on EOS devices
description:
- This module provides declarative management of the local usernames configured on
  Arista EOS 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
notes:
- Tested against Arista EOS 4.24.6F
options:
  aggregate:
    description:
    - The set of username objects to be configured on the remote Arista EOS device.  The
      list entries can either be the username or a hash of username and properties.  This
      argument is mutually exclusive with the C(username) argument.
    aliases:
    - users
    - collection
    type: list
    elements: dict
    suboptions:
      name:
        description:
        - The username to be configured on the remote Arista EOS device.  This argument
          accepts a stringv value and is mutually exclusive with the C(aggregate) argument.
        type: str
      configured_password:
        description:
        - The password to be configured on the remote Arista EOS device. The password
          needs to be provided in clear and it will be encrypted on the device.
        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.
        type: str
        choices:
        - on_create
        - always
      privilege:
        description:
        - The C(privilege) argument configures the privilege level of the user when logged
          into the system.  This argument accepts integer values in the range of 1 to
          15.
        type: int
      role:
        description:
        - 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.
        type: str
      sshkey:
        description:
        - Specifies the SSH public key to configure for the given username.  This argument
          accepts a valid SSH key value.
        type: str
      nopassword:
        description:
        - Defines the username without assigning a password.  This will allow the user
          to login to the system without being authenticated by a password.
        type: bool
      state:
        description:
        - 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
        type: str
        choices:
        - present
        - absent
  name:
    description:
    - The username to be configured on the remote Arista EOS device.  This argument
      accepts a stringv value and is mutually exclusive with the C(aggregate) argument.
    type: str
  configured_password:
    description:
    - The password to be configured on the remote Arista EOS device. The password
      needs to be provided in clear and it will be encrypted on the device.
    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
    type: str
    choices:
    - on_create
    - always
  privilege:
    description:
    - The C(privilege) argument configures the privilege level of the user when logged
      into the system.  This argument accepts integer values in the range of 1 to
      15.
    type: int
  role:
    description:
    - 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.
    type: str
  sshkey:
    description:
    - Specifies the SSH public key to configure for the given username.  This argument
      accepts a valid SSH key value.
    type: str
  nopassword:
    description:
    - Defines the username without assigning a password.  This will allow the user
      to login to the system without being authenticated by a password.
    type: bool
  purge:
    description:
    - 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 EOS constraints.
    type: bool
    default: false
  state:
    description:
    - 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
    type: str
    default: present
    choices:
    - present
    - absent
a3  
- name: create a new user
  arista.eos.eos_user:
    name: ansible
    sshkey: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
    state: present

- name: remove all users except admin
  arista.eos.eos_user:
    purge: true

- name: set multiple users to privilege level 15
  arista.eos.eos_user:
    aggregate:
      - name: netop
      - name: netend
    privilege: 15
    state: present

- name: Change Password for User netop
  arista.eos.eos_user:
    username: netop
    configured_password: '{{ new_password }}'
    update_password: always
    state: present
ae  
commands:
  description: The list of configuration mode commands to send to the device
  returned: always
  type: list
  sample:
    - name ansible secret password
    - name admin secret admin
session_name:
  description: The EOS config session name used to load the configuration
  returned: when changed is True
  type: str
  sample: ansible_1479315771
N)deepcopy)partial)AnsibleModule)	iteritems)remove_default_spec)
get_configload_configrun_commandsc                 J    d| cxk  rdk  sn |j                  d| z         y y )N      z*privilege must be between 1 and 15, got %smsg)	fail_json)valuemodules     g/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/arista/eos/plugins/modules/eos_user.pyvalidate_privileger      s/    <uD 	 	
     c                     d}t        | d      }t        j                  d|d   t        j                        }|r2|j	                  d      j                  d      }t        d |D              }|S )Nz4.20.10z,show version | grep "Software image version"z#Software image version:\s+([\d\.]+)r   r   .c              3   2   K   | ]  }t        |        y wN)int).0digits     r   	<genexpr>z!get_os_version.<locals>.<genexpr>   s     5%3u:5s   )r   researchMgroupsplittuple)r   
os_versionresponseversion_matchvs        r   get_os_versionr+      sn    J6H II.
M
 "((-5155
r   c                   	
 t               |j                  d   }| D ]W  }|\  
		
fd}
fd}
d   dk(  rj                  d
d   z         5 |d      r|d	k(  s	s |d

d   z          |d      r |d
d   z          |d      r |d
d   z          |d      r-t        |      }|dkD  r |d
d   z         n |d
d   z          |d      r
d   r	 |d       n |d
d   z         
j	                  d      dk(  s
j	                  d      s
j	                  d      
j	                  d      
j	                  d      g}t        d |D              du sF|j                  d       Z S )Nupdate_passwordc                 p    j                  |       xr# j                  |       j                  |       k7  S r   )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 )Nz	username name )append)r0   commandsr2   s    r   addz map_obj_to_commands.<locals>.add  s    ??tF|Q#GHHr   stateabsentno username %sr5   configured_passwordalwaysz	secret %srolezrole %s	privilegezprivilege %ssshkey)      
   z
ssh-key %sz	sshkey %s
nopasswordzno username %s nopasswordpresentc              3   $   K   | ]  }|d u  
 y wr    )r   r*   s     r   r    z&map_obj_to_commands.<locals>.<genexpr>&  s     ,19,s   Tz<configured_password, sshkey or nopassword should be providedr   )listparamsr7   r+   r/   allr   )updatesr   r-   updater3   r9   verr   r8   r1   r2   s           @@@r   map_obj_to_commandsrO      s   vHmm$56O .
d	@	I =H$OO,tF|;<-.(*$K$'<"==>	DL()$k!223! (C[ L4>12K$x.01%L!L!/$v,>?88G	)dhhv.>./&"E
 ,e,,4  V ! Y.^ Or   c                 t    t        j                  d| t         j                        }|r|j                  d      S y )Nz
role (\S+)r   r!   r"   r#   r$   datamatchs     r   
parse_rolerU   -  s.    IImT2440E{{1~ r   c                 t    t        j                  d| t         j                        }|r|j                  d      S y )Nzsshkey|ssh-key (.+)$r   rQ   rR   s     r   parse_sshkeyrW   3  s/    II-tRTT:E{{1~ r   c                     t        j                  d| t         j                        }|rt        |j	                  d            S y )Nzprivilege (\S+)r   )r!   r"   r#   r   r$   rR   s     r   parse_privilegerY   9  s4    II($5E5;;q>"" r   c           
         t        | dg      }t        j                  d|t        j                        }|s
t	               S t	               }t        |      D ]u  }d|z  }t        j                  ||t        j                        }dj                  |      }|dd|v d t        |      t        |      t        |      d}|j                  |       w |S )	Nzsection username)flagsz^username (\S+)zusername %s .+$
rF   rE   )r5   r:   rE   r=   rA   r@   r?   )r   r!   findallr#   rI   setjoinrW   rY   rU   r7   )r   rS   rT   	instancesuserregexcfgobjs           r   map_config_to_objre   ?  s    f%7$89DJJ)46EvIE
 "T)jjbdd+iin&#-#'"3'(-sO
 	 r   c                 (   |j                  |       s|j                  |    }n>|j                  |    j                  dd      }|j                  |   } |||           ||    }t	               j                  d| z        }t        ||f      r	 |||       |S )Ntypestrzvalidate_%s)r/   rJ   argument_spec _CHECK_ARGUMENT_TYPES_DISPATCHERglobalsrK   )keyitemr   r   
value_typetype_checker	validators          r   get_param_valuerq   Z  s    88C=c" ))#.2265A
>>zJT#YS	 	mc12I
E9% 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 ]g  }t        t        ||       } |d      |d<    |d      |d<    |d	      |d	<    |d
      |d
<    |d      |d<    |d      |d<   |j                  |       i |S )N	aggregater5   purgezname is requiredr   )rm   r   r=   rE   r@   r?   rA   r:   )rJ   rI   r   
isinstancedictr7   r   rq   )r   rs   
collectionrm   objects	get_values         r   map_params_to_objrz   n  sQ   k*I}}V$w)?6Mv&!34!6==#89:JV
 	(DdD)!!64.1t#  %7 8!!$'	( fG O$vF	&/0E&F"#&|4\%k2[ (V"8,X!'*Wt Nr   c                    t               }| D ]y  dv rt        fd|D        d       }t        d u d   dk(  f      r|j                  i f       E|sHt	              D ]$  \  }}|s	|||   k7  s|j                  |f       & { |S )Nr5   c              3   :   K   | ]  }|d    d    k(  s|  yw)r5   NrH   )r   ientrys     r   r    z!update_objects.<locals>.<genexpr>  s      GqAfIv,FGs   r:   rF   )rI   nextrK   r7   r	   )r2   r1   rL   rm   rl   r   r~   s         @r   update_objectsr     s    fG 2U?GDGNDeGn	9:;NNE2;''. 2
UUd3i/NNE4=122 Nr   c                  &   t        t               t        d      t        d      t        dddg      t        d      t               t        d      t        d	d	d
g            } t        |       }t        |       t        t        dd|ddg      t        dd            }|j                  |        dg}t	        ||d      }t               }ddi}|r||d<   t        |      }t        |      }t        t        ||      |      }	|j                  d   r]|D 
cg c]  }
|
d   	 }}
|D 
cg c]  }
|
d   	 }}
t        |      j                  |      D ]  }|dk7  s	|	j                  d|z          |	|d<   d|	v r|j                  d       |	rg|j                   }t!        ||	|       }|j#                  d!      r"|j$                  rd"|j#                  d!      i|d!<   |j#                  d#      |d$<   d|d<    |j&                  d&i | y%c c}
w c c}
w )'z%main entry point for module executionT)no_logbool)rg   r>   	on_create)defaultchoicesr   rF   r;   )r5   r=   rE   r-   r@   r?   rA   r:   rI   rv   rw   users)rg   elementsoptionsaliasesF)rg   r   )rs   rt   )r5   rs   )ri   mutually_exclusivesupports_check_modechangedwarningsrt   r5   adminr<   r8   zno username adminz!cannot delete the `admin` accountr   )commitdiffpreparedsessionsession_nameNrH   )rv   r   r
   rM   r   rI   rz   re   rO   r   rJ   r^   
differencer7   r   
check_moder   r/   _diff	exit_json)element_specaggregate_specri   r   r   r   resultr2   r1   r8   r0   
want_users
have_usersrm   r   r(   s                   r   mainr     s+   V -V$ (+
 E"V4 9y(.CDL l+N '"!7+	
 .M &/0#- F vHF%zV$DV$D">$#=vFH}}W)-.Aai.
.)-.Aai.
.
O..z: 	9Dw 04 78	9 "F: h&@A&&&vx?<<FLL((,,v*>?F6N!)i!8~ yFv+ /.s   H	+H__main__)$
__future__r   r   r   rg   __metaclass__DOCUMENTATIONEXAMPLESRETURNr!   copyr   	functoolsr   ansible.module_utils.basicr   ansible.module_utils.sixr	   Oansible_collections.ansible.netcommon.plugins.module_utils.network.common.utilsr
   Cansible_collections.arista.eos.plugins.module_utils.network.eos.eosr   r   r   r   r+   rO   rU   rW   rY   re   rq   rz   r   r   __name__rH   r   r   <module>r      s   $ A @ IV6
 
   4 . 
"3l#6(DIX zF r   