| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# This file is part of Ansible |
| 4 |
# |
| 5 |
# Ansible is free software: you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Ansible is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
# |
| 18 |
from __future__ import absolute_import, division, print_function |
| 19 |
|
| 20 |
|
| 21 |
__metaclass__ = type |
| 22 |
|
| 23 |
DOCUMENTATION = """ |
| 24 |
module: ios_user |
| 25 |
author: Trishna Guha (@trishnaguha) |
| 26 |
short_description: Module to manage the aggregates of local users. |
| 27 |
description: |
| 28 |
- This module provides declarative management of the local usernames configured on |
| 29 |
network devices. It allows playbooks to manage either individual usernames or the |
| 30 |
aggregate of usernames in the current running config. It also supports purging usernames |
| 31 |
from the configuration that are not explicitly defined. |
| 32 |
version_added: 1.0.0 |
| 33 |
notes: |
| 34 |
- Tested against Cisco IOSXE Version 17.3 on CML. |
| 35 |
- This module works with connection C(network_cli). |
| 36 |
See U(https://docs.ansible.com/ansible/latest/network/user_guide/platform_ios.html) |
| 37 |
options: |
| 38 |
aggregate: |
| 39 |
description: |
| 40 |
- The set of username objects to be configured on the remote Cisco IOS device. |
| 41 |
The list entries can either be the username or a hash of username and properties. |
| 42 |
This argument is mutually exclusive with the C(name) argument. |
| 43 |
aliases: |
| 44 |
- users |
| 45 |
- collection |
| 46 |
type: list |
| 47 |
elements: dict |
| 48 |
suboptions: |
| 49 |
name: |
| 50 |
description: |
| 51 |
- The username to be configured on the Cisco IOS device. This argument accepts |
| 52 |
a string value and is mutually exclusive with the C(aggregate) argument. Please |
| 53 |
note that this option is not same as C(provider username). |
| 54 |
type: str |
| 55 |
required: true |
| 56 |
configured_password: |
| 57 |
description: |
| 58 |
- The password to be configured on the Cisco IOS device. The password needs to |
| 59 |
be provided in clear and it will be encrypted on the device. Please note that |
| 60 |
this option is not same as C(provider password). |
| 61 |
type: str |
| 62 |
update_password: |
| 63 |
description: |
| 64 |
- Since passwords are encrypted in the device running config, this argument will |
| 65 |
instruct the module when to change the password. When set to C(always), the |
| 66 |
password will always be updated in the device and when set to C(on_create) the |
| 67 |
password will be updated only if the username is created. |
| 68 |
choices: |
| 69 |
- on_create |
| 70 |
- always |
| 71 |
type: str |
| 72 |
password_type: |
| 73 |
description: |
| 74 |
- This argument determines whether a 'password' or 'secret' will be configured. |
| 75 |
choices: |
| 76 |
- secret |
| 77 |
- password |
| 78 |
type: str |
| 79 |
hashed_password: |
| 80 |
description: |
| 81 |
- This option allows configuring hashed passwords on Cisco IOS devices. |
| 82 |
type: dict |
| 83 |
suboptions: |
| 84 |
type: |
| 85 |
description: |
| 86 |
- Specifies the type of hash (e.g., 5 for MD5, 8 for PBKDF2, etc.) |
| 87 |
- For this to work, the device needs to support the desired hash type |
| 88 |
type: int |
| 89 |
required: true |
| 90 |
value: |
| 91 |
description: |
| 92 |
- The actual hashed password to be configured on the device |
| 93 |
required: true |
| 94 |
type: str |
| 95 |
privilege: |
| 96 |
description: |
| 97 |
- The C(privilege) argument configures the privilege level of the user when logged |
| 98 |
into the system. This argument accepts integer values in the range of 1 to 15. |
| 99 |
type: int |
| 100 |
view: |
| 101 |
description: |
| 102 |
- Configures the view for the username in the device running configuration. The |
| 103 |
argument accepts a string value defining the view name. This argument does not |
| 104 |
check if the view has been configured on the device. |
| 105 |
aliases: |
| 106 |
- role |
| 107 |
type: str |
| 108 |
sshkey: |
| 109 |
description: |
| 110 |
- Specifies upto two SSH public key(s) to configure for the given username. |
| 111 |
- This argument accepts a valid SSH key value. |
| 112 |
type: list |
| 113 |
elements: str |
| 114 |
purge_keys: |
| 115 |
description: |
| 116 |
- Since cisco.ios devices have a max limit of 2 keys per user, this parameter allows |
| 117 |
removal of keys that are not passed in sshkey parameter as to write new keys being passed. |
| 118 |
- It will remove any previously configured sshkeys for a user that doesn't match with the provided sshkey list, |
| 119 |
with an exception for the `admin` user. |
| 120 |
- Works only with 'present' state and when 'sshkey' is defined |
| 121 |
type: bool |
| 122 |
nopassword: |
| 123 |
description: |
| 124 |
- Defines the username without assigning a password. This will allow the user |
| 125 |
to login to the system without being authenticated by a password. |
| 126 |
type: bool |
| 127 |
state: |
| 128 |
description: |
| 129 |
- Configures the state of the username definition as it relates to the device |
| 130 |
operational configuration. When set to I(present), the username(s) should be |
| 131 |
configured in the device active configuration and when set to I(absent) the |
| 132 |
username(s) should not be in the device active configuration |
| 133 |
choices: |
| 134 |
- present |
| 135 |
- absent |
| 136 |
type: str |
| 137 |
name: |
| 138 |
description: |
| 139 |
- The username to be configured on the Cisco IOS device. This argument accepts |
| 140 |
a string value and is mutually exclusive with the C(aggregate) argument. Please |
| 141 |
note that this option is not same as C(provider username). |
| 142 |
type: str |
| 143 |
configured_password: |
| 144 |
description: |
| 145 |
- The password to be configured on the Cisco IOS device. The password needs to |
| 146 |
be provided in clear and it will be encrypted on the device. Please note that |
| 147 |
this option is not same as C(provider password). |
| 148 |
type: str |
| 149 |
update_password: |
| 150 |
description: |
| 151 |
- Since passwords are encrypted in the device running config, this argument will |
| 152 |
instruct the module when to change the password. When set to C(always), the |
| 153 |
password will always be updated in the device and when set to C(on_create) the |
| 154 |
password will be updated only if the username is created. |
| 155 |
default: always |
| 156 |
choices: |
| 157 |
- on_create |
| 158 |
- always |
| 159 |
type: str |
| 160 |
password_type: |
| 161 |
description: |
| 162 |
- This argument determines whether a 'password' or 'secret' will be configured. |
| 163 |
default: secret |
| 164 |
choices: |
| 165 |
- secret |
| 166 |
- password |
| 167 |
type: str |
| 168 |
hashed_password: |
| 169 |
description: |
| 170 |
- This option allows configuring hashed passwords on Cisco IOS devices. |
| 171 |
type: dict |
| 172 |
suboptions: |
| 173 |
type: |
| 174 |
description: |
| 175 |
- Specifies the type of hash (e.g., 5 for MD5, 8 for PBKDF2, etc.) |
| 176 |
- For this to work, the device needs to support the desired hash type |
| 177 |
type: int |
| 178 |
required: true |
| 179 |
value: |
| 180 |
description: |
| 181 |
- The actual hashed password to be configured on the device |
| 182 |
required: true |
| 183 |
type: str |
| 184 |
privilege: |
| 185 |
description: |
| 186 |
- The C(privilege) argument configures the privilege level of the user when logged |
| 187 |
into the system. This argument accepts integer values in the range of 1 to 15. |
| 188 |
type: int |
| 189 |
view: |
| 190 |
description: |
| 191 |
- Configures the view for the username in the device running configuration. The |
| 192 |
argument accepts a string value defining the view name. This argument does not |
| 193 |
check if the view has been configured on the device. |
| 194 |
aliases: |
| 195 |
- role |
| 196 |
type: str |
| 197 |
sshkey: |
| 198 |
description: |
| 199 |
- Specifies upto two SSH public key(s) to configure for the given username. |
| 200 |
- This argument accepts a valid SSH key value. |
| 201 |
type: list |
| 202 |
elements: str |
| 203 |
purge_keys: |
| 204 |
description: |
| 205 |
- Since cisco.ios devices have a max limit of 2 keys per user, this parameter allows |
| 206 |
removal of keys that are not passed in sshkey parameter as to write new keys being passed. |
| 207 |
- It will remove any previously configured sshkeys for a user that doesn't match with the provided sshkey list, |
| 208 |
with an exception for the `admin` user. |
| 209 |
- Works only with 'present' state and when 'sshkey' is defined |
| 210 |
type: bool |
| 211 |
default: false |
| 212 |
nopassword: |
| 213 |
description: |
| 214 |
- Defines the username without assigning a password. This will allow the user |
| 215 |
to login to the system without being authenticated by a password. |
| 216 |
type: bool |
| 217 |
purge: |
| 218 |
description: |
| 219 |
- Instructs the module to consider the resource definition absolute. It will remove |
| 220 |
any previously configured usernames on the device with the exception of the |
| 221 |
`admin` user (the current defined set of users). |
| 222 |
type: bool |
| 223 |
default: false |
| 224 |
state: |
| 225 |
description: |
| 226 |
- Configures the state of the username definition as it relates to the device |
| 227 |
operational configuration. When set to I(present), the username(s) should be |
| 228 |
configured in the device active configuration and when set to I(absent) the |
| 229 |
username(s) should not be in the device active configuration |
| 230 |
default: present |
| 231 |
choices: |
| 232 |
- present |
| 233 |
- absent |
| 234 |
type: str |
| 235 |
extends_documentation_fragment: |
| 236 |
- cisco.ios.ios |
| 237 |
""" |
| 238 |
|
| 239 |
EXAMPLES = """ |
| 240 |
# Using state: present |
| 241 |
|
| 242 |
# Before state: |
| 243 |
# ------------- |
| 244 |
|
| 245 |
# router-ios#show running-config | section ^username |
| 246 |
# username testuser privilege 15 password 0 password |
| 247 |
|
| 248 |
# Present state create a new user play: |
| 249 |
# ------------------------------------- |
| 250 |
|
| 251 |
- name: Create a new user |
| 252 |
cisco.ios.ios_user: |
| 253 |
name: ansible |
| 254 |
nopassword: true |
| 255 |
sshkey: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}" |
| 256 |
state: present |
| 257 |
|
| 258 |
# Task Output |
| 259 |
# ----------- |
| 260 |
|
| 261 |
# commands: |
| 262 |
# - ip ssh pubkey-chain |
| 263 |
# - username ansible |
| 264 |
# - key-hash ssh-rsa 2ABB27BBC33ED53EF7D55037952ABB27 test@fedora |
| 265 |
# - exit |
| 266 |
# - exit |
| 267 |
# - username ansible nopassword |
| 268 |
|
| 269 |
# After state: |
| 270 |
# ------------ |
| 271 |
|
| 272 |
# router-ios#show running-config | section username |
| 273 |
# username testuser privilege 15 password 0 password |
| 274 |
# username ansible nopassword |
| 275 |
# username ansible |
| 276 |
# key-hash ssh-rsa 2ABB27BBC33ED53EF7D55037952ABB27 test@fedora |
| 277 |
|
| 278 |
# Using state: present |
| 279 |
|
| 280 |
# Before state: |
| 281 |
# ------------- |
| 282 |
|
| 283 |
# router-ios#show running-config | section ^username |
| 284 |
# username testuser privilege 15 password 0 password |
| 285 |
|
| 286 |
# Present state create a new user with multiple keys play: |
| 287 |
# -------------------------------------------------------- |
| 288 |
|
| 289 |
- name: Create a new user with multiple keys |
| 290 |
cisco.ios.ios_user: |
| 291 |
name: ansible |
| 292 |
sshkey: |
| 293 |
- "{{ lookup('file', '~/.ssh/id_rsa.pub') }}" |
| 294 |
- "{{ lookup('file', '~/path/to/public_key') }}" |
| 295 |
state: present |
| 296 |
|
| 297 |
# Task Output |
| 298 |
# ----------- |
| 299 |
|
| 300 |
# commands: |
| 301 |
# - ip ssh pubkey-chain |
| 302 |
# - username ansible |
| 303 |
# - key-hash ssh-rsa 2ABB27BBC33ED53EF7D55037952ABB27 test@fedora |
| 304 |
# - key-hash ssh-rsa 1985673DCF7FA9A0F374BB97DC2ABB27 test@fedora |
| 305 |
# - exit |
| 306 |
# - exit |
| 307 |
|
| 308 |
# After state: |
| 309 |
# ------------ |
| 310 |
|
| 311 |
# router-ios#show running-config | section username |
| 312 |
# username testuser privilege 15 password 0 password |
| 313 |
# username ansible |
| 314 |
# key-hash ssh-rsa 2ABB27BBC33ED53EF7D55037952ABB27 test@fedora |
| 315 |
# key-hash ssh-rsa 1985673DCF7FA9A0F374BB97DC2ABB27 test@fedora |
| 316 |
|
| 317 |
# Using purge_keys: true |
| 318 |
|
| 319 |
# Before state: |
| 320 |
# ------------- |
| 321 |
|
| 322 |
# router-ios#show running-config | section ^username |
| 323 |
# username admin privilege 15 password 0 password |
| 324 |
# username testuser privilege 15 password 0 password |
| 325 |
# username ansible nopassword |
| 326 |
# username ansible |
| 327 |
# key-hash ssh-rsa 2ABB27BBC33ED53EF7D55037952ABB27 test@fedora |
| 328 |
|
| 329 |
# Purge existing keys for a user and write new keys: |
| 330 |
# ---------------------------------- |
| 331 |
|
| 332 |
- name: Update user by adding new key and purging existing keys |
| 333 |
cisco.ios.ios_user: |
| 334 |
name: ansible |
| 335 |
sshkey: |
| 336 |
- "{{ lookup('file', '~/path/to/public_key') }}" |
| 337 |
purge_keys: true |
| 338 |
|
| 339 |
# Task Output |
| 340 |
# ----------- |
| 341 |
|
| 342 |
# commands: |
| 343 |
# - ip ssh pubkey-chain |
| 344 |
# - username ansible |
| 345 |
# - no key-hash ssh-rsa 2ABB27BBC33ED53EF7D55037952ABB27 test@fedora |
| 346 |
# - exit |
| 347 |
# - exit |
| 348 |
# - ip ssh pubkey-chain |
| 349 |
# - username ansible |
| 350 |
# - key-hash ssh-rsa B2C881222DB43D58A229EDF19D2228A2 test2@fedora |
| 351 |
# - exit |
| 352 |
# - exit |
| 353 |
|
| 354 |
# After state: |
| 355 |
# ------------ |
| 356 |
|
| 357 |
# router-ios#show running-config | section username |
| 358 |
# username admin privilege 15 password 0 password |
| 359 |
# username ansible nopassword |
| 360 |
# username ansible |
| 361 |
# key-hash ssh-rsa B2C881222DB43D58A229EDF19D2228A2 test2@fedora |
| 362 |
|
| 363 |
# Using Purge: true |
| 364 |
|
| 365 |
# Before state: |
| 366 |
# ------------- |
| 367 |
|
| 368 |
# router-ios#show running-config | section ^username |
| 369 |
# username admin privilege 15 password 0 password |
| 370 |
# username testuser privilege 15 password 0 password |
| 371 |
# username ansible nopassword |
| 372 |
# username ansible |
| 373 |
# key-hash ssh-rsa 2ABB27BBC33ED53EF7D55037952ABB27 test@fedora |
| 374 |
|
| 375 |
# Purge all users except admin play: |
| 376 |
# ---------------------------------- |
| 377 |
|
| 378 |
- name: Remove all users except admin |
| 379 |
cisco.ios.ios_user: |
| 380 |
purge: true |
| 381 |
|
| 382 |
# Task Output |
| 383 |
# ----------- |
| 384 |
|
| 385 |
# commands: |
| 386 |
# - no username testuser |
| 387 |
# - no username ansible |
| 388 |
# - ip ssh pubkey-chain |
| 389 |
# - no username ansible |
| 390 |
# - exit |
| 391 |
|
| 392 |
# After state: |
| 393 |
# ------------ |
| 394 |
|
| 395 |
# router-ios#show running-config | section username |
| 396 |
# username admin privilege 15 password 0 password |
| 397 |
|
| 398 |
# Using Purge: true |
| 399 |
|
| 400 |
# Before state: |
| 401 |
# ------------- |
| 402 |
|
| 403 |
# router-ios#show running-config | section ^username |
| 404 |
# username admin privilege 15 password 0 password |
| 405 |
# username testuser privilege 15 password 0 password1 |
| 406 |
# username testuser1 privilege 15 password 0 password2 |
| 407 |
# username ansible nopassword |
| 408 |
|
| 409 |
# Purge all users except admin and these listed users play: |
| 410 |
# --------------------------------------------------------- |
| 411 |
|
| 412 |
- name: Remove all users except admin and these listed users |
| 413 |
cisco.ios.ios_user: |
| 414 |
aggregate: |
| 415 |
- name: testuser |
| 416 |
- name: testuser1 |
| 417 |
purge: true |
| 418 |
|
| 419 |
# Task Output |
| 420 |
# ----------- |
| 421 |
|
| 422 |
|
| 423 |
# commands: |
| 424 |
# - no username ansible |
| 425 |
|
| 426 |
# After state: |
| 427 |
# ------------ |
| 428 |
|
| 429 |
# router-ios#show running-config | section username |
| 430 |
# username admin privilege 15 password 0 password |
| 431 |
# username testuser privilege 15 password 0 password1 |
| 432 |
# username testuser1 privilege 15 password 0 password2 |
| 433 |
|
| 434 |
# Using state: present |
| 435 |
|
| 436 |
# Before state: |
| 437 |
# ------------- |
| 438 |
|
| 439 |
# router-ios#show running-config | section ^username |
| 440 |
# username admin privilege 15 password 0 password |
| 441 |
# username netop password 0 password1 |
| 442 |
# username netend password 0 password2 |
| 443 |
|
| 444 |
# Present state set multiple users to privilege level 15 play: |
| 445 |
# ------------------------------------------------------------ |
| 446 |
|
| 447 |
- name: Set multiple users to privilege level 15 |
| 448 |
cisco.ios.ios_user: |
| 449 |
aggregate: |
| 450 |
- name: netop |
| 451 |
- name: netend |
| 452 |
privilege: 15 |
| 453 |
state: present |
| 454 |
|
| 455 |
# Task Output |
| 456 |
# ----------- |
| 457 |
|
| 458 |
# commands: |
| 459 |
# - username netop privilege 15 |
| 460 |
# - username netend privilege 15 |
| 461 |
|
| 462 |
# After state: |
| 463 |
# ------------ |
| 464 |
|
| 465 |
# router-ios#show running-config | section username |
| 466 |
# username admin privilege 15 password 0 password |
| 467 |
# username netop privilege 15 password 0 password1 |
| 468 |
# username netend privilege 15 password 0 password2 |
| 469 |
|
| 470 |
# Using state: present |
| 471 |
|
| 472 |
# Before state: |
| 473 |
# ------------- |
| 474 |
|
| 475 |
# router-ios#show running-config | section ^username |
| 476 |
# username admin privilege 15 password 0 password |
| 477 |
# username netop privilege 15 password 0 oldpassword |
| 478 |
|
| 479 |
# Present state Change Password for User netop play: |
| 480 |
# -------------------------------------------- |
| 481 |
|
| 482 |
- name: Change Password for User netop |
| 483 |
cisco.ios.ios_user: |
| 484 |
name: netop |
| 485 |
configured_password: "newpassword" |
| 486 |
password_type: password |
| 487 |
update_password: always |
| 488 |
state: present |
| 489 |
|
| 490 |
# Task Output |
| 491 |
# ----------- |
| 492 |
|
| 493 |
# commands: |
| 494 |
# - username netop password newpassword |
| 495 |
|
| 496 |
# After state: |
| 497 |
# ------------ |
| 498 |
|
| 499 |
# router-ios#show running-config | section username |
| 500 |
# username admin privilege 15 password 0 password |
| 501 |
# username netop privilege 15 password 0 newpassword |
| 502 |
|
| 503 |
# Using state: present |
| 504 |
|
| 505 |
# Before state: |
| 506 |
# ------------- |
| 507 |
|
| 508 |
# router-ios#show running-config | section ^username |
| 509 |
# username admin privilege 15 password 0 password |
| 510 |
# username netop privilege 15 password 0 password |
| 511 |
# username netend privilege 15 password 0 password |
| 512 |
|
| 513 |
# Present state set user view/role for users play: |
| 514 |
# -------------------------------------------- |
| 515 |
|
| 516 |
- name: Set user view/role for users |
| 517 |
cisco.ios.ios_user: |
| 518 |
aggregate: |
| 519 |
- name: netop |
| 520 |
- name: netend |
| 521 |
view: network-admin |
| 522 |
state: present |
| 523 |
|
| 524 |
# Task Output |
| 525 |
# ----------- |
| 526 |
|
| 527 |
# commands: |
| 528 |
# - username netop view network-admin |
| 529 |
# - username netend view network-admin |
| 530 |
|
| 531 |
# After state: |
| 532 |
# ------------ |
| 533 |
|
| 534 |
# router-ios#show running-config | section username |
| 535 |
# username admin privilege 15 password 0 password |
| 536 |
# username netop privilege 15 view network-admin password 0 password |
| 537 |
# username netend privilege 15 view network-admin password 0 password |
| 538 |
|
| 539 |
# Using state: present |
| 540 |
|
| 541 |
# Before state: |
| 542 |
# ------------- |
| 543 |
|
| 544 |
# router-ios#show running-config | section ^username |
| 545 |
# username admin privilege 15 password 0 password |
| 546 |
|
| 547 |
# Present state create a new user with hashed password play: |
| 548 |
# -------------------------------------------------------------- |
| 549 |
|
| 550 |
- name: Create a new user with hashed password |
| 551 |
cisco.ios.ios_user: |
| 552 |
name: ansibletest5 |
| 553 |
hashed_password: |
| 554 |
type: 9 |
| 555 |
value: "thiswillbereplacedwithhashedpassword" |
| 556 |
state: present |
| 557 |
|
| 558 |
# Task Output |
| 559 |
# ----------- |
| 560 |
|
| 561 |
# commands: |
| 562 |
# - username ansibletest5 secret 9 thiswillbereplacedwithhashedpassword |
| 563 |
|
| 564 |
# After state: |
| 565 |
# ------------ |
| 566 |
|
| 567 |
# router-ios#show running-config | section username |
| 568 |
# username admin privilege 15 password 0 password |
| 569 |
# username ansibletest5 secret 9 thiswillbereplacedwithhashedpassword |
| 570 |
|
| 571 |
# Using state: absent |
| 572 |
|
| 573 |
# Before state: |
| 574 |
# ------------- |
| 575 |
|
| 576 |
# router-ios#show running-config | section ^username |
| 577 |
# username admin privilege 15 password 0 password |
| 578 |
# username ansibletest1 password 0 password |
| 579 |
# username ansibletest2 secret 9 thiswillbereplacedwithhashedpassword |
| 580 |
# username ansibletest3 password 5 thistoowillbereplacedwithhashedpassword |
| 581 |
|
| 582 |
# Absent state remove multiple users play: |
| 583 |
# ---------------------------------------- |
| 584 |
|
| 585 |
- name: Delete users with aggregate |
| 586 |
cisco.ios.ios_user: |
| 587 |
aggregate: |
| 588 |
- name: ansibletest1 |
| 589 |
- name: ansibletest2 |
| 590 |
- name: ansibletest3 |
| 591 |
state: absent |
| 592 |
|
| 593 |
# Task Output |
| 594 |
# ----------- |
| 595 |
|
| 596 |
# commands: |
| 597 |
# - no username ansibletest1 |
| 598 |
# - no username ansibletest2 |
| 599 |
# - no username ansibletest3 |
| 600 |
|
| 601 |
# After state: |
| 602 |
# ------------ |
| 603 |
|
| 604 |
# router-ios#show running-config | section username |
| 605 |
# username admin privilege 15 password 0 password |
| 606 |
""" |
| 607 |
|
| 608 |
RETURN = """ |
| 609 |
commands: |
| 610 |
description: The list of configuration mode commands to send to the device |
| 611 |
returned: always |
| 612 |
type: list |
| 613 |
sample: |
| 614 |
- username ansible secret password |
| 615 |
- username admin secret admin |
| 616 |
""" |
| 617 |
import base64 |
| 618 |
import hashlib |
| 619 |
import re |
| 620 |
|
| 621 |
from copy import deepcopy |
| 622 |
from functools import partial |
| 623 |
|
| 624 |
from ansible.module_utils.basic import AnsibleModule |
| 625 |
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( |
| 626 |
remove_default_spec, |
| 627 |
) |
| 628 |
|
| 629 |
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.ios import ( |
| 630 |
get_config, |
| 631 |
load_config, |
| 632 |
) |
| 633 |
|
| 634 |
|
| 635 |
def validate_privilege(value, module): |
| 636 |
if value and not 1 <= value <= 15: |
| 637 |
module.fail_json(msg="privilege must be between 1 and 15, got %s" % value) |
| 638 |
|
| 639 |
|
| 640 |
def user_del_cmd(username): |
| 641 |
return { |
| 642 |
"command": "no username %s" % username, |
| 643 |
"prompt": "This operation will remove all username related configurations with same name", |
| 644 |
"answer": "y", |
| 645 |
"newline": False, |
| 646 |
} |
| 647 |
|
| 648 |
|
| 649 |
def remove_ssh(command, want, have): |
| 650 |
""" |
| 651 |
Function responsible to add no key-hash commands |
| 652 |
Compares want and have via set operations |
| 653 |
""" |
| 654 |
want_keys_set = set(want.get("sshkey", [])) |
| 655 |
have_keys_set = set(have.get("sshkey", [])) |
| 656 |
|
| 657 |
keys_to_be_removed = have_keys_set.difference(want_keys_set) |
| 658 |
if keys_to_be_removed: |
| 659 |
command.append("ip ssh pubkey-chain") |
| 660 |
command.append("username %s" % want["name"]) |
| 661 |
for item in keys_to_be_removed: |
| 662 |
command.append("no key-hash %s" % item) |
| 663 |
command.append("exit") |
| 664 |
command.append("exit") |
| 665 |
|
| 666 |
|
| 667 |
def add_ssh(command, want, x=None): |
| 668 |
command.append("ip ssh pubkey-chain") |
| 669 |
if x: |
| 670 |
command.append("username %s" % want["name"]) |
| 671 |
for item in x: |
| 672 |
command.append("key-hash %s" % item) |
| 673 |
command.append("exit") |
| 674 |
else: |
| 675 |
command.append("no username %s" % want["name"]) |
| 676 |
command.append("exit") |
| 677 |
|
| 678 |
|
| 679 |
def sshkey_fingerprint(sshkey): |
| 680 |
# IOS will accept a MD5 fingerprint of the public key |
| 681 |
# and is easier to configure in a single line |
| 682 |
# we calculate this fingerprint here |
| 683 |
if not sshkey: |
| 684 |
return None |
| 685 |
if " " in sshkey: |
| 686 |
# ssh-rsa AAA...== comment |
| 687 |
keyparts = sshkey.split(" ") |
| 688 |
keyparts[1] = hashlib.md5(base64.b64decode(keyparts[1])).hexdigest().upper() |
| 689 |
return " ".join(keyparts) |
| 690 |
else: |
| 691 |
# just the key, assume rsa type |
| 692 |
return "ssh-rsa %s" % hashlib.md5(base64.b64decode(sshkey)).hexdigest().upper() |
| 693 |
|
| 694 |
|
| 695 |
def map_obj_to_commands(updates, module): |
| 696 |
commands = list() |
| 697 |
update_password = module.params["update_password"] |
| 698 |
password_type = module.params["password_type"] |
| 699 |
|
| 700 |
def needs_update(want, have, x): |
| 701 |
return want.get(x) and want.get(x) != have.get(x) |
| 702 |
|
| 703 |
def add(command, want, x): |
| 704 |
command.append("username %s %s" % (want["name"], x)) |
| 705 |
|
| 706 |
def add_hashed_password(command, want, x, password_type): |
| 707 |
command.append( |
| 708 |
"username %s %s %s %s" % (want["name"], password_type, x.get("type"), x.get("value")), |
| 709 |
) |
| 710 |
|
| 711 |
for update in updates: |
| 712 |
want, have = update |
| 713 |
if want["state"] == "absent": |
| 714 |
if have["sshkey"]: |
| 715 |
add_ssh(commands, want) |
| 716 |
else: |
| 717 |
commands.append(user_del_cmd(want["name"])) |
| 718 |
if needs_update(want, have, "view"): |
| 719 |
add(commands, want, "view %s" % want["view"]) |
| 720 |
if needs_update(want, have, "privilege"): |
| 721 |
add(commands, want, "privilege %s" % want["privilege"]) |
| 722 |
if needs_update(want, have, "sshkey") and want["state"] == "present": |
| 723 |
purge_keys = want["purge_keys"] |
| 724 |
if purge_keys: |
| 725 |
if want["name"] == "admin": |
| 726 |
module.warn("Purging of admin ssh keys is not allowed") |
| 727 |
else: |
| 728 |
remove_ssh(commands, want, have) |
| 729 |
add_ssh(commands, want, want["sshkey"]) |
| 730 |
if needs_update(want, have, "configured_password"): |
| 731 |
if update_password == "always" or not have: |
| 732 |
if have and have["password_type"] and password_type != have["password_type"]: |
| 733 |
module.fail_json( |
| 734 |
msg="Can not have both a user password and a user secret." |
| 735 |
+ " Please choose one or the other.", |
| 736 |
) |
| 737 |
add(commands, want, "%s %s" % (password_type, want["configured_password"])) |
| 738 |
if needs_update(want, have, "hashed_password"): |
| 739 |
add_hashed_password(commands, want, want["hashed_password"], password_type) |
| 740 |
if needs_update(want, have, "nopassword"): |
| 741 |
if want["nopassword"]: |
| 742 |
add(commands, want, "nopassword") |
| 743 |
else: |
| 744 |
add(commands, want, user_del_cmd(want["name"])) |
| 745 |
return commands |
| 746 |
|
| 747 |
|
| 748 |
def parse_view(data): |
| 749 |
match = re.search("view (\\S+)", data, re.M) |
| 750 |
if match: |
| 751 |
return match.group(1) |
| 752 |
|
| 753 |
|
| 754 |
def parse_sshkey(data, user): |
| 755 |
sshregex = re.compile(r"username %s((?:\n\s+key-hash .+)+)" % user, re.M) |
| 756 |
sshcfg = re.search(sshregex, data) |
| 757 |
key_list = [] |
| 758 |
if sshcfg: |
| 759 |
match = re.findall(r"key-hash\s+(\S+\s+\S+(?:\s+\S+)?)$", sshcfg.group(), re.M) |
| 760 |
if match: |
| 761 |
key_list = match |
| 762 |
return key_list |
| 763 |
|
| 764 |
|
| 765 |
def parse_privilege(data): |
| 766 |
match = re.search("privilege (\\S+)", data, re.M) |
| 767 |
if match: |
| 768 |
return int(match.group(1)) |
| 769 |
|
| 770 |
|
| 771 |
def parse_password_type(data): |
| 772 |
type = None |
| 773 |
if data and data.split()[-3] in ["password", "secret"]: |
| 774 |
type = data.split()[-3] |
| 775 |
return type |
| 776 |
|
| 777 |
|
| 778 |
def map_config_to_obj(module): |
| 779 |
data = get_config(module, flags=["| section username"]) |
| 780 |
match = re.findall("^(?:u|\\s{1,2}u)sername (\\S+)", data, re.M) |
| 781 |
if not match: |
| 782 |
return list() |
| 783 |
instances = list() |
| 784 |
for user in set(match): |
| 785 |
regex = "username %s .+$" % user |
| 786 |
cfg = re.findall(regex, data, re.M) |
| 787 |
cfg = "\n".join(cfg) |
| 788 |
ssh_key_list = parse_sshkey(data, user) |
| 789 |
obj = { |
| 790 |
"name": user, |
| 791 |
"state": "present", |
| 792 |
"nopassword": "nopassword" in cfg, |
| 793 |
"configured_password": None, |
| 794 |
"hashed_password": None, |
| 795 |
"purge_keys": False, |
| 796 |
"password_type": parse_password_type(cfg), |
| 797 |
"sshkey": ssh_key_list, |
| 798 |
"is_only_ssh_user": False if cfg.strip() and ssh_key_list else True, |
| 799 |
"is_only_normal_user": True if cfg.strip() and ssh_key_list == [] else False, |
| 800 |
"privilege": parse_privilege(cfg), |
| 801 |
"view": parse_view(cfg), |
| 802 |
} |
| 803 |
instances.append(obj) |
| 804 |
return instances |
| 805 |
|
| 806 |
|
| 807 |
def get_param_value(key, item, module): |
| 808 |
# if key doesn't exist in the item, get it from module.params |
| 809 |
if not item.get(key): |
| 810 |
value = module.params[key] |
| 811 |
# if key does exist, do a type check on it to validate it |
| 812 |
else: |
| 813 |
value_type = module.argument_spec[key].get("type", "str") |
| 814 |
type_checker = module._CHECK_ARGUMENT_TYPES_DISPATCHER[value_type] |
| 815 |
type_checker(item[key]) |
| 816 |
value = item[key] |
| 817 |
# validate the param value (if validator func exists) |
| 818 |
validator = globals().get("validate_%s" % key) |
| 819 |
if all((value, validator)): |
| 820 |
validator(value, module) |
| 821 |
return value |
| 822 |
|
| 823 |
|
| 824 |
def map_params_to_obj(module): |
| 825 |
users = module.params["aggregate"] |
| 826 |
if not users: |
| 827 |
if not module.params["name"] and module.params["purge"]: |
| 828 |
return list() |
| 829 |
elif not module.params["name"]: |
| 830 |
module.fail_json(msg="username is required") |
| 831 |
else: |
| 832 |
aggregate = [{"name": module.params["name"]}] |
| 833 |
else: |
| 834 |
aggregate = list() |
| 835 |
for item in users: |
| 836 |
if not isinstance(item, dict): |
| 837 |
aggregate.append({"name": item}) |
| 838 |
elif "name" not in item: |
| 839 |
module.fail_json(msg="name is required") |
| 840 |
else: |
| 841 |
aggregate.append(item) |
| 842 |
objects = list() |
| 843 |
for item in aggregate: |
| 844 |
get_value = partial(get_param_value, item=item, module=module) |
| 845 |
item["configured_password"] = get_value("configured_password") |
| 846 |
item["hashed_password"] = get_value("hashed_password") |
| 847 |
item["nopassword"] = get_value("nopassword") |
| 848 |
item["privilege"] = get_value("privilege") |
| 849 |
item["view"] = get_value("view") |
| 850 |
item["purge_keys"] = get_value("purge_keys") |
| 851 |
item["sshkey"] = render_key_list(get_value("sshkey")) |
| 852 |
if len(item["sshkey"]) > 2: |
| 853 |
module.fail_json( |
| 854 |
msg="More than two ssh-keys supplied for a user. The length limit for ssh-keys is 2.", |
| 855 |
) |
| 856 |
item["state"] = get_value("state") |
| 857 |
objects.append(item) |
| 858 |
return objects |
| 859 |
|
| 860 |
|
| 861 |
def render_key_list(ssh_keys): |
| 862 |
key_list = [] |
| 863 |
if ssh_keys: |
| 864 |
for item in ssh_keys: |
| 865 |
key_list.append(sshkey_fingerprint(item)) |
| 866 |
return key_list |
| 867 |
|
| 868 |
|
| 869 |
def update_objects(want, have): |
| 870 |
updates = list() |
| 871 |
for entry in want: |
| 872 |
item = next((i for i in have if i["name"] == entry["name"]), None) |
| 873 |
if all((item is None, entry["state"] == "present")): |
| 874 |
updates.append((entry, {})) |
| 875 |
elif item: |
| 876 |
for key, value in entry.items(): |
| 877 |
if value and value != item[key]: |
| 878 |
updates.append((entry, item)) |
| 879 |
break |
| 880 |
return updates |
| 881 |
|
| 882 |
|
| 883 |
def find_set_difference(list1, list2, key): |
| 884 |
want_users = [x[key] for x in list1] |
| 885 |
have_users = [x[key] for x in list2] |
| 886 |
setdifference = set(have_users).difference(want_users) |
| 887 |
result = [item for item in list2 if item[key] in setdifference] |
| 888 |
return result |
| 889 |
|
| 890 |
|
| 891 |
def main(): |
| 892 |
"""main entry point for module execution""" |
| 893 |
hashed_password_spec = dict( |
| 894 |
type=dict(type="int", required=True), |
| 895 |
value=dict(no_log=True, required=True), |
| 896 |
) |
| 897 |
element_spec = dict( |
| 898 |
name=dict(), |
| 899 |
configured_password=dict(no_log=True), |
| 900 |
hashed_password=dict(no_log=True, type="dict", options=hashed_password_spec), |
| 901 |
nopassword=dict(type="bool"), |
| 902 |
update_password=dict(default="always", choices=["on_create", "always"]), |
| 903 |
password_type=dict(default="secret", choices=["secret", "password"]), |
| 904 |
privilege=dict(type="int"), |
| 905 |
view=dict(aliases=["role"]), |
| 906 |
sshkey=dict(type="list", elements="str", no_log=False), |
| 907 |
purge_keys=dict(type="bool", default=False), |
| 908 |
state=dict(default="present", choices=["present", "absent"]), |
| 909 |
) |
| 910 |
aggregate_spec = deepcopy(element_spec) |
| 911 |
aggregate_spec["name"] = dict(required=True) |
| 912 |
# remove default in aggregate spec, to handle common arguments |
| 913 |
remove_default_spec(aggregate_spec) |
| 914 |
argument_spec = dict( |
| 915 |
aggregate=dict( |
| 916 |
type="list", |
| 917 |
elements="dict", |
| 918 |
options=aggregate_spec, |
| 919 |
aliases=["users", "collection"], |
| 920 |
), |
| 921 |
purge=dict(type="bool", default=False), |
| 922 |
) |
| 923 |
argument_spec.update(element_spec) |
| 924 |
mutually_exclusive = [ |
| 925 |
("name", "aggregate"), |
| 926 |
("nopassword", "hashed_password", "configured_password"), |
| 927 |
] |
| 928 |
module = AnsibleModule( |
| 929 |
argument_spec=argument_spec, |
| 930 |
mutually_exclusive=mutually_exclusive, |
| 931 |
supports_check_mode=True, |
| 932 |
) |
| 933 |
warnings = list() |
| 934 |
result = {"changed": False, "warnings": warnings} |
| 935 |
want = map_params_to_obj(module) |
| 936 |
have = map_config_to_obj(module) |
| 937 |
|
| 938 |
commands = map_obj_to_commands(update_objects(want, have), module) |
| 939 |
if module.params["purge"]: |
| 940 |
setdifference = find_set_difference(want, have, "name") |
| 941 |
for item in setdifference: |
| 942 |
if item["name"] != "admin": |
| 943 |
if item["is_only_ssh_user"]: |
| 944 |
add_ssh(commands, item) |
| 945 |
if item["is_only_normal_user"]: |
| 946 |
commands.append(user_del_cmd(item["name"])) |
| 947 |
if item["is_only_normal_user"] is False and item["is_only_ssh_user"] is False: |
| 948 |
add_ssh(commands, item) |
| 949 |
commands.append(user_del_cmd(item["name"])) |
| 950 |
result["commands"] = commands |
| 951 |
if commands: |
| 952 |
if not module.check_mode: |
| 953 |
load_config(module, commands) |
| 954 |
result["changed"] = True |
| 955 |
module.exit_json(**result) |
| 956 |
|
| 957 |
|
| 958 |
if __name__ == "__main__": |
| 959 |
main() |