
    Vh=k                        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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 d Zd ZddZd Z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_user
author: Trishna Guha (@trishnaguha)
short_description: Module to manage the aggregates of local users.
description:
  - This module provides declarative management of the local usernames configured on
    network devices. It allows playbooks to manage either individual usernames or the
    aggregate 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 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:
  aggregate:
    description:
      - The set of username objects to be configured on the remote Cisco IOS 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 Cisco IOS device. This argument accepts
            a string value and is mutually exclusive with the C(aggregate) argument. Please
            note that this option is not same as C(provider username).
        type: str
        required: true
      configured_password:
        description:
          - The password to be configured on the Cisco IOS device. The password needs to
            be provided in clear and it will be encrypted on the device. Please note that
            this option is not same as C(provider password).
        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
      password_type:
        description:
          - This argument determines whether a 'password' or 'secret' will be configured.
        choices:
          - secret
          - password
        type: str
      hashed_password:
        description:
          - This option allows configuring hashed passwords on Cisco IOS devices.
        type: dict
        suboptions:
          type:
            description:
              - Specifies the type of hash (e.g., 5 for MD5, 8 for PBKDF2, etc.)
              - For this to work, the device needs to support the desired hash type
            type: int
            required: true
          value:
            description:
              - The actual hashed password to be configured on the device
            required: true
            type: str
      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
      view:
        description:
          - Configures the view for the username in the device running configuration. The
            argument accepts a string value defining the view name. This argument does not
            check if the view has been configured on the device.
        aliases:
          - role
        type: str
      sshkey:
        description:
          - Specifies one or more SSH public key(s) to configure for the given username.
          - This argument accepts a valid SSH key value.
        type: list
        elements: 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
        choices:
          - present
          - absent
        type: str
  name:
    description:
      - The username to be configured on the Cisco IOS device. This argument accepts
        a string value and is mutually exclusive with the C(aggregate) argument. Please
        note that this option is not same as C(provider username).
    type: str
  configured_password:
    description:
      - The password to be configured on the Cisco IOS device. The password needs to
        be provided in clear and it will be encrypted on the device. Please note that
        this option is not same as C(provider password).
    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
  password_type:
    description:
      - This argument determines whether a 'password' or 'secret' will be configured.
    default: secret
    choices:
      - secret
      - password
    type: str
  hashed_password:
    description:
      - This option allows configuring hashed passwords on Cisco IOS devices.
    type: dict
    suboptions:
      type:
        description:
          - Specifies the type of hash (e.g., 5 for MD5, 8 for PBKDF2, etc.)
          - For this to work, the device needs to support the desired hash type
        type: int
        required: true
      value:
        description:
          - The actual hashed password to be configured on the device
        required: true
        type: str
  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
  view:
    description:
      - Configures the view for the username in the device running configuration. The
        argument accepts a string value defining the view name. This argument does not
        check if the view has been configured on the device.
    aliases:
      - role
    type: str
  sshkey:
    description:
      - Specifies one or more SSH public key(s) to configure for the given username.
      - This argument accepts a valid SSH key value.
    type: list
    elements: 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 (the current defined set of users).
    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
    default: present
    choices:
      - present
      - absent
    type: str
extends_documentation_fragment:
  - cisco.ios.ios
aq  
# Using state: present

# Before state:
# -------------

# router-ios#show running-config | section ^username
# username testuser privilege 15 password 0 password

# Present state create a new user play:
# -------------------------------------

- name: Create a new user
  cisco.ios.ios_user:
    name: ansible
    nopassword: true
    sshkey: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
    state: present

# Task Output
# -----------

# commands:
# - ip ssh pubkey-chain
# - username ansible
# - key-hash ssh-rsa 2ABB27BBC33ED53EF7D55037952ABB27 test@fedora
# - exit
# - exit
# - username ansible nopassword

# After state:
# ------------

# router-ios#show running-config | section username
# username testuser privilege 15 password 0 password
# username ansible nopassword
#   username ansible
#    key-hash ssh-rsa 2ABB27BBC33ED53EF7D55037952ABB27 test@fedora

# Using state: present

# Before state:
# -------------

