| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
""" |
| 8 |
The module file for sonic_users |
| 9 |
""" |
| 10 |
|
| 11 |
from __future__ import absolute_import, division, print_function |
| 12 |
__metaclass__ = type |
| 13 |
|
| 14 |
DOCUMENTATION = """ |
| 15 |
--- |
| 16 |
module: sonic_users |
| 17 |
version_added: 1.1.0 |
| 18 |
notes: |
| 19 |
- Tested against Enterprise SONiC Distribution by Dell Technologies. |
| 20 |
- Supports C(check_mode). |
| 21 |
author: Niraimadaiselvam M (@niraimadaiselvamm) |
| 22 |
short_description: Manage users and its parameters |
| 23 |
description: |
| 24 |
- This module provides configuration management of users parameters on devices running Enterprise SONiC. |
| 25 |
options: |
| 26 |
config: |
| 27 |
description: |
| 28 |
- Specifies the users related configuration. |
| 29 |
type: list |
| 30 |
elements: dict |
| 31 |
suboptions: |
| 32 |
name: |
| 33 |
description: |
| 34 |
- Specifies the name of the user. |
| 35 |
type: str |
| 36 |
required: true |
| 37 |
role: |
| 38 |
description: |
| 39 |
- Specifies the role of the user. |
| 40 |
type: str |
| 41 |
choices: |
| 42 |
- admin |
| 43 |
- operator |
| 44 |
- netadmin |
| 45 |
- secadmin |
| 46 |
password: |
| 47 |
description: |
| 48 |
- Specifies the password of the user. |
| 49 |
type: str |
| 50 |
update_password: |
| 51 |
description: |
| 52 |
- Specifies the update password flag. |
| 53 |
- In case of always, password will be updated every time. |
| 54 |
- In case of on_create, password will be updated only when user is created. |
| 55 |
type: str |
| 56 |
choices: |
| 57 |
- always |
| 58 |
- on_create |
| 59 |
default: always |
| 60 |
ssh_key: |
| 61 |
version_added: 3.1.0 |
| 62 |
description: |
| 63 |
- Specifies the user ssh key for an existing user. |
| 64 |
- The ssh key pair (public and private key) can be created using ssh-keygen command on the server from |
| 65 |
- where the user will connect to the SONiC device. |
| 66 |
- Only the public key is configured in the ssh_key field. |
| 67 |
- The ssh_key option can only be "merged" after initial creation of the affected "user" instance |
| 68 |
- and must be merged without any additional options specified other than "name". |
| 69 |
- It can only be deleted as an individual option if it is the only option specified. |
| 70 |
- If other options are specified for deletion, the entire user instance is deleted. |
| 71 |
type: str |
| 72 |
state: |
| 73 |
description: |
| 74 |
- Specifies the operation to be performed on the users configured on the device. |
| 75 |
- In case of merged, the input configuration will be merged with the existing users configuration on the device. |
| 76 |
- In case of deleted the existing users configuration will be removed from the device. |
| 77 |
- In case of replaced, the existing specified user configuration will be replaced with provided configuration. |
| 78 |
- In case of overridden, the existing users configuration will be overridden with the provided configuration. |
| 79 |
default: merged |
| 80 |
choices: ['merged', 'deleted', 'overridden', 'replaced'] |
| 81 |
type: str |
| 82 |
""" |
| 83 |
|
| 84 |
EXAMPLES = """ |
| 85 |
# Using "deleted" state |
| 86 |
# |
| 87 |
# Before state: |
| 88 |
# ------------- |
| 89 |
# |
| 90 |
# sonic# show users configured |
| 91 |
# ---------------------------------------------------------------------- |
| 92 |
# User Role(s) |
| 93 |
# ---------------------------------------------------------------------- |
| 94 |
# admin admin |
| 95 |
# sysadmin admin |
| 96 |
# sysoperator operator |
| 97 |
|
| 98 |
- name: Delete user |
| 99 |
dellemc.enterprise_sonic.sonic_users: |
| 100 |
config: |
| 101 |
- name: sysoperator |
| 102 |
state: deleted |
| 103 |
|
| 104 |
# After state: |
| 105 |
# ------------ |
| 106 |
# |
| 107 |
# sonic# show users configured |
| 108 |
# ---------------------------------------------------------------------- |
| 109 |
# User Role(s) |
| 110 |
# ---------------------------------------------------------------------- |
| 111 |
# admin admin |
| 112 |
# sysadmin admin |
| 113 |
|
| 114 |
# Using "deleted" state |
| 115 |
# |
| 116 |
# Before state: |
| 117 |
# ------------- |
| 118 |
# |
| 119 |
# sonic# show users configured |
| 120 |
# ---------------------------------------------------------------------- |
| 121 |
# User Role(s) |
| 122 |
# ---------------------------------------------------------------------- |
| 123 |
# admin admin |
| 124 |
# sysadmin admin |
| 125 |
# sysoperator operator |
| 126 |
|
| 127 |
- name: Delete all users configurations except admin |
| 128 |
dellemc.enterprise_sonic.sonic_users: |
| 129 |
config: |
| 130 |
state: deleted |
| 131 |
|
| 132 |
# After state: |
| 133 |
# ------------ |
| 134 |
# |
| 135 |
# sonic# show users configured |
| 136 |
# ---------------------------------------------------------------------- |
| 137 |
# User Role(s) |
| 138 |
# ---------------------------------------------------------------------- |
| 139 |
# admin admin |
| 140 |
|
| 141 |
# Using "merged" state |
| 142 |
# |
| 143 |
# Before state: |
| 144 |
# ------------- |
| 145 |
# |
| 146 |
# sonic# show users configured |
| 147 |
# ---------------------------------------------------------------------- |
| 148 |
# User Role(s) |
| 149 |
# ---------------------------------------------------------------------- |
| 150 |
# admin admin |
| 151 |
|
| 152 |
- name: Merge users configurations |
| 153 |
dellemc.enterprise_sonic.sonic_users: |
| 154 |
config: |
| 155 |
- name: sysadmin |
| 156 |
role: admin |
| 157 |
password: admin |
| 158 |
update_password: always |
| 159 |
- name: sysoperator |
| 160 |
role: operator |
| 161 |
password: operator |
| 162 |
update_password: always |
| 163 |
state: merged |
| 164 |
|
| 165 |
# After state: |
| 166 |
# ------------ |
| 167 |
# |
| 168 |
# sonic# show users configured |
| 169 |
# ---------------------------------------------------------------------- |
| 170 |
# User Role(s) |
| 171 |
# ---------------------------------------------------------------------- |
| 172 |
# admin admin |
| 173 |
# sysadmin admin |
| 174 |
# sysoperator operator |
| 175 |
|
| 176 |
# Using "overridden" state |
| 177 |
# |
| 178 |
# Before state: |
| 179 |
# ------------- |
| 180 |
# |
| 181 |
# sonic# show users configured |
| 182 |
# ---------------------------------------------------------------------- |
| 183 |
# User Role(s) |
| 184 |
# ---------------------------------------------------------------------- |
| 185 |
# admin admin |
| 186 |
# sysadmin admin |
| 187 |
# sysoperator operator |
| 188 |
|
| 189 |
- name: Override users configurations |
| 190 |
dellemc.enterprise_sonic.sonic_users: |
| 191 |
config: |
| 192 |
- name: user1 |
| 193 |
role: secadmin |
| 194 |
password: 123abc |
| 195 |
update_password: always |
| 196 |
state: overridden |
| 197 |
|
| 198 |
# After state: |
| 199 |
# ------------ |
| 200 |
# |
| 201 |
# sonic# show users configured |
| 202 |
# ---------------------------------------------------------------------- |
| 203 |
# User Role(s) |
| 204 |
# ---------------------------------------------------------------------- |
| 205 |
# admin admin |
| 206 |
# user1 secadmin |
| 207 |
|
| 208 |
# Using "replaced" state |
| 209 |
# |
| 210 |
# Before state: |
| 211 |
# ------------- |
| 212 |
# |
| 213 |
# sonic# show users configured |
| 214 |
# ---------------------------------------------------------------------- |
| 215 |
# User Role(s) |
| 216 |
# ---------------------------------------------------------------------- |
| 217 |
# admin admin |
| 218 |
# user1 secadmin |
| 219 |
# user2 operator |
| 220 |
|
| 221 |
- name: Replace users configurations |
| 222 |
dellemc.enterprise_sonic.sonic_users: |
| 223 |
config: |
| 224 |
- name: user1 |
| 225 |
role: operator |
| 226 |
password: 123abc |
| 227 |
update_password: always |
| 228 |
- name: user2 |
| 229 |
role: netadmin |
| 230 |
password: 123abc |
| 231 |
update_password: always |
| 232 |
state: replaced |
| 233 |
|
| 234 |
# After state: |
| 235 |
# ------------ |
| 236 |
# |
| 237 |
# sonic# show users configured |
| 238 |
# ---------------------------------------------------------------------- |
| 239 |
# User Role(s) |
| 240 |
# ---------------------------------------------------------------------- |
| 241 |
# admin admin |
| 242 |
# user1 operator |
| 243 |
# user2 netadmin |
| 244 |
|
| 245 |
# Using Merged |
| 246 |
# |
| 247 |
# Before state: |
| 248 |
# ------------- |
| 249 |
# |
| 250 |
# sonic# show users sshkey |
| 251 |
# |
| 252 |
# sonic# show users configured |
| 253 |
# -------------------------------------------------------------------- |
| 254 |
# User Role(s) |
| 255 |
# -------------------------------------------------------------------- |
| 256 |
# admin admin |
| 257 |
# user1 operator |
| 258 |
# user2 netadmin |
| 259 |
|
| 260 |
- name: Configure user sshkey |
| 261 |
dellemc.enterprise_sonic.sonic_users: |
| 262 |
config: |
| 263 |
- name: user1 |
| 264 |
ssh_key: > |
| 265 |
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQCoCc6lPgrSAXZByJAPH6cwn0Mhj9J1zYUfiLc |
| 266 |
/iz/IwHt/7s++bN1tnL6bAan6Ssg4XvOF0mcP5K53AAP+bX5WHy/d1wm7icllBI0JT150qp9nY5y |
| 267 |
bjNdvLH11cxqc+mmNYa7d40fpeoUgMdSBGtSL0jY2PHHRCvVscFYjSm6tQQ== root@sonic |
| 268 |
state: merged |
| 269 |
|
| 270 |
# After state: |
| 271 |
# ------------ |
| 272 |
# |
| 273 |
# sonic# show users configured |
| 274 |
# ---------------------------------------------------------------------- |
| 275 |
# sonic# show users sshKey |
| 276 |
# User: user1 |
| 277 |
# SSH Key: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQCoCc6lPgrSAXZByJAPH6cwn0Mhj9J1zYUfiLc/iz/IwHt/7s |
| 278 |
# ++bN1tnL6bAan6Ssg4XvOF0mcP5K53AAP+bX5WHy/d1wm7icllBI0JT150qp9nY5ybjNdvLH11cxqc+mmNYa7d |
| 279 |
# 40fpeoUgMdSBGtSL0jY2PHHRCvVscFYjSm6tQQ== root@sonic |
| 280 |
# ---------------------------------------------------------------------- |
| 281 |
|
| 282 |
# Using Deleted |
| 283 |
# |
| 284 |
# Before state: |
| 285 |
# ------------- |
| 286 |
# |
| 287 |
# sonic# show users sshKey |
| 288 |
# User: user1 |
| 289 |
# SSH Key: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDHD2TJqQ/Fve+fG5i6heOJH55wgbEf+7kTtUGBUtNttQ9OXgBRr |
| 290 |
# A2h2GKSyUNlyfaVijBrcr2MKwhSASvk58WnEqZhfmmhRDsdNVXPlMQuDBheIlCaXyOh+URJZCfmfeERSVO7kjRhqM |
| 291 |
# mbHlpTbMDHdFgYEvHGcrHMwIZyZ6KbBw== root@sonic |
| 292 |
# ---------------------------------------------------------------------- |
| 293 |
|
| 294 |
- name: Delete sshkey for existing users |
| 295 |
dellemc.enterprise_sonic.sonic_users: |
| 296 |
config: |
| 297 |
- name: user1 |
| 298 |
ssh_key: |
| 299 |
state: deleted |
| 300 |
|
| 301 |
# After state: |
| 302 |
# ------------ |
| 303 |
# |
| 304 |
# sonic# show users sshkey |
| 305 |
# ---------------------------------------------------------------------- |
| 306 |
""" |
| 307 |
|
| 308 |
RETURN = """ |
| 309 |
before: |
| 310 |
description: The configuration prior to the module invocation. |
| 311 |
returned: always |
| 312 |
type: list |
| 313 |
sample: > |
| 314 |
The configuration returned will always be in the same format |
| 315 |
as the parameters above. |
| 316 |
after: |
| 317 |
description: The resulting configuration module invocation. |
| 318 |
returned: when changed |
| 319 |
type: list |
| 320 |
sample: > |
| 321 |
The configuration returned will always be in the same format |
| 322 |
as the parameters above. |
| 323 |
after_generated: |
| 324 |
description: The generated configuration module invocation. |
| 325 |
returned: when C(check_mode) |
| 326 |
type: list |
| 327 |
sample: > |
| 328 |
The configuration returned will always be in the same format |
| 329 |
as the parameters above. |
| 330 |
commands: |
| 331 |
description: The set of commands pushed to the remote device. |
| 332 |
returned: always |
| 333 |
type: list |
| 334 |
sample: ['command 1', 'command 2', 'command 3'] |
| 335 |
""" |
| 336 |
|
| 337 |
|
| 338 |
from ansible.module_utils.basic import AnsibleModule |
| 339 |
from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.argspec.users.users import UsersArgs |
| 340 |
from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.config.users.users import Users |
| 341 |
|
| 342 |
|
| 343 |
def main(): |
| 344 |
""" |
| 345 |
Main entry point for module execution |
| 346 |
|
| 347 |
:returns: the result form module invocation |
| 348 |
""" |
| 349 |
module = AnsibleModule(argument_spec=UsersArgs.argument_spec, |
| 350 |
supports_check_mode=True) |
| 351 |
|
| 352 |
result = Users(module).execute_module() |
| 353 |
module.exit_json(**result) |
| 354 |
|
| 355 |
|
| 356 |
if __name__ == '__main__': |
| 357 |
main() |