| 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_aaa_server |
| 26 |
extends_documentation_fragment: |
| 27 |
- cisco.nxos.nxos |
| 28 |
short_description: Manages AAA server global configuration. |
| 29 |
description: |
| 30 |
- Manages AAA server global configuration |
| 31 |
version_added: 1.0.0 |
| 32 |
author: |
| 33 |
- Jason Edelman (@jedelman8) |
| 34 |
notes: |
| 35 |
- Tested against NXOSv 7.3.(0)D1(1) on VIRL |
| 36 |
- Limited Support for Cisco MDS |
| 37 |
- The server_type parameter is always required. |
| 38 |
- If encrypt_type is not supplied, the global AAA server key will be stored as encrypted |
| 39 |
(type 7). |
| 40 |
- Changes to the global AAA server key with encrypt_type=0 are not idempotent. |
| 41 |
- state=default will set the supplied parameters to their default values. The parameters |
| 42 |
that you want to default must also be set to default. If global_key=default, the |
| 43 |
global key will be removed. |
| 44 |
options: |
| 45 |
server_type: |
| 46 |
description: |
| 47 |
- The server type is either radius or tacacs. |
| 48 |
required: true |
| 49 |
choices: |
| 50 |
- radius |
| 51 |
- tacacs |
| 52 |
type: str |
| 53 |
global_key: |
| 54 |
description: |
| 55 |
- Global AAA shared secret or keyword 'default'. |
| 56 |
type: str |
| 57 |
encrypt_type: |
| 58 |
description: |
| 59 |
- The state of encryption applied to the entered global key. O clear text, 7 encrypted. |
| 60 |
Type-6 encryption is not supported. |
| 61 |
choices: |
| 62 |
- '0' |
| 63 |
- '7' |
| 64 |
type: str |
| 65 |
deadtime: |
| 66 |
description: |
| 67 |
- Duration for which a non-reachable AAA server is skipped, in minutes or keyword |
| 68 |
'default. Range is 1-1440. Device default is 0. |
| 69 |
type: str |
| 70 |
server_timeout: |
| 71 |
description: |
| 72 |
- Global AAA server timeout period, in seconds or keyword 'default. Range is 1-60. |
| 73 |
Device default is 5. |
| 74 |
type: str |
| 75 |
directed_request: |
| 76 |
description: |
| 77 |
- Enables direct authentication requests to AAA server or keyword 'default' Device |
| 78 |
default is disabled. |
| 79 |
choices: |
| 80 |
- enabled |
| 81 |
- disabled |
| 82 |
- default |
| 83 |
type: str |
| 84 |
state: |
| 85 |
description: |
| 86 |
- Manage the state of the resource. |
| 87 |
default: present |
| 88 |
choices: |
| 89 |
- present |
| 90 |
- default |
| 91 |
type: str |
| 92 |
""" |
| 93 |
|
| 94 |
EXAMPLES = """ |
| 95 |
# Radius Server Basic settings |
| 96 |
- name: Radius Server Basic settings |
| 97 |
cisco.nxos.nxos_aaa_server: |
| 98 |
server_type: radius |
| 99 |
server_timeout: 9 |
| 100 |
deadtime: 20 |
| 101 |
directed_request: enabled |
| 102 |
|
| 103 |
# Tacacs Server Basic settings |
| 104 |
- name: Tacacs Server Basic settings |
| 105 |
cisco.nxos.nxos_aaa_server: |
| 106 |
server_type: tacacs |
| 107 |
server_timeout: 8 |
| 108 |
deadtime: 19 |
| 109 |
directed_request: disabled |
| 110 |
|
| 111 |
# Setting Global Key |
| 112 |
- name: AAA Server Global Key |
| 113 |
cisco.nxos.nxos_aaa_server: |
| 114 |
server_type: radius |
| 115 |
global_key: test_key |
| 116 |
""" |
| 117 |
|
| 118 |
RETURN = """ |
| 119 |
commands: |
| 120 |
description: command sent to the device |
| 121 |
returned: always |
| 122 |
type: list |
| 123 |
sample: ["radius-server deadtime 22", "radius-server timeout 11", |
| 124 |
"radius-server directed-request"] |
| 125 |
""" |
| 126 |
import re |
| 127 |
|
| 128 |
from ansible.module_utils.basic import AnsibleModule |
| 129 |
|
| 130 |
from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxos import ( |
| 131 |
load_config, |
| 132 |
run_commands, |
| 133 |
) |
| 134 |
|
| 135 |
|
| 136 |
PARAM_TO_DEFAULT_KEYMAP = { |
| 137 |
"server_timeout": "5", |
| 138 |
"deadtime": "0", |
| 139 |
"directed_request": "disabled", |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
def execute_show_command(command, module): |
| 144 |
command = {"command": command, "output": "text"} |
| 145 |
|
| 146 |
return run_commands(module, command) |
| 147 |
|
| 148 |
|
| 149 |
def flatten_list(command_lists): |
| 150 |
flat_command_list = [] |
| 151 |
for command in command_lists: |
| 152 |
if isinstance(command, list): |
| 153 |
flat_command_list.extend(command) |
| 154 |
else: |
| 155 |
flat_command_list.append(command) |
| 156 |
return flat_command_list |
| 157 |
|
| 158 |
|
| 159 |
def get_aaa_server_info(server_type, module): |
| 160 |
aaa_server_info = {} |
| 161 |
server_command = "show {0}-server".format(server_type) |
| 162 |
request_command = "show {0}-server directed-request".format(server_type) |
| 163 |
global_key_command = "show run | sec {0}".format(server_type) |
| 164 |
aaa_regex = r".*{0}-server\skey\s\d\s+(?P<key>\S+).*".format(server_type) |
| 165 |
|
| 166 |
server_body = execute_show_command(server_command, module)[0] |
| 167 |
|
| 168 |
split_server = server_body.splitlines() |
| 169 |
|
| 170 |
for line in split_server: |
| 171 |
if line.startswith("timeout"): |
| 172 |
aaa_server_info["server_timeout"] = line.split(":")[1] |
| 173 |
|
| 174 |
elif line.startswith("deadtime"): |
| 175 |
aaa_server_info["deadtime"] = line.split(":")[1] |
| 176 |
|
| 177 |
request_body = execute_show_command(request_command, module)[0] |
| 178 |
|
| 179 |
if bool(request_body): |
| 180 |
aaa_server_info["directed_request"] = request_body.replace("\n", "") |
| 181 |
else: |
| 182 |
aaa_server_info["directed_request"] = "disabled" |
| 183 |
|
| 184 |
key_body = execute_show_command(global_key_command, module)[0] |
| 185 |
|
| 186 |
try: |
| 187 |
match_global_key = re.match(aaa_regex, key_body, re.DOTALL) |
| 188 |
group_key = match_global_key.groupdict() |
| 189 |
aaa_server_info["global_key"] = group_key["key"].replace('"', "") |
| 190 |
except (AttributeError, TypeError): |
| 191 |
aaa_server_info["global_key"] = None |
| 192 |
|
| 193 |
return aaa_server_info |
| 194 |
|
| 195 |
|
| 196 |
def config_aaa_server(params, server_type): |
| 197 |
cmds = [] |
| 198 |
|
| 199 |
deadtime = params.get("deadtime") |
| 200 |
server_timeout = params.get("server_timeout") |
| 201 |
directed_request = params.get("directed_request") |
| 202 |
encrypt_type = params.get("encrypt_type", "7") |
| 203 |
global_key = params.get("global_key") |
| 204 |
|
| 205 |
if deadtime is not None: |
| 206 |
cmds.append("{0}-server deadtime {1}".format(server_type, deadtime)) |
| 207 |
|
| 208 |
if server_timeout is not None: |
| 209 |
cmds.append("{0}-server timeout {1}".format(server_type, server_timeout)) |
| 210 |
|
| 211 |
if directed_request is not None: |
| 212 |
if directed_request == "enabled": |
| 213 |
cmds.append("{0}-server directed-request".format(server_type)) |
| 214 |
elif directed_request == "disabled": |
| 215 |
cmds.append("no {0}-server directed-request".format(server_type)) |
| 216 |
|
| 217 |
if global_key is not None: |
| 218 |
cmds.append("{0}-server key {1} {2}".format(server_type, encrypt_type, global_key)) |
| 219 |
|
| 220 |
return cmds |
| 221 |
|
| 222 |
|
| 223 |
def default_aaa_server(existing, params, server_type): |
| 224 |
cmds = [] |
| 225 |
|
| 226 |
deadtime = params.get("deadtime") |
| 227 |
server_timeout = params.get("server_timeout") |
| 228 |
directed_request = params.get("directed_request") |
| 229 |
global_key = params.get("global_key") |
| 230 |
existing_key = existing.get("global_key") |
| 231 |
|
| 232 |
if deadtime is not None and existing.get("deadtime") != PARAM_TO_DEFAULT_KEYMAP["deadtime"]: |
| 233 |
cmds.append("no {0}-server deadtime 1".format(server_type)) |
| 234 |
|
| 235 |
if ( |
| 236 |
server_timeout is not None |
| 237 |
and existing.get("server_timeout") != PARAM_TO_DEFAULT_KEYMAP["server_timeout"] |
| 238 |
): |
| 239 |
cmds.append("no {0}-server timeout 1".format(server_type)) |
| 240 |
|
| 241 |
if ( |
| 242 |
directed_request is not None |
| 243 |
and existing.get("directed_request") != PARAM_TO_DEFAULT_KEYMAP["directed_request"] |
| 244 |
): |
| 245 |
cmds.append("no {0}-server directed-request".format(server_type)) |
| 246 |
|
| 247 |
if global_key is not None and existing_key is not None: |
| 248 |
cmds.append("no {0}-server key 7 {1}".format(server_type, existing_key)) |
| 249 |
|
| 250 |
return cmds |
| 251 |
|
| 252 |
|
| 253 |
def main(): |
| 254 |
argument_spec = dict( |
| 255 |
server_type=dict(type="str", choices=["radius", "tacacs"], required=True), |
| 256 |
global_key=dict(type="str", no_log=True), |
| 257 |
encrypt_type=dict(type="str", choices=["0", "7"]), |
| 258 |
deadtime=dict(type="str"), |
| 259 |
server_timeout=dict(type="str"), |
| 260 |
directed_request=dict(type="str", choices=["enabled", "disabled", "default"]), |
| 261 |
state=dict(choices=["default", "present"], default="present"), |
| 262 |
) |
| 263 |
|
| 264 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 265 |
|
| 266 |
warnings = list() |
| 267 |
results = {"changed": False, "commands": [], "warnings": warnings} |
| 268 |
|
| 269 |
server_type = module.params["server_type"] |
| 270 |
global_key = module.params["global_key"] |
| 271 |
encrypt_type = module.params["encrypt_type"] |
| 272 |
deadtime = module.params["deadtime"] |
| 273 |
server_timeout = module.params["server_timeout"] |
| 274 |
directed_request = module.params["directed_request"] |
| 275 |
state = module.params["state"] |
| 276 |
|
| 277 |
if encrypt_type and not global_key: |
| 278 |
module.fail_json(msg="encrypt_type must be used with global_key.") |
| 279 |
|
| 280 |
args = dict( |
| 281 |
server_type=server_type, |
| 282 |
global_key=global_key, |
| 283 |
encrypt_type=encrypt_type, |
| 284 |
deadtime=deadtime, |
| 285 |
server_timeout=server_timeout, |
| 286 |
directed_request=directed_request, |
| 287 |
) |
| 288 |
|
| 289 |
proposed = dict((k, v) for k, v in args.items() if v is not None) |
| 290 |
|
| 291 |
existing = get_aaa_server_info(server_type, module) |
| 292 |
|
| 293 |
commands = [] |
| 294 |
if state == "present": |
| 295 |
if deadtime: |
| 296 |
try: |
| 297 |
if int(deadtime) < 0 or int(deadtime) > 1440: |
| 298 |
raise ValueError |
| 299 |
except ValueError: |
| 300 |
module.fail_json(msg="deadtime must be an integer between 0 and 1440") |
| 301 |
|
| 302 |
if server_timeout: |
| 303 |
try: |
| 304 |
if int(server_timeout) < 1 or int(server_timeout) > 60: |
| 305 |
raise ValueError |
| 306 |
except ValueError: |
| 307 |
module.fail_json(msg="server_timeout must be an integer between 1 and 60") |
| 308 |
|
| 309 |
delta = dict(set(proposed.items()).difference(existing.items())) |
| 310 |
if delta: |
| 311 |
command = config_aaa_server(delta, server_type) |
| 312 |
if command: |
| 313 |
commands.append(command) |
| 314 |
|
| 315 |
elif state == "default": |
| 316 |
for key, value in proposed.items(): |
| 317 |
if key != "server_type" and value != "default": |
| 318 |
module.fail_json(msg='Parameters must be set to "default"' "when state=default") |
| 319 |
command = default_aaa_server(existing, proposed, server_type) |
| 320 |
if command: |
| 321 |
commands.append(command) |
| 322 |
|
| 323 |
cmds = flatten_list(commands) |
| 324 |
if cmds: |
| 325 |
results["changed"] = True |
| 326 |
if not module.check_mode: |
| 327 |
load_config(module, cmds) |
| 328 |
if "configure" in cmds: |
| 329 |
cmds.pop(0) |
| 330 |
results["commands"] = cmds |
| 331 |
|
| 332 |
module.exit_json(**results) |
| 333 |
|
| 334 |
|
| 335 |
if __name__ == "__main__": |
| 336 |
main() |