# router-ios#show running-config | section ^username
# username testuser privilege 15 password 0 password

# Present state create a new user with multiple keys play:
# --------------------------------------------------------

- name: Create a new user with multiple keys
  cisco.ios.ios_user:
    name: ansible
    sshkey:
      - "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
      - "{{ lookup('file', '~/path/to/public_key') }}"
    state: present

# Task Output
# -----------

# commands:
# - ip ssh pubkey-chain
# - username ansible
# - key-hash ssh-rsa 2ABB27BBC33ED53EF7D55037952ABB27 test@fedora
# - key-hash ssh-rsa 1985673DCF7FA9A0F374BB97DC2ABB27 test@fedora
# - exit
# - exit

# After state:
# ------------

# router-ios#show running-config | section username
# username testuser privilege 15 password 0 password
#   username ansible
#    key-hash ssh-rsa 2ABB27BBC33ED53EF7D55037952ABB27 test@fedora
#    key-hash ssh-rsa 1985673DCF7FA9A0F374BB97DC2ABB27 test@fedora

# Using Purge: true

# Before state:
# -------------

# router-ios#show running-config | section ^username
# username admin privilege 15 password 0 password
# username testuser privilege 15 password 0 password
# username ansible nopassword
#   username ansible
#    key-hash ssh-rsa 2ABB27BBC33ED53EF7D55037952ABB27 test@fedora

# Purge all users except admin play:
# ----------------------------------

- name: Remove all users except admin
  cisco.ios.ios_user:
    purge: true

# Task Output
# -----------

# commands:
# - no username testuser
# - no username ansible
# - ip ssh pubkey-chain
# - no username ansible
# - exit

# After state:
# ------------

# router-ios#show running-config | section username
# username admin privilege 15 password 0 password

# Using Purge: true

# Before state:
# -------------

# router-ios#show running-config | section ^username
# username admin privilege 15 password 0 password
# username testuser privilege 15 password 0 password1
# username testuser1 privilege 15 password 0 password2
# username ansible nopassword

# Purge all users except admin and these listed users play:
# ---------------------------------------------------------

- name: Remove all users except admin and these listed users
  cisco.ios.ios_user:
    aggregate:
      - name: testuser
      - name: testuser1
    purge: true

# Task Output
# -----------

# commands:
# - no username ansible

# After state:
# ------------

# router-ios#show running-config | section username
# username admin privilege 15 password 0 password
# username testuser privilege 15 password 0 password1
# username testuser1 privilege 15 password 0 password2

# Using state: present

# Before state:
# -------------

# router-ios#show running-config | section ^username
# username admin privilege 15 password 0 password
# username netop password 0 password1
# username netend password 0 password2

# Present state set multiple users to privilege level 15 play:
# ------------------------------------------------------------

- name: Set multiple users to privilege level 15
  cisco.ios.ios_user:
    aggregate:
      - name: netop
      - name: netend
    privilege: 15
    state: present

# Task Output
# -----------

# commands:
# - username netop privilege 15
# - username netend privilege 15

# After state:
# ------------

# router-ios#show running-config | section username
# username admin privilege 15 password 0 password
# username netop privilege 15 password 0 password1
# username netend privilege 15 password 0 password2

# Using state: present

# Before state:
# -------------

# router-ios#show running-config | section ^username
# username admin privilege 15 password 0 password
# username netop privilege 15 password 0 oldpassword

# Present state Change Password for User netop play:
# --------------------------------------------

- name: Change Password for User netop
  cisco.ios.ios_user:
    name: netop
    configured_password: "newpassword"
    password_type: password
    update_password: always
    state: present

# Task Output
# -----------

# commands:
# - username netop password newpassword

# After state:
# ------------

# router-ios#show running-config | section username
# username admin privilege 15 password 0 password
# username netop privilege 15 password 0 newpassword

# Using state: present

# Before state:
# -------------

# router-ios#show running-config | section ^username
# username admin privilege 15 password 0 password
# username netop privilege 15 password 0 password
# username netend privilege 15 password 0 password

# Present state set user view/role for users play:
# --------------------------------------------

