| 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_system |
| 26 |
extends_documentation_fragment: |
| 27 |
- cisco.nxos.nxos |
| 28 |
author: Peter Sprygada (@privateip) |
| 29 |
short_description: Manage the system attributes on Cisco NXOS devices |
| 30 |
notes: |
| 31 |
- Unsupported for Cisco MDS |
| 32 |
description: |
| 33 |
- This module provides declarative management of node system attributes on Cisco NXOS |
| 34 |
devices. It provides an option to configure host system parameters or remove those |
| 35 |
parameters from the device active configuration. |
| 36 |
version_added: 1.0.0 |
| 37 |
options: |
| 38 |
hostname: |
| 39 |
description: |
| 40 |
- Configure the device hostname parameter. This option takes an ASCII string value |
| 41 |
or keyword 'default' |
| 42 |
type: str |
| 43 |
domain_name: |
| 44 |
description: |
| 45 |
- Configures the default domain name suffix to be used when referencing this node |
| 46 |
by its FQDN. This argument accepts either a list of domain names or a list |
| 47 |
of dicts that configure the domain name and VRF name or keyword 'default'. See |
| 48 |
examples. |
| 49 |
type: list |
| 50 |
elements: raw |
| 51 |
domain_lookup: |
| 52 |
description: |
| 53 |
- Enables or disables the DNS lookup feature in Cisco NXOS. This argument accepts |
| 54 |
boolean values. When enabled, the system will try to resolve hostnames using |
| 55 |
DNS and when disabled, hostnames will not be resolved. |
| 56 |
type: bool |
| 57 |
domain_search: |
| 58 |
description: |
| 59 |
- Configures a list of domain name suffixes to search when performing DNS name |
| 60 |
resolution. This argument accepts either a list of domain names or a list of |
| 61 |
dicts that configure the domain name and VRF name or keyword 'default'. See |
| 62 |
examples. |
| 63 |
type: list |
| 64 |
elements: raw |
| 65 |
name_servers: |
| 66 |
description: |
| 67 |
- List of DNS name servers by IP address to use to perform name resolution lookups. This |
| 68 |
argument accepts either a list of DNS servers or a list of hashes that configure |
| 69 |
the name server and VRF name or keyword 'default'. See examples. |
| 70 |
type: list |
| 71 |
elements: raw |
| 72 |
system_mtu: |
| 73 |
description: |
| 74 |
- Specifies the mtu, must be an integer or keyword 'default'. |
| 75 |
type: str |
| 76 |
state: |
| 77 |
description: |
| 78 |
- State of the configuration values in the device's current active configuration. When |
| 79 |
set to I(present), the values should be configured in the device active configuration |
| 80 |
and when set to I(absent) the values should not be in the device active configuration |
| 81 |
default: present |
| 82 |
choices: |
| 83 |
- present |
| 84 |
- absent |
| 85 |
type: str |
| 86 |
""" |
| 87 |
|
| 88 |
EXAMPLES = """ |
| 89 |
- name: configure hostname and domain-name |
| 90 |
cisco.nxos.nxos_system: |
| 91 |
hostname: nxos01 |
| 92 |
domain_name: test.example.com |
| 93 |
|
| 94 |
- name: remove configuration |
| 95 |
cisco.nxos.nxos_system: |
| 96 |
state: absent |
| 97 |
|
| 98 |
- name: configure name servers |
| 99 |
cisco.nxos.nxos_system: |
| 100 |
name_servers: |
| 101 |
- 8.8.8.8 |
| 102 |
- 8.8.4.4 |
| 103 |
|
| 104 |
- name: configure name servers with VRF support |
| 105 |
cisco.nxos.nxos_system: |
| 106 |
name_servers: |
| 107 |
- {server: 8.8.8.8, vrf: mgmt} |
| 108 |
- {server: 8.8.4.4, vrf: mgmt} |
| 109 |
""" |
| 110 |
|
| 111 |
RETURN = """ |
| 112 |
commands: |
| 113 |
description: The list of configuration mode commands to send to the device |
| 114 |
returned: always |
| 115 |
type: list |
| 116 |
sample: |
| 117 |
- hostname nxos01 |
| 118 |
- ip domain-name test.example.com |
| 119 |
""" |
| 120 |
import re |
| 121 |
|
| 122 |
from ansible.module_utils.basic import AnsibleModule |
| 123 |
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.config import ( |
| 124 |
NetworkConfig, |
| 125 |
) |
| 126 |
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( |
| 127 |
ComplexList, |
| 128 |
) |
| 129 |
|
| 130 |
from ansible_collections.cisco.nxos.plugins.module_utils.network.nxos.nxos import ( |
| 131 |
get_config, |
| 132 |
load_config, |
| 133 |
) |
| 134 |
|
| 135 |
|
| 136 |
_CONFIGURED_VRFS = None |
| 137 |
|
| 138 |
|
| 139 |
def has_vrf(module, vrf): |
| 140 |
global _CONFIGURED_VRFS |
| 141 |
if _CONFIGURED_VRFS is not None: |
| 142 |
return vrf in _CONFIGURED_VRFS |
| 143 |
config = get_config(module) |
| 144 |
_CONFIGURED_VRFS = re.findall(r"vrf context (\S+)", config) |
| 145 |
return vrf in _CONFIGURED_VRFS |
| 146 |
|
| 147 |
|
| 148 |
def map_obj_to_commands(want, have, module): |
| 149 |
commands = list() |
| 150 |
state = module.params["state"] |
| 151 |
|
| 152 |
def needs_update(x): |
| 153 |
return want.get(x) and (want.get(x) != have.get(x)) |
| 154 |
|
| 155 |
def difference(x, y, z): |
| 156 |
return [item for item in x[z] if item not in y[z]] |
| 157 |
|
| 158 |
def remove(cmd, commands, vrf=None): |
| 159 |
if vrf: |
| 160 |
commands.append("vrf context %s" % vrf) |
| 161 |
commands.append(cmd) |
| 162 |
if vrf: |
| 163 |
commands.append("exit") |
| 164 |
|
| 165 |
def add(cmd, commands, vrf=None): |
| 166 |
if vrf: |
| 167 |
if not has_vrf(module, vrf): |
| 168 |
module.fail_json(msg="invalid vrf name %s" % vrf) |
| 169 |
return remove(cmd, commands, vrf) |
| 170 |
|
| 171 |
if state == "absent": |
| 172 |
if have["hostname"]: |
| 173 |
commands.append("no hostname") |
| 174 |
|
| 175 |
for item in have["domain_name"]: |
| 176 |
cmd = "no ip domain-name %s" % item["name"] |
| 177 |
remove(cmd, commands, item["vrf"]) |
| 178 |
|
| 179 |
for item in have["domain_search"]: |
| 180 |
cmd = "no ip domain-list %s" % item["name"] |
| 181 |
remove(cmd, commands, item["vrf"]) |
| 182 |
|
| 183 |
for item in have["name_servers"]: |
| 184 |
cmd = "no ip name-server %s" % item["server"] |
| 185 |
remove(cmd, commands, item["vrf"]) |
| 186 |
|
| 187 |
if have["system_mtu"]: |
| 188 |
commands.append("no system jumbomtu") |
| 189 |
|
| 190 |
if state == "present": |
| 191 |
if needs_update("hostname"): |
| 192 |
if want["hostname"] == "default": |
| 193 |
if have["hostname"]: |
| 194 |
commands.append("no hostname") |
| 195 |
else: |
| 196 |
commands.append("hostname %s" % want["hostname"]) |
| 197 |
|
| 198 |
if want.get("domain_lookup") is not None: |
| 199 |
if have.get("domain_lookup") != want.get("domain_lookup"): |
| 200 |
cmd = "ip domain-lookup" |
| 201 |
if want["domain_lookup"] is False: |
| 202 |
cmd = "no %s" % cmd |
| 203 |
commands.append(cmd) |
| 204 |
|
| 205 |
if want["domain_name"]: |
| 206 |
if want.get("domain_name")[0]["name"] == "default": |
| 207 |
if have["domain_name"]: |
| 208 |
for item in have["domain_name"]: |
| 209 |
cmd = "no ip domain-name %s" % item["name"] |
| 210 |
remove(cmd, commands, item["vrf"]) |
| 211 |
else: |
| 212 |
for item in difference(have, want, "domain_name"): |
| 213 |
cmd = "no ip domain-name %s" % item["name"] |
| 214 |
remove(cmd, commands, item["vrf"]) |
| 215 |
for item in difference(want, have, "domain_name"): |
| 216 |
cmd = "ip domain-name %s" % item["name"] |
| 217 |
add(cmd, commands, item["vrf"]) |
| 218 |
|
| 219 |
if want["domain_search"]: |
| 220 |
if want.get("domain_search")[0]["name"] == "default": |
| 221 |
if have["domain_search"]: |
| 222 |
for item in have["domain_search"]: |
| 223 |
cmd = "no ip domain-list %s" % item["name"] |
| 224 |
remove(cmd, commands, item["vrf"]) |
| 225 |
else: |
| 226 |
for item in difference(have, want, "domain_search"): |
| 227 |
cmd = "no ip domain-list %s" % item["name"] |
| 228 |
remove(cmd, commands, item["vrf"]) |
| 229 |
for item in difference(want, have, "domain_search"): |
| 230 |
cmd = "ip domain-list %s" % item["name"] |
| 231 |
add(cmd, commands, item["vrf"]) |
| 232 |
|
| 233 |
if want["name_servers"]: |
| 234 |
if want.get("name_servers")[0]["server"] == "default": |
| 235 |
if have["name_servers"]: |
| 236 |
for item in have["name_servers"]: |
| 237 |
cmd = "no ip name-server %s" % item["server"] |
| 238 |
remove(cmd, commands, item["vrf"]) |
| 239 |
else: |
| 240 |
for item in difference(have, want, "name_servers"): |
| 241 |
cmd = "no ip name-server %s" % item["server"] |
| 242 |
remove(cmd, commands, item["vrf"]) |
| 243 |
for item in difference(want, have, "name_servers"): |
| 244 |
cmd = "ip name-server %s" % item["server"] |
| 245 |
add(cmd, commands, item["vrf"]) |
| 246 |
|
| 247 |
if needs_update("system_mtu"): |
| 248 |
if want["system_mtu"] == "default": |
| 249 |
if have["system_mtu"]: |
| 250 |
commands.append("no system jumbomtu") |
| 251 |
else: |
| 252 |
commands.append("system jumbomtu %s" % want["system_mtu"]) |
| 253 |
|
| 254 |
return commands |
| 255 |
|
| 256 |
|
| 257 |
def parse_hostname(config): |
| 258 |
match = re.search(r"^hostname (\S+)", config, re.M) |
| 259 |
if match: |
| 260 |
return match.group(1) |
| 261 |
|
| 262 |
|
| 263 |
def parse_domain_name(config, vrf_config): |
| 264 |
objects = list() |
| 265 |
match = re.search(r"^ip domain-name (\S+)", config, re.M) |
| 266 |
if match: |
| 267 |
objects.append({"name": match.group(1), "vrf": None}) |
| 268 |
|
| 269 |
for vrf, cfg in vrf_config.items(): |
| 270 |
match = re.search(r"ip domain-name (\S+)", cfg, re.M) |
| 271 |
if match: |
| 272 |
objects.append({"name": match.group(1), "vrf": vrf}) |
| 273 |
|
| 274 |
return objects |
| 275 |
|
| 276 |
|
| 277 |
def parse_domain_search(config, vrf_config): |
| 278 |
objects = list() |
| 279 |
|
| 280 |
for item in re.findall(r"^ip domain-list (\S+)", config, re.M): |
| 281 |
objects.append({"name": item, "vrf": None}) |
| 282 |
|
| 283 |
for vrf, cfg in vrf_config.items(): |
| 284 |
for item in re.findall(r"ip domain-list (\S+)", cfg, re.M): |
| 285 |
objects.append({"name": item, "vrf": vrf}) |
| 286 |
|
| 287 |
return objects |
| 288 |
|
| 289 |
|
| 290 |
def parse_name_servers(config, vrf_config, vrfs): |
| 291 |
objects = list() |
| 292 |
|
| 293 |
match = re.search("^ip name-server (.+)$", config, re.M) |
| 294 |
if match and "use-vrf" not in match.group(1): |
| 295 |
for addr in match.group(1).split(" "): |
| 296 |
objects.append({"server": addr, "vrf": None}) |
| 297 |
|
| 298 |
for vrf, cfg in vrf_config.items(): |
| 299 |
vrf_match = re.search("ip name-server (.+)", cfg, re.M) |
| 300 |
if vrf_match: |
| 301 |
for addr in vrf_match.group(1).split(" "): |
| 302 |
objects.append({"server": addr, "vrf": vrf}) |
| 303 |
|
| 304 |
return objects |
| 305 |
|
| 306 |
|
| 307 |
def parse_system_mtu(config): |
| 308 |
match = re.search(r"^system jumbomtu (\d+)", config, re.M) |
| 309 |
if match: |
| 310 |
return match.group(1) |
| 311 |
|
| 312 |
|
| 313 |
def map_config_to_obj(module): |
| 314 |
config = get_config(module) |
| 315 |
configobj = NetworkConfig(indent=2, contents=config) |
| 316 |
|
| 317 |
vrf_config = {} |
| 318 |
|
| 319 |
vrfs = re.findall(r"^vrf context (\S+)$", config, re.M) |
| 320 |
for vrf in vrfs: |
| 321 |
config_data = configobj.get_block_config(path=["vrf context %s" % vrf]) |
| 322 |
vrf_config[vrf] = config_data |
| 323 |
|
| 324 |
return { |
| 325 |
"hostname": parse_hostname(config), |
| 326 |
"domain_lookup": "no ip domain-lookup" not in config, |
| 327 |
"domain_name": parse_domain_name(config, vrf_config), |
| 328 |
"domain_search": parse_domain_search(config, vrf_config), |
| 329 |
"name_servers": parse_name_servers(config, vrf_config, vrfs), |
| 330 |
"system_mtu": parse_system_mtu(config), |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
def map_params_to_obj(module): |
| 335 |
obj = { |
| 336 |
"hostname": module.params["hostname"], |
| 337 |
"domain_lookup": module.params["domain_lookup"], |
| 338 |
"system_mtu": module.params["system_mtu"], |
| 339 |
} |
| 340 |
|
| 341 |
domain_name = ComplexList(dict(name=dict(key=True), vrf=dict()), module) |
| 342 |
|
| 343 |
domain_search = ComplexList(dict(name=dict(key=True), vrf=dict()), module) |
| 344 |
|
| 345 |
name_servers = ComplexList(dict(server=dict(key=True), vrf=dict()), module) |
| 346 |
|
| 347 |
for arg, cast in [ |
| 348 |
("domain_name", domain_name), |
| 349 |
("domain_search", domain_search), |
| 350 |
("name_servers", name_servers), |
| 351 |
]: |
| 352 |
if module.params[arg] is not None: |
| 353 |
obj[arg] = cast(module.params[arg]) |
| 354 |
else: |
| 355 |
obj[arg] = None |
| 356 |
|
| 357 |
return obj |
| 358 |
|
| 359 |
|
| 360 |
def main(): |
| 361 |
"""main entry point for module execution""" |
| 362 |
argument_spec = dict( |
| 363 |
hostname=dict(), |
| 364 |
domain_lookup=dict(type="bool"), |
| 365 |
# { name: <str>, vrf: <str> } |
| 366 |
domain_name=dict(type="list", elements="raw"), |
| 367 |
# {name: <str>, vrf: <str> } |
| 368 |
domain_search=dict(type="list", elements="raw"), |
| 369 |
# { server: <str>; vrf: <str> } |
| 370 |
name_servers=dict(type="list", elements="raw"), |
| 371 |
system_mtu=dict(type="str"), |
| 372 |
state=dict(default="present", choices=["present", "absent"]), |
| 373 |
) |
| 374 |
|
| 375 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 376 |
|
| 377 |
warnings = list() |
| 378 |
|
| 379 |
result = {"changed": False} |
| 380 |
if warnings: |
| 381 |
result["warnings"] = warnings |
| 382 |
|
| 383 |
want = map_params_to_obj(module) |
| 384 |
have = map_config_to_obj(module) |
| 385 |
|
| 386 |
commands = map_obj_to_commands(want, have, module) |
| 387 |
result["commands"] = commands |
| 388 |
|
| 389 |
if commands: |
| 390 |
if not module.check_mode: |
| 391 |
load_config(module, commands) |
| 392 |
result["changed"] = True |
| 393 |
|
| 394 |
module.exit_json(**result) |
| 395 |
|
| 396 |
|
| 397 |
if __name__ == "__main__": |
| 398 |
main() |