| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
|
| 5 |
from __future__ import absolute_import, division, print_function |
| 6 |
|
| 7 |
|
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = """ |
| 12 |
module: iosxr_system |
| 13 |
author: |
| 14 |
- Peter Sprygada (@privateip) |
| 15 |
- Kedar Kekan (@kedarX) |
| 16 |
short_description: Module to manage the system attributes. |
| 17 |
description: |
| 18 |
- This module provides declarative management of node system attributes on Cisco IOS |
| 19 |
XR devices. It provides an option to configure host system parameters or remove |
| 20 |
those parameters from the device active configuration. |
| 21 |
version_added: 1.0.0 |
| 22 |
requirements: |
| 23 |
- ncclient >= 0.5.3 when using netconf |
| 24 |
- lxml >= 4.1.1 when using netconf |
| 25 |
extends_documentation_fragment: |
| 26 |
- cisco.iosxr.iosxr |
| 27 |
notes: |
| 28 |
- This module works with connection C(network_cli) and C(netconf). See L(the IOS-XR |
| 29 |
Platform Options,../network/user_guide/platform_iosxr.html). |
| 30 |
- name-servers I(state=absent) operation with C(netconf) transport is a success, but |
| 31 |
with rpc-error. This is due to XR platform issue. Recommended to use I(ignore_errors) |
| 32 |
option with the task as a workaround. |
| 33 |
options: |
| 34 |
hostname: |
| 35 |
description: |
| 36 |
- Configure the device hostname parameter. This option takes an ASCII string value. |
| 37 |
type: str |
| 38 |
vrf: |
| 39 |
description: |
| 40 |
- VRF name for domain services |
| 41 |
type: str |
| 42 |
default: "default" |
| 43 |
domain_name: |
| 44 |
description: |
| 45 |
- Configure the IP domain name on the remote device to the provided value. Value |
| 46 |
should be in the dotted name form and will be appended to the C(hostname) to |
| 47 |
create a fully-qualified domain name. |
| 48 |
type: str |
| 49 |
domain_search: |
| 50 |
description: |
| 51 |
- Provides the list of domain suffixes to append to the hostname for the purpose |
| 52 |
of doing name resolution. This argument accepts a list of names and will be |
| 53 |
reconciled with the current active configuration on the running node. |
| 54 |
type: list |
| 55 |
elements: str |
| 56 |
lookup_source: |
| 57 |
description: |
| 58 |
- The C(lookup_source) argument provides one or more source interfaces to use |
| 59 |
for performing DNS lookups. The interface provided in C(lookup_source) must |
| 60 |
be a valid interface configured on the device. |
| 61 |
type: str |
| 62 |
lookup_enabled: |
| 63 |
description: |
| 64 |
- Provides administrative control for enabling or disabling DNS lookups. When |
| 65 |
this argument is set to True, lookups are performed and when it is set to False, |
| 66 |
lookups are not performed. |
| 67 |
type: bool |
| 68 |
default: true |
| 69 |
name_servers: |
| 70 |
description: |
| 71 |
- The C(name_serves) argument accepts a list of DNS name servers by way of either |
| 72 |
FQDN or IP address to use to perform name resolution lookups. This argument |
| 73 |
accepts wither a list of DNS servers See examples. |
| 74 |
type: list |
| 75 |
elements: 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 (default vrf=default) |
| 90 |
cisco.iosxr.iosxr_system: |
| 91 |
hostname: iosxr01 |
| 92 |
domain_name: test.example.com |
| 93 |
domain_search: |
| 94 |
- ansible.com |
| 95 |
- redhat.com |
| 96 |
- cisco.com |
| 97 |
- name: remove configuration |
| 98 |
cisco.iosxr.iosxr_system: |
| 99 |
hostname: iosxr01 |
| 100 |
domain_name: test.example.com |
| 101 |
domain_search: |
| 102 |
- ansible.com |
| 103 |
- redhat.com |
| 104 |
- cisco.com |
| 105 |
state: absent |
| 106 |
- name: configure hostname and domain-name with vrf |
| 107 |
cisco.iosxr.iosxr_system: |
| 108 |
hostname: iosxr01 |
| 109 |
vrf: nondefault |
| 110 |
domain_name: test.example.com |
| 111 |
domain_search: |
| 112 |
- ansible.com |
| 113 |
- redhat.com |
| 114 |
- cisco.com |
| 115 |
- name: configure DNS lookup sources |
| 116 |
cisco.iosxr.iosxr_system: |
| 117 |
lookup_source: MgmtEth0/0/CPU0/0 |
| 118 |
lookup_enabled: true |
| 119 |
- name: configure name servers |
| 120 |
cisco.iosxr.iosxr_system: |
| 121 |
name_servers: |
| 122 |
- 8.8.8.8 |
| 123 |
- 8.8.4.4 |
| 124 |
""" |
| 125 |
|
| 126 |
RETURN = """ |
| 127 |
commands: |
| 128 |
description: The list of configuration mode commands to send to the device |
| 129 |
returned: always |
| 130 |
type: list |
| 131 |
sample: |
| 132 |
- hostname iosxr01 |
| 133 |
- ip domain-name test.example.com |
| 134 |
xml: |
| 135 |
description: NetConf rpc xml sent to device with transport C(netconf) |
| 136 |
returned: always (empty list when no xml rpc to send) |
| 137 |
type: list |
| 138 |
sample: |
| 139 |
- '<config xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0"> |
| 140 |
<ip-domain xmlns="http://cisco.com/ns/yang/Cisco-IOS-XR-ip-domain-cfg"> |
| 141 |
<vrfs> |
| 142 |
<vrf> |
| 143 |
<vrf-name>default</vrf-name> |
| 144 |
<lists> |
| 145 |
<list xc:operation="merge"> |
| 146 |
<order>0</order> |
| 147 |
<list-name>redhat.com</list-name> |
| 148 |
</list> |
| 149 |
</lists> |
| 150 |
</vrf> |
| 151 |
</vrfs> |
| 152 |
</ip-domain> |
| 153 |
</config>' |
| 154 |
""" |
| 155 |
|
| 156 |
import collections |
| 157 |
import re |
| 158 |
|
| 159 |
from ansible.module_utils.basic import AnsibleModule |
| 160 |
|
| 161 |
from ansible_collections.cisco.iosxr.plugins.module_utils.network.iosxr.iosxr import ( |
| 162 |
build_xml, |
| 163 |
etree_find, |
| 164 |
etree_findall, |
| 165 |
get_config, |
| 166 |
is_cliconf, |
| 167 |
is_netconf, |
| 168 |
load_config, |
| 169 |
) |
| 170 |
|
| 171 |
|
| 172 |
def diff_list(want, have): |
| 173 |
adds = set(want).difference(have) |
| 174 |
removes = set(have).difference(want) |
| 175 |
return (adds, removes) |
| 176 |
|
| 177 |
|
| 178 |
class ConfigBase(object): |
| 179 |
def __init__(self, module): |
| 180 |
self._module = module |
| 181 |
self._result = {"changed": False, "warnings": []} |
| 182 |
self._want = dict() |
| 183 |
self._have = dict() |
| 184 |
|
| 185 |
def map_params_to_obj(self): |
| 186 |
self._want.update( |
| 187 |
{ |
| 188 |
"hostname": self._module.params["hostname"], |
| 189 |
"vrf": self._module.params["vrf"], |
| 190 |
"domain_name": self._module.params["domain_name"], |
| 191 |
"domain_search": self._module.params["domain_search"], |
| 192 |
"lookup_source": self._module.params["lookup_source"], |
| 193 |
"lookup_enabled": self._module.params["lookup_enabled"], |
| 194 |
"name_servers": self._module.params["name_servers"], |
| 195 |
}, |
| 196 |
) |
| 197 |
|
| 198 |
|
| 199 |
class CliConfiguration(ConfigBase): |
| 200 |
def __init__(self, module): |
| 201 |
super(CliConfiguration, self).__init__(module) |
| 202 |
|
| 203 |
def map_obj_to_commands(self): |
| 204 |
commands = list() |
| 205 |
state = self._module.params["state"] |
| 206 |
|
| 207 |
def needs_update(x): |
| 208 |
return self._want.get(x) and (self._want.get(x) != self._have.get(x)) |
| 209 |
|
| 210 |
if state == "absent": |
| 211 |
if self._have["hostname"] != "ios": |
| 212 |
commands.append("no hostname") |
| 213 |
if self._have["domain_name"]: |
| 214 |
commands.append("no domain name") |
| 215 |
if self._have["lookup_source"]: |
| 216 |
commands.append( |
| 217 |
"no domain lookup source-interface {0!s}".format(self._have["lookup_source"]), |
| 218 |
) |
| 219 |
if not self._have["lookup_enabled"]: |
| 220 |
commands.append("no domain lookup disable") |
| 221 |
for item in self._have["name_servers"]: |
| 222 |
commands.append("no domain name-server {0!s}".format(item)) |
| 223 |
for item in self._have["domain_search"]: |
| 224 |
commands.append("no domain list {0!s}".format(item)) |
| 225 |
|
| 226 |
elif state == "present": |
| 227 |
if needs_update("hostname"): |
| 228 |
commands.append("hostname {0!s}".format(self._want["hostname"])) |
| 229 |
|
| 230 |
if needs_update("domain_name"): |
| 231 |
commands.append("domain name {0!s}".format(self._want["domain_name"])) |
| 232 |
|
| 233 |
if needs_update("lookup_source"): |
| 234 |
commands.append( |
| 235 |
"domain lookup source-interface {0!s}".format(self._want["lookup_source"]), |
| 236 |
) |
| 237 |
|
| 238 |
cmd = None |
| 239 |
if not self._want["lookup_enabled"] and self._have["lookup_enabled"]: |
| 240 |
cmd = "domain lookup disable" |
| 241 |
elif self._want["lookup_enabled"] and not self._have["lookup_enabled"]: |
| 242 |
cmd = "no domain lookup disable" |
| 243 |
if cmd is not None: |
| 244 |
commands.append(cmd) |
| 245 |
|
| 246 |
if self._want["name_servers"] is not None: |
| 247 |
adds, removes = diff_list(self._want["name_servers"], self._have["name_servers"]) |
| 248 |
for item in adds: |
| 249 |
commands.append("domain name-server {0!s}".format(item)) |
| 250 |
for item in removes: |
| 251 |
commands.append("no domain name-server {0!s}".format(item)) |
| 252 |
|
| 253 |
if self._want["domain_search"] is not None: |
| 254 |
adds, removes = diff_list(self._want["domain_search"], self._have["domain_search"]) |
| 255 |
for item in adds: |
| 256 |
commands.append("domain list {0!s}".format(item)) |
| 257 |
for item in removes: |
| 258 |
commands.append("no domain list {0!s}".format(item)) |
| 259 |
|
| 260 |
self._result["commands"] = [] |
| 261 |
if commands: |
| 262 |
commit = not self._module.check_mode |
| 263 |
diff = load_config(self._module, commands, commit=commit) |
| 264 |
if diff: |
| 265 |
self._result["diff"] = dict(prepared=diff) |
| 266 |
|
| 267 |
self._result["commands"] = commands |
| 268 |
self._result["changed"] = True |
| 269 |
|
| 270 |
def parse_hostname(self, config): |
| 271 |
match = re.search(r"^hostname (\S+)", config, re.M) |
| 272 |
if match: |
| 273 |
return match.group(1) |
| 274 |
|
| 275 |
def parse_domain_name(self, config): |
| 276 |
match = re.search(r"^domain name (\S+)", config, re.M) |
| 277 |
if match: |
| 278 |
return match.group(1) |
| 279 |
|
| 280 |
def parse_lookup_source(self, config): |
| 281 |
match = re.search(r"^domain lookup source-interface (\S+)", config, re.M) |
| 282 |
if match: |
| 283 |
return match.group(1) |
| 284 |
|
| 285 |
def map_config_to_obj(self): |
| 286 |
config = get_config(self._module) |
| 287 |
self._have.update( |
| 288 |
{ |
| 289 |
"hostname": self.parse_hostname(config), |
| 290 |
"domain_name": self.parse_domain_name(config), |
| 291 |
"domain_search": re.findall(r"^domain list (\S+)", config, re.M), |
| 292 |
"lookup_source": self.parse_lookup_source(config), |
| 293 |
"lookup_enabled": "domain lookup disable" not in config, |
| 294 |
"name_servers": re.findall(r"^domain name-server (\S+)", config, re.M), |
| 295 |
}, |
| 296 |
) |
| 297 |
|
| 298 |
def run(self): |
| 299 |
self.map_params_to_obj() |
| 300 |
self.map_config_to_obj() |
| 301 |
self.map_obj_to_commands() |
| 302 |
|
| 303 |
return self._result |
| 304 |
|
| 305 |
|
| 306 |
class NCConfiguration(ConfigBase): |
| 307 |
def __init__(self, module): |
| 308 |
super(NCConfiguration, self).__init__(module) |
| 309 |
self._system_meta = collections.OrderedDict() |
| 310 |
self._system_domain_meta = collections.OrderedDict() |
| 311 |
self._system_server_meta = collections.OrderedDict() |
| 312 |
self._hostname_meta = collections.OrderedDict() |
| 313 |
self._lookup_source_meta = collections.OrderedDict() |
| 314 |
self._lookup_meta = collections.OrderedDict() |
| 315 |
|
| 316 |
def map_obj_to_xml_rpc(self): |
| 317 |
self._system_meta.update( |
| 318 |
[ |
| 319 |
("vrfs", {"xpath": "ip-domain/vrfs", "tag": True, "operation": "edit"}), |
| 320 |
("vrf", {"xpath": "ip-domain/vrfs/vrf", "tag": True, "operation": "edit"}), |
| 321 |
("a:vrf", {"xpath": "ip-domain/vrfs/vrf/vrf-name", "operation": "edit"}), |
| 322 |
( |
| 323 |
"a:domain_name", |
| 324 |
{ |
| 325 |
"xpath": "ip-domain/vrfs/vrf/name", |
| 326 |
"operation": "edit", |
| 327 |
"attrib": "operation", |
| 328 |
}, |
| 329 |
), |
| 330 |
], |
| 331 |
) |
| 332 |
|
| 333 |
self._system_domain_meta.update( |
| 334 |
[ |
| 335 |
("vrfs", {"xpath": "ip-domain/vrfs", "tag": True, "operation": "edit"}), |
| 336 |
("vrf", {"xpath": "ip-domain/vrfs/vrf", "tag": True, "operation": "edit"}), |
| 337 |
("a:vrf", {"xpath": "ip-domain/vrfs/vrf/vrf-name", "operation": "edit"}), |
| 338 |
("lists", {"xpath": "ip-domain/vrfs/vrf/lists", "tag": True, "operation": "edit"}), |
| 339 |
( |
| 340 |
"list", |
| 341 |
{ |
| 342 |
"xpath": "ip-domain/vrfs/vrf/lists/list", |
| 343 |
"tag": True, |
| 344 |
"operation": "edit", |
| 345 |
"attrib": "operation", |
| 346 |
}, |
| 347 |
), |
| 348 |
("a:order", {"xpath": "ip-domain/vrfs/vrf/lists/list/order", "operation": "edit"}), |
| 349 |
( |
| 350 |
"a:domain_search", |
| 351 |
{"xpath": "ip-domain/vrfs/vrf/lists/list/list-name", "operation": "edit"}, |
| 352 |
), |
| 353 |
], |
| 354 |
) |
| 355 |
|
| 356 |
self._system_server_meta.update( |
| 357 |
[ |
| 358 |
("vrfs", {"xpath": "ip-domain/vrfs", "tag": True, "operation": "edit"}), |
| 359 |
("vrf", {"xpath": "ip-domain/vrfs/vrf", "tag": True, "operation": "edit"}), |
| 360 |
("a:vrf", {"xpath": "ip-domain/vrfs/vrf/vrf-name", "operation": "edit"}), |
| 361 |
( |
| 362 |
"servers", |
| 363 |
{"xpath": "ip-domain/vrfs/vrf/servers", "tag": True, "operation": "edit"}, |
| 364 |
), |
| 365 |
( |
| 366 |
"server", |
| 367 |
{ |
| 368 |
"xpath": "ip-domain/vrfs/vrf/servers/server", |
| 369 |
"tag": True, |
| 370 |
"operation": "edit", |
| 371 |
"attrib": "operation", |
| 372 |
}, |
| 373 |
), |
| 374 |
( |
| 375 |
"a:order", |
| 376 |
{"xpath": "ip-domain/vrfs/vrf/servers/server/order", "operation": "edit"}, |
| 377 |
), |
| 378 |
( |
| 379 |
"a:name_servers", |
| 380 |
{ |
| 381 |
"xpath": "ip-domain/vrfs/vrf/servers/server/server-address", |
| 382 |
"operation": "edit", |
| 383 |
}, |
| 384 |
), |
| 385 |
], |
| 386 |
) |
| 387 |
|
| 388 |
self._hostname_meta.update( |
| 389 |
[ |
| 390 |
( |
| 391 |
"a:hostname", |
| 392 |
{"xpath": "host-names/host-name", "operation": "edit", "attrib": "operation"}, |
| 393 |
), |
| 394 |
], |
| 395 |
) |
| 396 |
|
| 397 |
self._lookup_source_meta.update( |
| 398 |
[ |
| 399 |
("vrfs", {"xpath": "ip-domain/vrfs", "tag": True, "operation": "edit"}), |
| 400 |
("vrf", {"xpath": "ip-domain/vrfs/vrf", "tag": True, "operation": "edit"}), |
| 401 |
("a:vrf", {"xpath": "ip-domain/vrfs/vrf/vrf-name", "operation": "edit"}), |
| 402 |
( |
| 403 |
"a:lookup_source", |
| 404 |
{ |
| 405 |
"xpath": "ip-domain/vrfs/vrf/source-interface", |
| 406 |
"operation": "edit", |
| 407 |
"attrib": "operation", |
| 408 |
}, |
| 409 |
), |
| 410 |
], |
| 411 |
) |
| 412 |
|
| 413 |
self._lookup_meta.update( |
| 414 |
[ |
| 415 |
("vrfs", {"xpath": "ip-domain/vrfs", "tag": True, "operation": "edit"}), |
| 416 |
("vrf", {"xpath": "ip-domain/vrfs/vrf", "tag": True, "operation": "edit"}), |
| 417 |
("a:vrf", {"xpath": "ip-domain/vrfs/vrf/vrf-name", "operation": "edit"}), |
| 418 |
( |
| 419 |
"lookup", |
| 420 |
{ |
| 421 |
"xpath": "ip-domain/vrfs/vrf/lookup", |
| 422 |
"tag": True, |
| 423 |
"operation": "edit", |
| 424 |
"attrib": "operation", |
| 425 |
}, |
| 426 |
), |
| 427 |
], |
| 428 |
) |
| 429 |
|
| 430 |
state = self._module.params["state"] |
| 431 |
_get_filter = build_xml("ip-domain", opcode="filter") |
| 432 |
running = get_config(self._module, source="running", config_filter=_get_filter) |
| 433 |
_get_filter = build_xml("host-names", opcode="filter") |
| 434 |
hostname_runn = get_config(self._module, source="running", config_filter=_get_filter) |
| 435 |
|
| 436 |
hostname_ele = etree_find(hostname_runn, "host-name") |
| 437 |
hostname = hostname_ele.text if hostname_ele is not None else None |
| 438 |
|
| 439 |
vrf_ele = etree_findall(running, "vrf") |
| 440 |
vrf_map = {} |
| 441 |
for vrf in vrf_ele: |
| 442 |
name_server_list = list() |
| 443 |
domain_list = list() |
| 444 |
vrf_name_ele = etree_find(vrf, "vrf-name") |
| 445 |
vrf_name = vrf_name_ele.text if vrf_name_ele is not None else None |
| 446 |
|
| 447 |
domain_name_ele = etree_find(vrf, "name") |
| 448 |
domain_name = domain_name_ele.text if domain_name_ele is not None else None |
| 449 |
|
| 450 |
domain_ele = etree_findall(vrf, "list-name") |
| 451 |
for domain in domain_ele: |
| 452 |
domain_list.append(domain.text) |
| 453 |
|
| 454 |
server_ele = etree_findall(vrf, "server-address") |
| 455 |
for server in server_ele: |
| 456 |
name_server_list.append(server.text) |
| 457 |
|
| 458 |
lookup_source_ele = etree_find(vrf, "source-interface") |
| 459 |
lookup_source = lookup_source_ele.text if lookup_source_ele is not None else None |
| 460 |
|
| 461 |
lookup_enabled = False if etree_find(vrf, "lookup") is not None else True |
| 462 |
|
| 463 |
vrf_map[vrf_name] = { |
| 464 |
"domain_name": domain_name, |
| 465 |
"domain_search": domain_list, |
| 466 |
"name_servers": name_server_list, |
| 467 |
"lookup_source": lookup_source, |
| 468 |
"lookup_enabled": lookup_enabled, |
| 469 |
} |
| 470 |
|
| 471 |
opcode = None |
| 472 |
hostname_param = {} |
| 473 |
lookup_param = {} |
| 474 |
system_param = {} |
| 475 |
sys_server_params = list() |
| 476 |
sys_domain_params = list() |
| 477 |
add_domain_params = list() |
| 478 |
del_domain_params = list() |
| 479 |
add_server_params = list() |
| 480 |
del_server_params = list() |
| 481 |
lookup_source_params = {} |
| 482 |
|
| 483 |
try: |
| 484 |
sys_node = vrf_map[self._want["vrf"]] |
| 485 |
except KeyError: |
| 486 |
sys_node = { |
| 487 |
"domain_name": None, |
| 488 |
"domain_search": [], |
| 489 |
"name_servers": [], |
| 490 |
"lookup_source": None, |
| 491 |
"lookup_enabled": True, |
| 492 |
} |
| 493 |
|
| 494 |
if state == "absent": |
| 495 |
opcode = "delete" |
| 496 |
|
| 497 |
def needs_update(x): |
| 498 |
return self._want[x] is not None and self._want[x] == sys_node[x] |
| 499 |
|
| 500 |
if needs_update("domain_name"): |
| 501 |
system_param = {"vrf": self._want["vrf"], "domain_name": self._want["domain_name"]} |
| 502 |
|
| 503 |
if needs_update("hostname"): |
| 504 |
hostname_param = {"hostname": hostname} |
| 505 |
|
| 506 |
if not self._want["lookup_enabled"] and not sys_node["lookup_enabled"]: |
| 507 |
lookup_param["vrf"] = self._want["vrf"] |
| 508 |
|
| 509 |
if needs_update("lookup_source"): |
| 510 |
lookup_source_params["vrf"] = self._want["vrf"] |
| 511 |
lookup_source_params["lookup_source"] = self._want["lookup_source"] |
| 512 |
|
| 513 |
if self._want["domain_search"]: |
| 514 |
domain_param = {} |
| 515 |
domain_param["domain_name"] = self._want["domain_name"] |
| 516 |
domain_param["vrf"] = self._want["vrf"] |
| 517 |
domain_param["order"] = "0" |
| 518 |
for domain in self._want["domain_search"]: |
| 519 |
if domain in sys_node["domain_search"]: |
| 520 |
domain_param["domain_search"] = domain |
| 521 |
sys_domain_params.append(domain_param.copy()) |
| 522 |
|
| 523 |
if self._want["name_servers"]: |
| 524 |
server_param = {} |
| 525 |
server_param["vrf"] = self._want["vrf"] |
| 526 |
server_param["order"] = "0" |
| 527 |
for server in self._want["name_servers"]: |
| 528 |
if server in sys_node["name_servers"]: |
| 529 |
server_param["name_servers"] = server |
| 530 |
sys_server_params.append(server_param.copy()) |
| 531 |
|
| 532 |
elif state == "present": |
| 533 |
opcode = "merge" |
| 534 |
|
| 535 |
def needs_update(x): |
| 536 |
return self._want[x] is not None and ( |
| 537 |
sys_node[x] is None |
| 538 |
or (sys_node[x] is not None and self._want[x] != sys_node[x]) |
| 539 |
) |
| 540 |
|
| 541 |
if needs_update("domain_name"): |
| 542 |
system_param = {"vrf": self._want["vrf"], "domain_name": self._want["domain_name"]} |
| 543 |
|
| 544 |
if self._want["hostname"] is not None and self._want["hostname"] != hostname: |
| 545 |
hostname_param = {"hostname": self._want["hostname"]} |
| 546 |
|
| 547 |
if not self._want["lookup_enabled"] and sys_node["lookup_enabled"]: |
| 548 |
lookup_param["vrf"] = self._want["vrf"] |
| 549 |
|
| 550 |
if needs_update("lookup_source"): |
| 551 |
lookup_source_params["vrf"] = self._want["vrf"] |
| 552 |
lookup_source_params["lookup_source"] = self._want["lookup_source"] |
| 553 |
|
| 554 |
if self._want["domain_search"]: |
| 555 |
domain_adds, domain_removes = diff_list( |
| 556 |
self._want["domain_search"], |
| 557 |
sys_node["domain_search"], |
| 558 |
) |
| 559 |
domain_param = {} |
| 560 |
domain_param["domain_name"] = self._want["domain_name"] |
| 561 |
domain_param["vrf"] = self._want["vrf"] |
| 562 |
domain_param["order"] = "0" |
| 563 |
for domain in domain_adds: |
| 564 |
if domain not in sys_node["domain_search"]: |
| 565 |
domain_param["domain_search"] = domain |
| 566 |
add_domain_params.append(domain_param.copy()) |
| 567 |
for domain in domain_removes: |
| 568 |
if domain in sys_node["domain_search"]: |
| 569 |
domain_param["domain_search"] = domain |
| 570 |
del_domain_params.append(domain_param.copy()) |
| 571 |
|
| 572 |
if self._want["name_servers"]: |
| 573 |
server_adds, server_removes = diff_list( |
| 574 |
self._want["name_servers"], |
| 575 |
sys_node["name_servers"], |
| 576 |
) |
| 577 |
server_param = {} |
| 578 |
server_param["vrf"] = self._want["vrf"] |
| 579 |
server_param["order"] = "0" |
| 580 |
for domain in server_adds: |
| 581 |
if domain not in sys_node["name_servers"]: |
| 582 |
server_param["name_servers"] = domain |
| 583 |
add_server_params.append(server_param.copy()) |
| 584 |
for domain in server_removes: |
| 585 |
if domain in sys_node["name_servers"]: |
| 586 |
server_param["name_servers"] = domain |
| 587 |
del_server_params.append(server_param.copy()) |
| 588 |
|
| 589 |
self._result["xml"] = [] |
| 590 |
_edit_filter_list = list() |
| 591 |
if opcode: |
| 592 |
if hostname_param: |
| 593 |
_edit_filter_list.append( |
| 594 |
build_xml( |
| 595 |
"host-names", |
| 596 |
xmap=self._hostname_meta, |
| 597 |
params=hostname_param, |
| 598 |
opcode=opcode, |
| 599 |
), |
| 600 |
) |
| 601 |
|
| 602 |
if system_param: |
| 603 |
_edit_filter_list.append( |
| 604 |
build_xml( |
| 605 |
"ip-domain", |
| 606 |
xmap=self._system_meta, |
| 607 |
params=system_param, |
| 608 |
opcode=opcode, |
| 609 |
), |
| 610 |
) |
| 611 |
|
| 612 |
if lookup_source_params: |
| 613 |
_edit_filter_list.append( |
| 614 |
build_xml( |
| 615 |
"ip-domain", |
| 616 |
xmap=self._lookup_source_meta, |
| 617 |
params=lookup_source_params, |
| 618 |
opcode=opcode, |
| 619 |
), |
| 620 |
) |
| 621 |
if lookup_param: |
| 622 |
_edit_filter_list.append( |
| 623 |
build_xml( |
| 624 |
"ip-domain", |
| 625 |
xmap=self._lookup_meta, |
| 626 |
params=lookup_param, |
| 627 |
opcode=opcode, |
| 628 |
), |
| 629 |
) |
| 630 |
|
| 631 |
if opcode == "delete": |
| 632 |
if sys_domain_params: |
| 633 |
_edit_filter_list.append( |
| 634 |
build_xml( |
| 635 |
"ip-domain", |
| 636 |
xmap=self._system_domain_meta, |
| 637 |
params=sys_domain_params, |
| 638 |
opcode=opcode, |
| 639 |
), |
| 640 |
) |
| 641 |
if sys_server_params: |
| 642 |
_edit_filter_list.append( |
| 643 |
build_xml( |
| 644 |
"ip-domain", |
| 645 |
xmap=self._system_server_meta, |
| 646 |
params=sys_server_params, |
| 647 |
opcode=opcode, |
| 648 |
), |
| 649 |
) |
| 650 |
if self._want["vrf"] != "default": |
| 651 |
self._result["warnings"] = [ |
| 652 |
"name-servers delete operation with non-default vrf is a success, " |
| 653 |
"but with rpc-error. Recommended to use 'ignore_errors' option with the task as a workaround", |
| 654 |
] |
| 655 |
elif opcode == "merge": |
| 656 |
if add_domain_params: |
| 657 |
_edit_filter_list.append( |
| 658 |
build_xml( |
| 659 |
"ip-domain", |
| 660 |
xmap=self._system_domain_meta, |
| 661 |
params=add_domain_params, |
| 662 |
opcode=opcode, |
| 663 |
), |
| 664 |
) |
| 665 |
if del_domain_params: |
| 666 |
_edit_filter_list.append( |
| 667 |
build_xml( |
| 668 |
"ip-domain", |
| 669 |
xmap=self._system_domain_meta, |
| 670 |
params=del_domain_params, |
| 671 |
opcode="delete", |
| 672 |
), |
| 673 |
) |
| 674 |
|
| 675 |
if add_server_params: |
| 676 |
_edit_filter_list.append( |
| 677 |
build_xml( |
| 678 |
"ip-domain", |
| 679 |
xmap=self._system_server_meta, |
| 680 |
params=add_server_params, |
| 681 |
opcode=opcode, |
| 682 |
), |
| 683 |
) |
| 684 |
if del_server_params: |
| 685 |
_edit_filter_list.append( |
| 686 |
build_xml( |
| 687 |
"ip-domain", |
| 688 |
xmap=self._system_server_meta, |
| 689 |
params=del_server_params, |
| 690 |
opcode="delete", |
| 691 |
), |
| 692 |
) |
| 693 |
|
| 694 |
diff = None |
| 695 |
if _edit_filter_list: |
| 696 |
commit = not self._module.check_mode |
| 697 |
diff = load_config( |
| 698 |
self._module, |
| 699 |
_edit_filter_list, |
| 700 |
commit=commit, |
| 701 |
running=running, |
| 702 |
nc_get_filter=_get_filter, |
| 703 |
) |
| 704 |
|
| 705 |
if diff: |
| 706 |
if self._module._diff: |
| 707 |
self._result["diff"] = dict(prepared=diff) |
| 708 |
|
| 709 |
self._result["xml"] = _edit_filter_list |
| 710 |
self._result["changed"] = True |
| 711 |
|
| 712 |
def run(self): |
| 713 |
self.map_params_to_obj() |
| 714 |
self.map_obj_to_xml_rpc() |
| 715 |
|
| 716 |
return self._result |
| 717 |
|
| 718 |
|
| 719 |
def main(): |
| 720 |
"""Main entry point for Ansible module execution""" |
| 721 |
argument_spec = dict( |
| 722 |
hostname=dict(type="str"), |
| 723 |
vrf=dict(type="str", default="default"), |
| 724 |
domain_name=dict(type="str"), |
| 725 |
domain_search=dict(type="list", elements="str"), |
| 726 |
name_servers=dict(type="list", elements="str"), |
| 727 |
lookup_source=dict(type="str"), |
| 728 |
lookup_enabled=dict(type="bool", default=True), |
| 729 |
state=dict(choices=["present", "absent"], default="present"), |
| 730 |
) |
| 731 |
|
| 732 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 733 |
|
| 734 |
config_object = None |
| 735 |
if is_cliconf(module): |
| 736 |
# Commenting the below cliconf deprecation support call for Ansible 2.9 as it'll be continued to be supported |
| 737 |
config_object = CliConfiguration(module) |
| 738 |
elif is_netconf(module): |
| 739 |
config_object = NCConfiguration(module) |
| 740 |
|
| 741 |
result = None |
| 742 |
if config_object: |
| 743 |
result = config_object.run() |
| 744 |
module.exit_json(**result) |
| 745 |
|
| 746 |
|
| 747 |
if __name__ == "__main__": |
| 748 |
main() |