- name: Set user view/role for users
  cisco.ios.ios_user:
    aggregate:
      - name: netop
      - name: netend
    view: network-admin
    state: present

# Task Output
# -----------

# commands:
# - username netop view network-admin
# - username netend view network-admin

# After state:
# ------------

# router-ios#show running-config | section username
# username admin privilege 15 password 0 password
# username netop privilege 15 view network-admin password 0 password
# username netend privilege 15 view network-admin password 0 password

# Using state: present

# Before state:
# -------------

# router-ios#show running-config | section ^username
# username admin privilege 15 password 0 password

# Present state create a new user with hashed password play:
# --------------------------------------------------------------

- name: Create a new user with hashed password
  cisco.ios.ios_user:
    name: ansibletest5
    hashed_password:
      type: 9
      value: "thiswillbereplacedwithhashedpassword"
    state: present

# Task Output
# -----------

# commands:
# - username ansibletest5 secret 9 thiswillbereplacedwithhashedpassword

# After state:
# ------------

# router-ios#show running-config | section username
# username admin privilege 15 password 0 password
# username ansibletest5 secret 9 thiswillbereplacedwithhashedpassword

# Using state: absent

# Before state:
# -------------

# router-ios#show running-config | section ^username
# username admin privilege 15 password 0 password
# username ansibletest1 password 0 password
# username ansibletest2 secret 9 thiswillbereplacedwithhashedpassword
# username ansibletest3 password 5 thistoowillbereplacedwithhashedpassword

# Absent state remove multiple users play:
# ----------------------------------------

- name: Delete users with aggregate
  cisco.ios.ios_user:
    aggregate:
      - name: ansibletest1
      - name: ansibletest2
      - name: ansibletest3
    state: absent

# Task Output
# -----------

# commands:
# - no username ansibletest1
# - no username ansibletest2
# - no username ansibletest3

# After state:
# ------------

# router-ios#show running-config | section username
# username admin privilege 15 password 0 password
z
commands:
  description: The list of configuration mode commands to send to the device
  returned: always
  type: list
  sample:
    - username ansible secret password
    - username admin secret admin
N)deepcopy)partial)AnsibleModule)	iteritems)remove_default_spec)
get_configload_configc                 P    | r$d| cxk  rdk  sn |j                  d| z         y y y )N      z*privilege must be between 1 and 15, got %smsg)	fail_json)valuemodules     f/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/cisco/ios/plugins/modules/ios_user.pyvalidate_privileger   <  s/    Q%%2%IEQR &u    c                     d| z  ddddS )Nno username %szMThis operation will remove all username related configurations with same nameyF)commandpromptanswernewline )usernames    r   user_del_cmdr!   A  s    #h.a	 r   c                    | j                  d       |rD| j                  d|d   z         |D ]  }| j                  d|z          | j                  d       n| j                  d|d   z         | j                  d       y )Nzip ssh pubkey-chainzusername %snamezkey-hash %sexitr   append)r   wantxitems       r   add_sshr*   J  su    NN()}tF|34 	1DNN=4/0	1v'$v,67NN6r   c                 v   | sy d| v rl| j                  d      }t        j                  t        j                  |d               j                         j                         |d<   dj                  |      S dt        j                  t        j                  |             j                         j                         z  S )N r   z
ssh-rsa %s)splithashlibmd5base64	b64decode	hexdigestupperjoin)sshkeykeypartss     r   sshkey_fingerprintr7   V  s     
f}<<$kk&"2"28A;"?@JJLRRTxx!! gkk&*:*:6*BCMMOUUWWWr   c           	         t               }|j                  d   }|j                  d   }d }d }d }| D ]  }|\  }	}
|	d   dk(  r/|
d   rt        ||	       n|j                  t	        |	d	                 ||	|
d
      r |||	d|	d
   z          ||	|
d      r |||	d|	d   z          ||	|
d      rt        ||	|	d           ||	|
d      r:|dk(  s|
s3|
r|
d   r||
d   k7  r|j                  d        |||	|d|	d           ||	|
d      r |||	|	d   |        ||	|
d      s|	d   r |||	d        |||	t	        |	d	                 |S )Nupdate_passwordpassword_typec                 n    | j                  |      xr# | j                  |      |j                  |      k7  S N)get)r'   haver(   s      r   needs_updatez)map_obj_to_commands.<locals>.needs_updatek  s*    xx{9txx{dhhqk99r   c                 8    | j                  d|d   d|       y )N	username r#   r,   r%   )r   r'   r(   s      r   addz map_obj_to_commands.<locals>.addn  s    4<;<r   c                     | j                  d|d   d|d|j                  d      d|j                  d             y )NrA   r#   r,   typer   )r&   r=   )r   r'   r(   r:   s       r   add_hashed_passwordz0map_obj_to_commands.<locals>.add_hashed_passwordq  s3    &*6lM155=RSRWRWX_R`a	
r   stateabsentr5   r#   viewzview %s	privilegezprivilege %sconfigured_passwordalwayszTCan not have both a user password and a user secret. Please choose one or the other.r   r,   hashed_password
nopassword)listparamsr*   r&   r!   r   )updatesr   commandsr9   r:   r?   rB   rE   updater'   r>   s              r   map_obj_to_commandsrS   f  s   vHmm$56OMM/2M:=

  @
