| 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 |
|
| 24 |
DOCUMENTATION = """ |
| 25 |
module: nxos_user |
| 26 |
extends_documentation_fragment: |
| 27 |
- cisco.nxos.nxos |
| 28 |
author: Peter Sprygada (@privateip) |
| 29 |
notes: |
| 30 |
- Limited Support for Cisco MDS |
| 31 |
short_description: Manage the collection of local users on Nexus devices |
| 32 |
description: |
| 33 |
- This module provides declarative management of the local usernames configured on |
| 34 |
Cisco Nexus devices. It allows playbooks to manage either individual usernames |
| 35 |
or the collection of usernames in the current running config. It also supports |
| 36 |
purging usernames from the configuration that are not explicitly defined. |
| 37 |
version_added: 1.0.0 |
| 38 |
options: |
| 39 |
aggregate: |
| 40 |
description: |
| 41 |
- The set of username objects to be configured on the remote Cisco Nexus device. The |
| 42 |
list entries can either be the username or a hash of username and properties. This |
| 43 |
argument is mutually exclusive with the C(name) argument. |
| 44 |
aliases: |
| 45 |
- users |
| 46 |
- collection |
| 47 |
type: list |
| 48 |
elements: dict |
| 49 |
suboptions: |
| 50 |
name: |
| 51 |
description: |
| 52 |
- The username to be configured on the remote Cisco Nexus device. This argument |
| 53 |
accepts a string value and is mutually exclusive with the C(aggregate) argument. |
| 54 |
type: str |
| 55 |
configured_password: |
| 56 |
description: |
| 57 |
- The password to be configured on the network device. The password needs to be |
| 58 |
provided in cleartext and it will be encrypted on the device. |
| 59 |
type: str |
| 60 |
hashed_password: |
| 61 |
description: |
| 62 |
- The hashed password to be configured on the network device. The password needs to |
| 63 |
already be encrypted. |
| 64 |
type: str |
| 65 |
update_password: |
| 66 |
description: |
| 67 |
- Since passwords are encrypted in the device running config, this argument will |
| 68 |
instruct the module when to change the password. When set to C(always), the |
| 69 |
password will always be updated in the device and when set to C(on_create) the |
| 70 |
password will be updated only if the username is created. |
| 71 |
choices: |
| 72 |
- on_create |
| 73 |
- always |
| 74 |
type: str |
| 75 |
roles: |
| 76 |
description: |
| 77 |
- The C(role) argument configures the role for the username in the device running |
| 78 |
configuration. The argument accepts a string value defining the role name. This |
| 79 |
argument does not check if the role has been configured on the device. |
| 80 |
aliases: |
| 81 |
- role |
| 82 |
type: list |
| 83 |
elements: str |
| 84 |
sshkey: |
| 85 |
description: |
| 86 |
- The C(sshkey) argument defines the SSH public key to configure for the username. This |
| 87 |
argument accepts a valid SSH key value. |
| 88 |
type: str |
| 89 |
state: |
| 90 |
description: |
| 91 |
- The C(state) argument configures the state of the username definition as it |
| 92 |
relates to the device operational configuration. When set to I(present), the |
| 93 |
username(s) should be configured in the device active configuration and when |
| 94 |
set to I(absent) the username(s) should not be in the device active configuration |
| 95 |
choices: |
| 96 |
- present |
| 97 |
- absent |
| 98 |
type: str |
| 99 |
name: |
| 100 |
description: |
| 101 |
- The username to be configured on the remote Cisco Nexus device. This argument |
| 102 |
accepts a string value and is mutually exclusive with the C(aggregate) argument. |
| 103 |
type: str |
| 104 |
configured_password: |
| 105 |
description: |
| 106 |
- The password to be configured on the network device. The password needs to be |
| 107 |
provided in cleartext and it will be encrypted on the device. |
| 108 |
type: str |
| 109 |
hashed_password: |
| 110 |
description: |
| 111 |
- The hashed password to be configured on the network device. The password needs to |
| 112 |
already be encrypted. |
| 113 |
type: str |
| 114 |
update_password: |
| 115 |
description: |
| 116 |
- Since passwords are encrypted in the device running config, this argument will |
| 117 |
instruct the module when to change the password. When set to C(always), the |
| 118 |
password will always be updated in the device and when set to C(on_create) the |
| 119 |
password will be updated only if the username is created. |
| 120 |
default: always |
| 121 |
choices: |
| 122 |
- on_create |
| 123 |
- always |
| 124 |
type: str |
| 125 |
roles: |
| 126 |
description: |
| 127 |
- The C(role) argument configures the role for the username in the device running |
| 128 |
configuration. The argument accepts a string value defining the role name. This |
| 129 |
argument does not check if the role has been configured on the device. |
| 130 |
aliases: |
| 131 |
- role |
| 132 |
type: list |
| 133 |
elements: str |
| 134 |
sshkey: |
| 135 |
description: |
| 136 |
- The C(sshkey) argument defines the SSH public key to configure for the username. This |
| 137 |
argument accepts a valid SSH key value. |
| 138 |
type: str |
| 139 |
purge: |
| 140 |
description: |
| 141 |
- The C(purge) argument instructs the module to consider the resource definition |
| 142 |
absolute. It will remove any previously configured usernames on the device |
| 143 |
with the exception of the `admin` user which cannot be deleted per nxos constraints. |
| 144 |
type: bool |
| 145 |
default: false |
| 146 |
state: |
| 147 |
description: |
| 148 |
- The C(state) argument configures the state of the username definition as it |
| 149 |
relates to the device operational configuration. When set to I(present), the |
| 150 |
username(s) should be configured in the device active configuration and when |
| 151 |
set to I(absent) the username(s) should not be in the device active configuration |
| 152 |
default: present |
| 153 |
choices: |
| 154 |
- present |
| 155 |
- absent |
| 156 |
type: str |
| 157 |
""" |
| 158 |
|
| 159 |
EXAMPLES = """ |
| 160 |
- name: create a new user |
| 161 |
cisco.nxos.nxos_user: |
| 162 |
name: ansible |
| 163 |
sshkey: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}" |
| 164 |
state: present |
| 165 |
|
| 166 |
- name: remove all users except admin |
| 167 |
cisco.nxos.nxos_user: |
| 168 |
purge: true |
| 169 |
|
| 170 |
- name: set multiple users role |
| 171 |
cisco.nxos.nxos_user: |
| 172 |
aggregate: |
| 173 |
- name: netop |
| 174 |
- name: netend |
| 175 |
role: network-operator |
| 176 |
state: present |
| 177 |
""" |
| 178 |
|
| 179 |
RETURN = """ |
| 180 |
commands: |
| 181 |
description: The list of configuration mode commands to send to the device |
| 182 |
returned: always |
| 183 |
type: list |
| 184 |
sample: |
| 185 |
- name ansible |
| 186 |
- name ansible password password |
| 187 |
""" |
| 188 |
import re |
| 189 |
|
| 190 |
from copy import deepcopy |
| 191 |
from functools import partial |
| 192 |
|
| 193 |
from ansible.module_utils.basic import AnsibleModule |
| 194 |
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( |
| 195 |
remove_default_spec, |
| 196 |
to_list, |
| 197 |
) |
| 198 |
|
| 199 |
from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxos import ( |
| 200 |
get_config, |
| 201 |
load_config, |
| 202 |
run_commands, |
| 203 |
) |
| 204 |
|
| 205 |
|
| 206 |
BUILTIN_ROLES = [ |
| 207 |
"network-admin", |
| 208 |
"network-operator", |
| 209 |
"vdc-admin", |
| 210 |
"vdc-operator", |
| 211 |
"priv-15", |
| 212 |
"priv-14", |
| 213 |
"priv-13", |
| 214 |
"priv-12", |
| 215 |
"priv-11", |
| 216 |
"priv-10", |
| 217 |
"priv-9", |
| 218 |
"priv-8", |
| 219 |
"priv-7", |
| 220 |
"priv-6", |
| 221 |
"priv-5", |
| 222 |
"priv-4", |
| 223 |
"priv-3", |
| 224 |
"priv-2", |
| 225 |
"priv-1", |
| 226 |
"priv-0", |
| 227 |
"dev-ops", |
| 228 |
] |
| 229 |
|
| 230 |
|
| 231 |
def get_custom_roles(module): |
| 232 |
return re.findall( |
| 233 |
r"^role name (\S+)", |
| 234 |
get_config(module, flags=["| include '^role name'"]), |
| 235 |
re.M, |
| 236 |
) |
| 237 |
|
| 238 |
|
| 239 |
def validate_roles(value, module): |
| 240 |
valid_roles = BUILTIN_ROLES + get_custom_roles(module) |
| 241 |
for item in value: |
| 242 |
if item not in valid_roles: |
| 243 |
module.fail_json(msg="invalid role specified") |
| 244 |
|
| 245 |
|
| 246 |
def map_obj_to_commands(updates, module): |
| 247 |
commands = list() |
| 248 |
update_password = module.params["update_password"] |
| 249 |
|
| 250 |
for update in updates: |
| 251 |
want, have = update |
| 252 |
|
| 253 |
def needs_update(x): |
| 254 |
return want.get(x) and (want.get(x) != have.get(x)) |
| 255 |
|
| 256 |
def add(x): |
| 257 |
return commands.append("username %s %s" % (want["name"], x)) |
| 258 |
|
| 259 |
def remove(x): |
| 260 |
return commands.append("no username %s %s" % (want["name"], x)) |
| 261 |
|
| 262 |
def configure_roles(): |
| 263 |
if want["roles"]: |
| 264 |
if have: |
| 265 |
for item in set(have["roles"]).difference(want["roles"]): |
| 266 |
remove("role %s" % item) |
| 267 |
|
| 268 |
for item in set(want["roles"]).difference(have["roles"]): |
| 269 |
add("role %s" % item) |
| 270 |
else: |
| 271 |
for item in want["roles"]: |
| 272 |
add("role %s" % item) |
| 273 |
|
| 274 |
return True |
| 275 |
return False |
| 276 |
|
| 277 |
if want["state"] == "absent": |
| 278 |
commands.append("no username %s" % want["name"]) |
| 279 |
continue |
| 280 |
|
| 281 |
roles_configured = False |
| 282 |
if want["state"] == "present" and not have: |
| 283 |
roles_configured = configure_roles() |
| 284 |
if not roles_configured: |
| 285 |
commands.append("username %s" % want["name"]) |
| 286 |
|
| 287 |
if needs_update("configured_password"): |
| 288 |
if update_password == "always" or not have: |
| 289 |
add("password %s" % want["configured_password"]) |
| 290 |
|
| 291 |
if needs_update("hashed_password"): |
| 292 |
if update_password == "always" or not have: |
| 293 |
add("password 5 %s" % want["hashed_password"]) |
| 294 |
|
| 295 |
if needs_update("sshkey"): |
| 296 |
add("sshkey %s" % want["sshkey"]) |
| 297 |
|
| 298 |
if not roles_configured: |
| 299 |
configure_roles() |
| 300 |
|
| 301 |
return commands |
| 302 |
|
| 303 |
|
| 304 |
def parse_password(data): |
| 305 |
if not data.get("remote_login"): |
| 306 |
return "<PASSWORD>" |
| 307 |
|
| 308 |
|
| 309 |
def parse_roles(data): |
| 310 |
configured_roles = None |
| 311 |
if "TABLE_role" in data: |
| 312 |
configured_roles = data.get("TABLE_role")["ROW_role"] |
| 313 |
|
| 314 |
roles = list() |
| 315 |
if configured_roles: |
| 316 |
for item in to_list(configured_roles): |
| 317 |
roles.append(item["role"]) |
| 318 |
return roles |
| 319 |
|
| 320 |
|
| 321 |
def map_config_to_obj(module): |
| 322 |
out = run_commands(module, [{"command": "show user-account", "output": "json"}]) |
| 323 |
data = out[0] |
| 324 |
|
| 325 |
objects = list() |
| 326 |
|
| 327 |
for item in to_list(data["TABLE_template"]["ROW_template"]): |
| 328 |
objects.append( |
| 329 |
{ |
| 330 |
"name": item["usr_name"], |
| 331 |
"configured_password": parse_password(item), |
| 332 |
"hashed_password": parse_password(item), |
| 333 |
"sshkey": item.get("sshkey_info"), |
| 334 |
"roles": parse_roles(item), |
| 335 |
"state": "present", |
| 336 |
}, |
| 337 |
) |
| 338 |
return objects |
| 339 |
|
| 340 |
|
| 341 |
def get_param_value(key, item, module): |
| 342 |
# if key doesn't exist in the item, get it from module.params |
| 343 |
if not item.get(key): |
| 344 |
value = module.params[key] |
| 345 |
|
| 346 |
# if key does exist, do a type check on it to validate it |
| 347 |
else: |
| 348 |
value_type = module.argument_spec[key].get("type", "str") |
| 349 |
type_checker = module._CHECK_ARGUMENT_TYPES_DISPATCHER[value_type] |
| 350 |
type_checker(item[key]) |
| 351 |
value = item[key] |
| 352 |
|
| 353 |
return value |
| 354 |
|
| 355 |
|
| 356 |
def map_params_to_obj(module): |
| 357 |
aggregate = module.params["aggregate"] |
| 358 |
if not aggregate: |
| 359 |
if not module.params["name"] and module.params["purge"]: |
| 360 |
return list() |
| 361 |
elif not module.params["name"]: |
| 362 |
module.fail_json(msg="username is required") |
| 363 |
else: |
| 364 |
collection = [{"name": module.params["name"]}] |
| 365 |
else: |
| 366 |
collection = list() |
| 367 |
for item in aggregate: |
| 368 |
if not isinstance(item, dict): |
| 369 |
collection.append({"name": item}) |
| 370 |
elif "name" not in item: |
| 371 |
module.fail_json(msg="name is required") |
| 372 |
else: |
| 373 |
collection.append(item) |
| 374 |
|
| 375 |
objects = list() |
| 376 |
|
| 377 |
for item in collection: |
| 378 |
get_value = partial(get_param_value, item=item, module=module) |
| 379 |
item.update( |
| 380 |
{ |
| 381 |
"configured_password": get_value("configured_password"), |
| 382 |
"hashed_password": get_value("hashed_password"), |
| 383 |
"sshkey": get_value("sshkey"), |
| 384 |
"roles": get_value("roles"), |
| 385 |
"state": get_value("state"), |
| 386 |
}, |
| 387 |
) |
| 388 |
|
| 389 |
for key, value in item.items(): |
| 390 |
if value: |
| 391 |
# validate the param value (if validator func exists) |
| 392 |
validator = globals().get("validate_%s" % key) |
| 393 |
if all((value, validator)): |
| 394 |
validator(value, module) |
| 395 |
|
| 396 |
objects.append(item) |
| 397 |
|
| 398 |
return objects |
| 399 |
|
| 400 |
|
| 401 |
def update_objects(want, have): |
| 402 |
updates = list() |
| 403 |
for entry in want: |
| 404 |
item = next((i for i in have if i["name"] == entry["name"]), None) |
| 405 |
if all((item is None, entry["state"] == "present")): |
| 406 |
updates.append((entry, {})) |
| 407 |
elif item: |
| 408 |
for key, value in entry.items(): |
| 409 |
if value and value != item[key]: |
| 410 |
updates.append((entry, item)) |
| 411 |
return updates |
| 412 |
|
| 413 |
|
| 414 |
def get_configured_usernames(module): |
| 415 |
config_output = run_commands( |
| 416 |
module, |
| 417 |
[{"command": "show running-config | section ^username", "output": "text"}], |
| 418 |
) |
| 419 |
usernames = set() |
| 420 |
for line in config_output[0].splitlines(): |
| 421 |
if line.startswith("username "): |
| 422 |
username = line.split()[1] |
| 423 |
usernames.add(username) |
| 424 |
return usernames |
| 425 |
|
| 426 |
|
| 427 |
def main(): |
| 428 |
"""main entry point for module execution""" |
| 429 |
element_spec = dict( |
| 430 |
name=dict(), |
| 431 |
configured_password=dict(no_log=True), |
| 432 |
hashed_password=dict(no_log=True), |
| 433 |
update_password=dict(default="always", choices=["on_create", "always"]), |
| 434 |
roles=dict(type="list", aliases=["role"], elements="str"), |
| 435 |
sshkey=dict(no_log=False), |
| 436 |
state=dict(default="present", choices=["present", "absent"]), |
| 437 |
) |
| 438 |
|
| 439 |
aggregate_spec = deepcopy(element_spec) |
| 440 |
|
| 441 |
# remove default in aggregate spec, to handle common arguments |
| 442 |
remove_default_spec(aggregate_spec) |
| 443 |
|
| 444 |
argument_spec = dict( |
| 445 |
aggregate=dict( |
| 446 |
type="list", |
| 447 |
elements="dict", |
| 448 |
options=aggregate_spec, |
| 449 |
aliases=["collection", "users"], |
| 450 |
), |
| 451 |
purge=dict(type="bool", default=False), |
| 452 |
) |
| 453 |
|
| 454 |
argument_spec.update(element_spec) |
| 455 |
|
| 456 |
mutually_exclusive = [("name", "aggregate"), ("configured_password", "hashed_password")] |
| 457 |
|
| 458 |
module = AnsibleModule( |
| 459 |
argument_spec=argument_spec, |
| 460 |
mutually_exclusive=mutually_exclusive, |
| 461 |
supports_check_mode=True, |
| 462 |
) |
| 463 |
|
| 464 |
result = {"changed": False, "warnings": []} |
| 465 |
|
| 466 |
want = map_params_to_obj(module) |
| 467 |
have = map_config_to_obj(module) |
| 468 |
|
| 469 |
commands = map_obj_to_commands(update_objects(want, have), module) |
| 470 |
|
| 471 |
if module.params["purge"]: |
| 472 |
want_users = set([x["name"] for x in want]) |
| 473 |
have_users = get_configured_usernames(module) |
| 474 |
|
| 475 |
for item in set(have_users).difference(want_users): |
| 476 |
if item != "admin": |
| 477 |
item = item.replace("\\", "\\\\") |
| 478 |
commands.append("no username %s" % item) |
| 479 |
|
| 480 |
result["commands"] = commands |
| 481 |
|
| 482 |
# the nxos cli prevents this by rule so capture it and display |
| 483 |
# a nice failure message |
| 484 |
if "no username admin" in commands: |
| 485 |
module.fail_json(msg="cannot delete the `admin` account") |
| 486 |
|
| 487 |
# check if provided hashed password is infact a hash |
| 488 |
if module.params["hashed_password"] is not None: |
| 489 |
if not re.match(r"^\$5\$......\$.*$", module.params["hashed_password"]): |
| 490 |
module.fail_json(msg="Provided hash is not valid") |
| 491 |
|
| 492 |
if commands: |
| 493 |
if not module.check_mode: |
| 494 |
responses = load_config(module, commands) |
| 495 |
for resp in responses: |
| 496 |
if resp.lower().startswith("wrong password"): |
| 497 |
module.fail_json(msg=resp) |
| 498 |
else: |
| 499 |
result["warnings"].extend( |
| 500 |
[x[9:] for x in resp.splitlines() if x.startswith("WARNING: ")], |
| 501 |
) |
| 502 |
|
| 503 |
result["changed"] = True |
| 504 |
|
| 505 |
module.exit_json(**result) |
| 506 |
|
| 507 |
|
| 508 |
if __name__ == "__main__": |
| 509 |
main() |