| 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 |
# pylint: skip-file |
| 18 |
from __future__ import absolute_import, division, print_function |
| 19 |
|
| 20 |
|
| 21 |
__metaclass__ = type |
| 22 |
|
| 23 |
|
| 24 |
DOCUMENTATION = """ |
| 25 |
module: nxos_nxapi |
| 26 |
extends_documentation_fragment: |
| 27 |
- cisco.nxos.nxos |
| 28 |
author: Peter Sprygada (@privateip) |
| 29 |
short_description: Manage NXAPI configuration on an NXOS device. |
| 30 |
notes: |
| 31 |
- Limited Support for Cisco MDS |
| 32 |
description: |
| 33 |
- Configures the NXAPI feature on devices running Cisco NXOS. The NXAPI feature is |
| 34 |
absent from the configuration by default. Since this module manages the NXAPI feature |
| 35 |
it only supports the use of the C(Cli) transport. |
| 36 |
version_added: 1.0.0 |
| 37 |
options: |
| 38 |
http_port: |
| 39 |
description: |
| 40 |
- Configure the port with which the HTTP server will listen on for requests. By |
| 41 |
default, NXAPI will bind the HTTP service to the standard HTTP port 80. This |
| 42 |
argument accepts valid port values in the range of 1 to 65535. |
| 43 |
required: false |
| 44 |
default: 80 |
| 45 |
type: int |
| 46 |
http: |
| 47 |
description: |
| 48 |
- Controls the operating state of the HTTP protocol as one of the underlying transports |
| 49 |
for NXAPI. By default, NXAPI will enable the HTTP transport when the feature |
| 50 |
is first configured. To disable the use of the HTTP transport, set the value |
| 51 |
of this argument to False. |
| 52 |
required: false |
| 53 |
default: true |
| 54 |
type: bool |
| 55 |
aliases: |
| 56 |
- enable_http |
| 57 |
https_port: |
| 58 |
description: |
| 59 |
- Configure the port with which the HTTPS server will listen on for requests. By |
| 60 |
default, NXAPI will bind the HTTPS service to the standard HTTPS port 443. This |
| 61 |
argument accepts valid port values in the range of 1 to 65535. |
| 62 |
required: false |
| 63 |
default: 443 |
| 64 |
type: int |
| 65 |
https: |
| 66 |
description: |
| 67 |
- Controls the operating state of the HTTPS protocol as one of the underlying |
| 68 |
transports for NXAPI. By default, NXAPI will disable the HTTPS transport when |
| 69 |
the feature is first configured. To enable the use of the HTTPS transport, |
| 70 |
set the value of this argument to True. |
| 71 |
required: false |
| 72 |
default: false |
| 73 |
type: bool |
| 74 |
aliases: |
| 75 |
- enable_https |
| 76 |
sandbox: |
| 77 |
description: |
| 78 |
- The NXAPI feature provides a web base UI for developers for entering commands. This |
| 79 |
feature is initially disabled when the NXAPI feature is configured for the first |
| 80 |
time. When the C(sandbox) argument is set to True, the developer sandbox URL |
| 81 |
will accept requests and when the value is set to False, the sandbox URL is |
| 82 |
unavailable. This is supported on NX-OS 7K series. |
| 83 |
required: false |
| 84 |
type: bool |
| 85 |
aliases: |
| 86 |
- enable_sandbox |
| 87 |
state: |
| 88 |
description: |
| 89 |
- The C(state) argument controls whether or not the NXAPI feature is configured |
| 90 |
on the remote device. When the value is C(present) the NXAPI feature configuration |
| 91 |
is present in the device running-config. When the values is C(absent) the feature |
| 92 |
configuration is removed from the running-config. |
| 93 |
choices: |
| 94 |
- present |
| 95 |
- absent |
| 96 |
required: false |
| 97 |
default: present |
| 98 |
type: str |
| 99 |
ssl_strong_ciphers: |
| 100 |
description: |
| 101 |
- Controls the use of whether strong or weak ciphers are configured. By default, |
| 102 |
this feature is disabled and weak ciphers are configured. To enable the use |
| 103 |
of strong ciphers, set the value of this argument to True. |
| 104 |
required: false |
| 105 |
default: false |
| 106 |
type: bool |
| 107 |
tlsv1_0: |
| 108 |
description: |
| 109 |
- Controls the use of the Transport Layer Security version 1.0 is configured. By |
| 110 |
default, this feature is enabled. To disable the use of TLSV1.0, set the value |
| 111 |
of this argument to True. |
| 112 |
required: false |
| 113 |
default: true |
| 114 |
type: bool |
| 115 |
tlsv1_1: |
| 116 |
description: |
| 117 |
- Controls the use of the Transport Layer Security version 1.1 is configured. By |
| 118 |
default, this feature is disabled. To enable the use of TLSV1.1, set the value |
| 119 |
of this argument to True. |
| 120 |
required: false |
| 121 |
default: false |
| 122 |
type: bool |
| 123 |
tlsv1_2: |
| 124 |
description: |
| 125 |
- Controls the use of the Transport Layer Security version 1.2 is configured. By |
| 126 |
default, this feature is disabled. To enable the use of TLSV1.2, set the value |
| 127 |
of this argument to True. |
| 128 |
required: false |
| 129 |
default: false |
| 130 |
type: bool |
| 131 |
""" |
| 132 |
|
| 133 |
EXAMPLES = """ |
| 134 |
- name: Enable NXAPI access with default configuration |
| 135 |
cisco.nxos.nxos_nxapi: |
| 136 |
state: present |
| 137 |
|
| 138 |
- name: Enable NXAPI with no HTTP, HTTPS at port 9443 and sandbox disabled |
| 139 |
cisco.nxos.nxos_nxapi: |
| 140 |
enable_http: false |
| 141 |
https_port: 9443 |
| 142 |
https: true |
| 143 |
enable_sandbox: false |
| 144 |
|
| 145 |
- name: remove NXAPI configuration |
| 146 |
cisco.nxos.nxos_nxapi: |
| 147 |
state: absent |
| 148 |
""" |
| 149 |
|
| 150 |
RETURN = """ |
| 151 |
updates: |
| 152 |
description: |
| 153 |
- Returns the list of commands that need to be pushed into the remote |
| 154 |
device to satisfy the arguments |
| 155 |
returned: always |
| 156 |
type: list |
| 157 |
sample: ['no feature nxapi'] |
| 158 |
""" |
| 159 |
import re |
| 160 |
|
| 161 |
from ansible.module_utils.basic import AnsibleModule |
| 162 |
|
| 163 |
from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxos import ( |
| 164 |
get_capabilities, |
| 165 |
load_config, |
| 166 |
run_commands, |
| 167 |
) |
| 168 |
from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.utils.utils import Version |
| 169 |
|
| 170 |
|
| 171 |
def check_args(module, warnings, capabilities): |
| 172 |
network_api = capabilities.get("network_api", "nxapi") |
| 173 |
if network_api == "nxapi": |
| 174 |
module.fail_json(msg="module not supported over nxapi transport") |
| 175 |
|
| 176 |
os_platform = capabilities["device_info"]["network_os_platform"] |
| 177 |
if "7K" not in os_platform and module.params["sandbox"]: |
| 178 |
module.fail_json( |
| 179 |
msg="sandbox or enable_sandbox is supported on NX-OS 7K series of switches", |
| 180 |
) |
| 181 |
|
| 182 |
state = module.params["state"] |
| 183 |
|
| 184 |
if state == "started": |
| 185 |
module.params["state"] = "present" |
| 186 |
warnings.append( |
| 187 |
"state=started is deprecated and will be removed in a " |
| 188 |
"a future release. Please use state=present instead", |
| 189 |
) |
| 190 |
elif state == "stopped": |
| 191 |
module.params["state"] = "absent" |
| 192 |
warnings.append( |
| 193 |
"state=stopped is deprecated and will be removed in a " |
| 194 |
"a future release. Please use state=absent instead", |
| 195 |
) |
| 196 |
|
| 197 |
for key in ["http_port", "https_port"]: |
| 198 |
if module.params[key] is not None: |
| 199 |
if not 1 <= module.params[key] <= 65535: |
| 200 |
module.fail_json(msg="%s must be between 1 and 65535" % key) |
| 201 |
|
| 202 |
return warnings |
| 203 |
|
| 204 |
|
| 205 |
def map_obj_to_commands(want, have, module, warnings, capabilities): |
| 206 |
send_commands = list() |
| 207 |
commands = dict() |
| 208 |
os_platform = None |
| 209 |
os_version = None |
| 210 |
|
| 211 |
device_info = capabilities.get("device_info") |
| 212 |
if device_info: |
| 213 |
os_version = device_info.get("network_os_version") |
| 214 |
if os_version: |
| 215 |
os_version = os_version[:3] |
| 216 |
os_platform = device_info.get("network_os_platform") |
| 217 |
if os_platform: |
| 218 |
os_platform = os_platform[:3] |
| 219 |
|
| 220 |
def needs_update(x): |
| 221 |
return want.get(x) is not None and (want.get(x) != have.get(x)) |
| 222 |
|
| 223 |
if needs_update("state"): |
| 224 |
if want["state"] == "absent": |
| 225 |
return ["no feature nxapi"] |
| 226 |
send_commands.append("feature nxapi") |
| 227 |
elif want["state"] == "absent": |
| 228 |
return send_commands |
| 229 |
|
| 230 |
for parameter in ["http", "https"]: |
| 231 |
port_param = parameter + "_port" |
| 232 |
if needs_update(parameter): |
| 233 |
if want.get(parameter) is False: |
| 234 |
commands[parameter] = "no nxapi %s" % parameter |
| 235 |
else: |
| 236 |
commands[parameter] = "nxapi %s port %s" % ( |
| 237 |
parameter, |
| 238 |
want.get(port_param), |
| 239 |
) |
| 240 |
|
| 241 |
if needs_update(port_param) and want.get(parameter) is True: |
| 242 |
commands[parameter] = "nxapi %s port %s" % ( |
| 243 |
parameter, |
| 244 |
want.get(port_param), |
| 245 |
) |
| 246 |
|
| 247 |
if needs_update("sandbox"): |
| 248 |
commands["sandbox"] = "nxapi sandbox" |
| 249 |
if not want["sandbox"]: |
| 250 |
commands["sandbox"] = "no %s" % commands["sandbox"] |
| 251 |
|
| 252 |
if os_platform and os_version: |
| 253 |
if (os_platform == "N9K" or os_platform == "N3K") and Version(os_version) >= "9.2": |
| 254 |
if needs_update("ssl_strong_ciphers"): |
| 255 |
commands["ssl_strong_ciphers"] = "nxapi ssl ciphers weak" |
| 256 |
if want["ssl_strong_ciphers"] is True: |
| 257 |
commands["ssl_strong_ciphers"] = "no nxapi ssl ciphers weak" |
| 258 |
|
| 259 |
have_ssl_protocols = "" |
| 260 |
want_ssl_protocols = "" |
| 261 |
for key, value in { |
| 262 |
"tlsv1_2": "TLSv1.2", |
| 263 |
"tlsv1_1": "TLSv1.1", |
| 264 |
"tlsv1_0": "TLSv1", |
| 265 |
}.items(): |
| 266 |
if needs_update(key): |
| 267 |
if want.get(key) is True: |
| 268 |
want_ssl_protocols = " ".join([want_ssl_protocols, value]) |
| 269 |
elif have.get(key) is True: |
| 270 |
have_ssl_protocols = " ".join([have_ssl_protocols, value]) |
| 271 |
|
| 272 |
if len(want_ssl_protocols) > 0: |
| 273 |
commands["ssl_protocols"] = "nxapi ssl protocols%s" % ( |
| 274 |
" ".join([want_ssl_protocols, have_ssl_protocols]) |
| 275 |
) |
| 276 |
else: |
| 277 |
warnings.append( |
| 278 |
"os_version and/or os_platform keys from " |
| 279 |
"platform capabilities are not available. " |
| 280 |
"Any NXAPI SSL optional arguments will be ignored", |
| 281 |
) |
| 282 |
|
| 283 |
send_commands.extend(commands.values()) |
| 284 |
|
| 285 |
return send_commands |
| 286 |
|
| 287 |
|
| 288 |
def parse_http(data): |
| 289 |
http_res = [r"nxapi http port (\d+)"] |
| 290 |
http_port = None |
| 291 |
|
| 292 |
for regex in http_res: |
| 293 |
match = re.search(regex, data, re.M) |
| 294 |
if match: |
| 295 |
http_port = int(match.group(1)) |
| 296 |
break |
| 297 |
|
| 298 |
return {"http": http_port is not None, "http_port": http_port} |
| 299 |
|
| 300 |
|
| 301 |
def parse_https(data): |
| 302 |
https_res = [r"nxapi https port (\d+)"] |
| 303 |
https_port = None |
| 304 |
|
| 305 |
for regex in https_res: |
| 306 |
match = re.search(regex, data, re.M) |
| 307 |
if match: |
| 308 |
https_port = int(match.group(1)) |
| 309 |
break |
| 310 |
|
| 311 |
return {"https": https_port is not None, "https_port": https_port} |
| 312 |
|
| 313 |
|
| 314 |
def parse_sandbox(data): |
| 315 |
sandbox = [item for item in data.split("\n") if re.search(r".*sandbox.*", item)] |
| 316 |
value = False |
| 317 |
if sandbox and sandbox[0] == "nxapi sandbox": |
| 318 |
value = True |
| 319 |
return {"sandbox": value} |
| 320 |
|
| 321 |
|
| 322 |
def parse_ssl_strong_ciphers(data): |
| 323 |
ciphers_res = [r"(\w+) nxapi ssl ciphers weak"] |
| 324 |
value = None |
| 325 |
|
| 326 |
for regex in ciphers_res: |
| 327 |
match = re.search(regex, data, re.M) |
| 328 |
if match: |
| 329 |
value = match.group(1) |
| 330 |
break |
| 331 |
|
| 332 |
return {"ssl_strong_ciphers": value == "no"} |
| 333 |
|
| 334 |
|
| 335 |
def parse_ssl_protocols(data): |
| 336 |
tlsv1_0 = re.search(r"(?<!\S)TLSv1(?!\S)", data, re.M) is not None |
| 337 |
tlsv1_1 = re.search(r"(?<!\S)TLSv1.1(?!\S)", data, re.M) is not None |
| 338 |
tlsv1_2 = re.search(r"(?<!\S)TLSv1.2(?!\S)", data, re.M) is not None |
| 339 |
|
| 340 |
return {"tlsv1_0": tlsv1_0, "tlsv1_1": tlsv1_1, "tlsv1_2": tlsv1_2} |
| 341 |
|
| 342 |
|
| 343 |
def map_config_to_obj(module): |
| 344 |
out = run_commands(module, ["show run all | inc nxapi"], check_rc=False)[0] |
| 345 |
match = re.search(r"no feature nxapi", out, re.M) |
| 346 |
# There are two possible outcomes when nxapi is disabled on nxos platforms. |
| 347 |
# 1. Nothing is displayed in the running config. |
| 348 |
# 2. The 'no feature nxapi' command is displayed in the running config. |
| 349 |
if match or out == "": |
| 350 |
return {"state": "absent"} |
| 351 |
|
| 352 |
out = str(out).strip() |
| 353 |
|
| 354 |
obj = {"state": "present"} |
| 355 |
obj.update(parse_http(out)) |
| 356 |
obj.update(parse_https(out)) |
| 357 |
obj.update(parse_sandbox(out)) |
| 358 |
obj.update(parse_ssl_strong_ciphers(out)) |
| 359 |
obj.update(parse_ssl_protocols(out)) |
| 360 |
|
| 361 |
return obj |
| 362 |
|
| 363 |
|
| 364 |
def map_params_to_obj(module): |
| 365 |
obj = { |
| 366 |
"http": module.params["http"], |
| 367 |
"http_port": module.params["http_port"], |
| 368 |
"https": module.params["https"], |
| 369 |
"https_port": module.params["https_port"], |
| 370 |
"sandbox": module.params["sandbox"], |
| 371 |
"state": module.params["state"], |
| 372 |
"ssl_strong_ciphers": module.params["ssl_strong_ciphers"], |
| 373 |
"tlsv1_0": module.params["tlsv1_0"], |
| 374 |
"tlsv1_1": module.params["tlsv1_1"], |
| 375 |
"tlsv1_2": module.params["tlsv1_2"], |
| 376 |
} |
| 377 |
|
| 378 |
return obj |
| 379 |
|
| 380 |
|
| 381 |
def main(): |
| 382 |
"""main entry point for module execution""" |
| 383 |
argument_spec = dict( |
| 384 |
http=dict(aliases=["enable_http"], type="bool", default=True), |
| 385 |
http_port=dict(type="int", default=80), |
| 386 |
https=dict(aliases=["enable_https"], type="bool", default=False), |
| 387 |
https_port=dict(type="int", default=443), |
| 388 |
sandbox=dict(aliases=["enable_sandbox"], type="bool"), |
| 389 |
state=dict(default="present", choices=["present", "absent"]), |
| 390 |
ssl_strong_ciphers=dict(type="bool", default=False), |
| 391 |
tlsv1_0=dict(type="bool", default=True), |
| 392 |
tlsv1_1=dict(type="bool", default=False), |
| 393 |
tlsv1_2=dict(type="bool", default=False), |
| 394 |
) |
| 395 |
|
| 396 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 397 |
|
| 398 |
warnings = list() |
| 399 |
warning_msg = "Module nxos_nxapi currently defaults to configure 'http port 80'. " |
| 400 |
warning_msg += "Default behavior is changing to configure 'https port 443'" |
| 401 |
warning_msg += " when params 'http, http_port, https, https_port' are not set in the playbook" |
| 402 |
module.deprecate(msg=warning_msg, date="2022-06-01", collection_name="cisco.nxos") |
| 403 |
|
| 404 |
capabilities = get_capabilities(module) |
| 405 |
|
| 406 |
check_args(module, warnings, capabilities) |
| 407 |
|
| 408 |
want = map_params_to_obj(module) |
| 409 |
have = map_config_to_obj(module) |
| 410 |
|
| 411 |
commands = map_obj_to_commands(want, have, module, warnings, capabilities) |
| 412 |
|
| 413 |
result = {"changed": False, "warnings": warnings, "commands": commands} |
| 414 |
|
| 415 |
if commands: |
| 416 |
if not module.check_mode: |
| 417 |
load_config(module, commands) |
| 418 |
result["changed"] = True |
| 419 |
|
| 420 |
module.exit_json(**result) |
| 421 |
|
| 422 |
|
| 423 |
if __name__ == "__main__": |
| 424 |
main() |