d=H$H~$'T&\ :;dF+$	DL 89dK0$k1B BCdH-HdDN3d$9:(*$D1mtOG\6\$$= %  Hd}dCX>Y$Z[d$56$5F0GWdL1L!HdL1HdLf$>?7@8 Or   c                 t    t        j                  d| t         j                        }|r|j                  d      S y )Nz
view (\S+)r   )researchMgroupdatamatchs     r   
parse_viewr\     s.    IImT2440E{{1~ r   c                     d|z  }t        j                  || t         j                        }g }|r7t        j                  d|j	                         t         j                        }|r|}|S )Nzusername %s(\n\s+key-hash .+$)+zkey-hash (\S+ \S+(?: .+)?)$)rU   rV   rW   findallrX   )rZ   usersshregexsshcfgkey_listr[   s         r   parse_sshkeyrc     sT    2T9HYYxrtt,FH

:FLLNBDDQHOr   c                     t        j                  d| t         j                        }|rt        |j	                  d            S y )Nzprivilege (\S+)r   )rU   rV   rW   intrX   rY   s     r   parse_privilegerf     s4    II($5E5;;q>"" r   c                 ^    d }| r(| j                         d   dv r| j                         d   }|S )N)passwordsecret)r-   )rZ   rD   s     r   parse_password_typerk     s2    D

R $::zz|BKr   c                    t        | dg      }t        j                  d|t        j                        }|s
t	               S t	               }t        |      D ]  }d|z  }t        j                  ||t        j                        }dj                  |      }t        ||      }|dd|v d d t        |      ||j                         r|rdnd	|j                         r|g k(  rd	ndt        |      t        |      d
}|j                  |        |S )Nz| section username)flagsz(?:^(?:u|\s{2}u))sername (\S+)zusername %s .+$
presentrM   FT)r#   rF   rM   rJ   rL   r:   r5   is_only_ssh_useris_only_normal_userrI   rH   )r   rU   r^   rW   rN   setr4   rc   rk   striprf   r\   r&   )	r   rZ   r[   	instancesr_   regexcfgssh_key_listobjs	            r   map_config_to_objry     s    f%9$:;DJJ94FEvIE
 !D(jjbdd+iin#D$/&#-#'#05"),4+.99;<2;M4SX(-sO
 	%& r   c                 (   |j                  |       s|j                  |    }n>|j                  |    j                  dd      }|j                  |   } |||           ||    }t	               j                  d| z        }t        ||f      r	 |||       |S )NrD   strzvalidate_%s)r=   rO   argument_spec _CHECK_ARGUMENT_TYPES_DISPATCHERglobalsall)keyr)   r   r   
value_typetype_checker	validators          r   get_param_valuer     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 ]{  }t        t        ||       } |d      |d<    |d	      |d	<    |d
      |d
<    |d      |d<    |d      |d<   t         |d            |d<    |d      |d<   |j                  |       } |S )N	aggregater#   purgezusername is requiredr   zname is required)r)   r   rJ   rL   rM   rI   rH   r5   rF   )	rO   rN   r   
isinstancedictr&   r   r   render_key_list)r   usersr   r)   objects	get_values         r   map_params_to_objr     sg   MM+&E}}V$w)?6Mv&!78 &--"789IF	 	'DdD)  &$0t#  %7 8  &	' fG 	O$vF	&/0E&F"#"+,=">&|4\%k2[ (V(8)<=X!'*Wt	 Nr   c                 P    g }| r!| D ]  }|j                  t        |              |S r<   )r&   r7   )ssh_keysrb   r)   s      r   r   r      s0    H 	6DOO.t45	6Or   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#   Nr   ).0ientrys     r   	<genexpr>z!update_objects.<locals>.<genexpr>  s      C1&	U6](BQCs   rF   ro   )rN   nextr   r&   r	   )r'   r>   rP   r)   r   r   r   s         @r   update_objectsr     s    fG 2CCTJeGn	9:;NNE2;''. 2
UUd3i/NNE4=122 Nr   c                     | D cg c]  }||   	 }}|D cg c]  }||   	 }}t        |      j                  |      }|D cg c]  }||   |v s| }}|S c c}w c c}w c c}w r<   )rr   
difference)	list1list2r   r(   
want_users
have_userssetdifferencer)   results	            r   find_set_differencer     sr    "'(Q!C&(J("'(Q!C&(J(
O..z:M$CtS	](BdCFCM	 )(Cs   AAA"A"c                  D   t        t        dd      t        dd            } t        t               t        d      t        dd|       t        d	
      t        dddg      t        dddg      t        d
      t        dg      t        ddd      t        dddg      
      }t        |      }t        d      |d<   t        |       t        t        dd|ddg      t        d	d            }|j                  |       d d!g}t	        ||d"      }t               }d|d#}t        |      }t        |      }	t        t        ||	      |      }
|j                  d$   rt        ||	d      }|D ]w  }|d   d%k7  s|d&   rt        |
|       |d'   r|
j                  t        |d                |d'   du sG|d&   du sOt        |
|       |
j                  t        |d                y |
|d(<   |
r|j                  st!        ||
       d|d)<    |j"                  d+i | y*),z%main entry point for module executionre   T)rD   required)no_logr   )rD   r   )r   r   )r   rD   optionsbool)rD   rK   	on_create)defaultchoicesrj   ri   role)aliasesrN   r{   F)rD   elementsr   ro   rG   )
r#   rJ   rL   rM   r9   r:   rI   rH   r5   rF   )r   r#   r   
collection)rD   r   r   r   )rD   r   )r   r   )r#   r   )rM   rL   rJ   )r|   mutually_exclusivesupports_check_mode)changedwarningsr   adminrp   rq   rQ   r   Nr   )r   r   r
   rR   r   rN   r   ry   rS   r   rO   r   r*   r&   r!   
check_moder   	exit_json)hashed_password_specelement_specaggregate_specr|   r   r   r   r   r'   r>   rQ   r   r)   s                r   mainr     s;   ut,$. V -Dv?STV$XX7NO8h
5KLE"6(#%>9y(.CDL l+N!40N6'"l+	
 .M &@ #- F
 vHH5FV$DV$D">$#=vFH}}W+D$?! 	@DF|w&*+Hd+-.OOLf$>?-.%7DAS<TX]<]Hd+OOLf$>?	@ "F:  ) yFvr   __main__r<   )*
__future__r   r   r   rD   __metaclass__DOCUMENTATIONEXAMPLESRETURNr0   r.   rU   copyr   	functoolsr   ansible.module_utils.basicr   ansible.module_utils.sixr	   Oansible_collections.ansible.netcommon.plugins.module_utils.network.common.utilsr
   Bansible_collections.cisco.ios.plugins.module_utils.network.ios.iosr   r   r   r!   r*   r7   rS   r\   rc   rf   rk   ry   r   r   r   r   r   r   __name__r   r   r   <module>r      s   $ A @ EN@D

   	   4 .S
	X ,^#8"@
?D zF r   