| 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 |
DOCUMENTATION = """ |
| 23 |
module: ios_vrf |
| 24 |
author: Peter Sprygada (@privateip) |
| 25 |
short_description: Module to configure VRF definitions. |
| 26 |
description: |
| 27 |
- This module provides declarative management of VRF definitions on Cisco IOS devices. It |
| 28 |
allows playbooks to manage individual or the entire VRF collection. It also supports |
| 29 |
purging VRF definitions from the configuration that are not explicitly defined. |
| 30 |
version_added: 1.0.0 |
| 31 |
extends_documentation_fragment: |
| 32 |
- cisco.ios.ios |
| 33 |
notes: |
| 34 |
- Tested against Cisco IOSXE Version 17.3 on CML. |
| 35 |
- This module works with connection C(network_cli). |
| 36 |
See U(https://docs.ansible.com/ansible/latest/network/user_guide/platform_ios.html) |
| 37 |
options: |
| 38 |
vrfs: |
| 39 |
description: |
| 40 |
- The set of VRF definition objects to be configured on the remote IOS device. Ths |
| 41 |
list entries can either be the VRF name or a hash of VRF definitions and attributes. This |
| 42 |
argument is mutually exclusive with the C(name) argument. |
| 43 |
type: list |
| 44 |
elements: raw |
| 45 |
name: |
| 46 |
description: |
| 47 |
- The name of the VRF definition to be managed on the remote IOS device. The |
| 48 |
VRF definition name is an ASCII string name used to uniquely identify the VRF. This |
| 49 |
argument is mutually exclusive with the C(vrfs) argument |
| 50 |
type: str |
| 51 |
description: |
| 52 |
description: |
| 53 |
- Provides a short description of the VRF definition in the current active configuration. The |
| 54 |
VRF definition value accepts alphanumeric characters used to provide additional |
| 55 |
information about the VRF. |
| 56 |
type: str |
| 57 |
address_family: |
| 58 |
description: |
| 59 |
- The list of address families with MDT parameters to be configured on the remote IOS device. |
| 60 |
type: list |
| 61 |
suboptions: |
| 62 |
afi: |
| 63 |
description: Address family identifier. |
| 64 |
type: str |
| 65 |
choices: |
| 66 |
["ipv4", "ipv6"] |
| 67 |
mdt: |
| 68 |
description: MDT parameters. |
| 69 |
type: dict |
| 70 |
suboptions: |
| 71 |
auto_discovery: |
| 72 |
description: Auto-discovery parameters. |
| 73 |
type: dict |
| 74 |
suboptions: |
| 75 |
vxlan: |
| 76 |
description: Vxlan parameters. |
| 77 |
type: dict |
| 78 |
suboptions: |
| 79 |
enable: |
| 80 |
description: Enable VXLAN. |
| 81 |
type: bool |
| 82 |
inter_as: |
| 83 |
description: Enable inter-as. |
| 84 |
type: bool |
| 85 |
default: |
| 86 |
description: Parameters for default option. |
| 87 |
type: dict |
| 88 |
suboptions: |
| 89 |
vxlan_mcast_group: |
| 90 |
description: VXLAN multicast group value. |
| 91 |
type: str |
| 92 |
data: |
| 93 |
description: Parameters for data option. |
| 94 |
type: dict |
| 95 |
suboptions: |
| 96 |
vxlan_mcast_group: |
| 97 |
description: VXLAN multicast group value. |
| 98 |
type: str |
| 99 |
threshold: |
| 100 |
description: Threshold value. |
| 101 |
type: int |
| 102 |
overlay: |
| 103 |
description: Parameters for overlay option. |
| 104 |
type: dict |
| 105 |
suboptions: |
| 106 |
use_bgp: |
| 107 |
description: parameters for BGP option. |
| 108 |
type: dict |
| 109 |
suboptions: |
| 110 |
enable: |
| 111 |
description: Enable use BGP. |
| 112 |
type: bool |
| 113 |
spt_only: |
| 114 |
description: Enable SPT only. |
| 115 |
type: bool |
| 116 |
elements: dict |
| 117 |
rd: |
| 118 |
description: |
| 119 |
- The router-distinguisher value uniquely identifies the VRF to routing processes |
| 120 |
on the remote IOS system. The RD value takes the form of C(A:B) where C(A) |
| 121 |
and C(B) are both numeric values. |
| 122 |
type: str |
| 123 |
interfaces: |
| 124 |
description: |
| 125 |
- Identifies the set of interfaces that should be configured in the VRF. Interfaces |
| 126 |
must be routed interfaces in order to be placed into a VRF. |
| 127 |
type: list |
| 128 |
elements: str |
| 129 |
associated_interfaces: |
| 130 |
description: |
| 131 |
- This is a intent option and checks the operational state of the for given vrf |
| 132 |
C(name) for associated interfaces. If the value in the C(associated_interfaces) |
| 133 |
does not match with the operational state of vrf interfaces on device it will |
| 134 |
result in failure. |
| 135 |
type: list |
| 136 |
elements: str |
| 137 |
delay: |
| 138 |
description: |
| 139 |
- Time in seconds to wait before checking for the operational state on remote |
| 140 |
device. |
| 141 |
default: 10 |
| 142 |
type: int |
| 143 |
purge: |
| 144 |
description: |
| 145 |
- Instructs the module to consider the VRF definition absolute. It will remove |
| 146 |
any previously configured VRFs on the device. |
| 147 |
default: false |
| 148 |
type: bool |
| 149 |
state: |
| 150 |
description: |
| 151 |
- Configures the state of the VRF definition as it relates to the device operational |
| 152 |
configuration. When set to I(present), the VRF should be configured in the |
| 153 |
device active configuration and when set to I(absent) the VRF should not be |
| 154 |
in the device active configuration |
| 155 |
default: present |
| 156 |
choices: |
| 157 |
- present |
| 158 |
- absent |
| 159 |
type: str |
| 160 |
route_both: |
| 161 |
description: |
| 162 |
- Adds an export and import list of extended route target communities to the VRF. |
| 163 |
type: list |
| 164 |
elements: str |
| 165 |
route_export: |
| 166 |
description: |
| 167 |
- Adds an export list of extended route target communities to the VRF. |
| 168 |
type: list |
| 169 |
elements: str |
| 170 |
route_import: |
| 171 |
description: |
| 172 |
- Adds an import list of extended route target communities to the VRF. |
| 173 |
type: list |
| 174 |
elements: str |
| 175 |
route_both_ipv4: |
| 176 |
description: |
| 177 |
- Adds an export and import list of extended route target communities in address-family |
| 178 |
configuration submode to the VRF. |
| 179 |
type: list |
| 180 |
elements: str |
| 181 |
route_export_ipv4: |
| 182 |
description: |
| 183 |
- Adds an export list of extended route target communities in address-family configuration |
| 184 |
submode to the VRF. |
| 185 |
type: list |
| 186 |
elements: str |
| 187 |
route_import_ipv4: |
| 188 |
description: |
| 189 |
- Adds an import list of extended route target communities in address-family configuration |
| 190 |
submode to the VRF. |
| 191 |
type: list |
| 192 |
elements: str |
| 193 |
route_both_ipv6: |
| 194 |
description: |
| 195 |
- Adds an export and import list of extended route target communities in address-family |
| 196 |
configuration submode to the VRF. |
| 197 |
type: list |
| 198 |
elements: str |
| 199 |
route_export_ipv6: |
| 200 |
description: |
| 201 |
- Adds an export list of extended route target communities in address-family configuration |
| 202 |
submode to the VRF. |
| 203 |
type: list |
| 204 |
elements: str |
| 205 |
route_import_ipv6: |
| 206 |
description: |
| 207 |
- Adds an import list of extended route target communities in address-family configuration |
| 208 |
submode to the VRF. |
| 209 |
type: list |
| 210 |
elements: str |
| 211 |
""" |
| 212 |
|
| 213 |
EXAMPLES = """ |
| 214 |
- name: Configure a vrf named management |
| 215 |
cisco.ios.ios_vrf: |
| 216 |
name: management |
| 217 |
description: oob mgmt vrf |
| 218 |
interfaces: |
| 219 |
- Management1 |
| 220 |
|
| 221 |
- name: Remove a vrf named test |
| 222 |
cisco.ios.ios_vrf: |
| 223 |
name: test |
| 224 |
state: absent |
| 225 |
|
| 226 |
- name: Configure set of VRFs and purge any others |
| 227 |
cisco.ios.ios_vrf: |
| 228 |
vrfs: |
| 229 |
- red |
| 230 |
- blue |
| 231 |
- green |
| 232 |
purge: true |
| 233 |
|
| 234 |
- name: Creates a list of import RTs for the VRF with the same parameters |
| 235 |
cisco.ios.ios_vrf: |
| 236 |
name: test_import |
| 237 |
rd: 1:100 |
| 238 |
route_import: |
| 239 |
- 1:100 |
| 240 |
- 3:100 |
| 241 |
|
| 242 |
- name: |
| 243 |
Creates a list of import RTs in address-family configuration submode for the |
| 244 |
VRF with the same parameters |
| 245 |
cisco.ios.ios_vrf: |
| 246 |
name: test_import_ipv4 |
| 247 |
rd: 1:100 |
| 248 |
route_import_ipv4: |
| 249 |
- 1:100 |
| 250 |
- 3:100 |
| 251 |
|
| 252 |
- name: |
| 253 |
Creates a list of import RTs in address-family configuration submode for the |
| 254 |
VRF with the same parameters |
| 255 |
cisco.ios.ios_vrf: |
| 256 |
name: test_import_ipv6 |
| 257 |
rd: 1:100 |
| 258 |
route_import_ipv6: |
| 259 |
- 1:100 |
| 260 |
- 3:100 |
| 261 |
|
| 262 |
- name: Creates a list of export RTs for the VRF with the same parameters |
| 263 |
cisco.ios.ios_vrf: |
| 264 |
name: test_export |
| 265 |
rd: 1:100 |
| 266 |
route_export: |
| 267 |
- 1:100 |
| 268 |
- 3:100 |
| 269 |
|
| 270 |
- name: |
| 271 |
Creates a list of export RTs in address-family configuration submode for the |
| 272 |
VRF with the same parameters |
| 273 |
cisco.ios.ios_vrf: |
| 274 |
name: test_export_ipv4 |
| 275 |
rd: 1:100 |
| 276 |
route_export_ipv4: |
| 277 |
- 1:100 |
| 278 |
- 3:100 |
| 279 |
|
| 280 |
- name: |
| 281 |
Creates a list of export RTs in address-family configuration submode for the |
| 282 |
VRF with the same parameters |
| 283 |
cisco.ios.ios_vrf: |
| 284 |
name: test_export_ipv6 |
| 285 |
rd: 1:100 |
| 286 |
route_export_ipv6: |
| 287 |
- 1:100 |
| 288 |
- 3:100 |
| 289 |
|
| 290 |
- name: |
| 291 |
Creates a list of import and export route targets for the VRF with the same |
| 292 |
parameters |
| 293 |
cisco.ios.ios_vrf: |
| 294 |
name: test_both |
| 295 |
rd: 1:100 |
| 296 |
route_both: |
| 297 |
- 1:100 |
| 298 |
- 3:100 |
| 299 |
|
| 300 |
- name: |
| 301 |
Creates a list of import and export route targets in address-family configuration |
| 302 |
submode for the VRF with the same parameters |
| 303 |
cisco.ios.ios_vrf: |
| 304 |
name: test_both_ipv4 |
| 305 |
rd: 1:100 |
| 306 |
route_both_ipv4: |
| 307 |
- 1:100 |
| 308 |
- 3:100 |
| 309 |
|
| 310 |
- name: |
| 311 |
Creates a list of import and export route targets in address-family configuration |
| 312 |
submode for the VRF with the same parameters |
| 313 |
cisco.ios.ios_vrf: |
| 314 |
name: test_both_ipv6 |
| 315 |
rd: 1:100 |
| 316 |
route_both_ipv6: |
| 317 |
- 1:100 |
| 318 |
- 3:100 |
| 319 |
""" |
| 320 |
|
| 321 |
RETURN = """ |
| 322 |
commands: |
| 323 |
description: The list of configuration mode commands to send to the device |
| 324 |
returned: always |
| 325 |
type: list |
| 326 |
sample: |
| 327 |
- vrf definition ansible |
| 328 |
- description management vrf |
| 329 |
- rd: 1:100 |
| 330 |
start: |
| 331 |
description: The time the job started |
| 332 |
returned: always |
| 333 |
type: str |
| 334 |
sample: "2016-11-16 10:38:15.126146" |
| 335 |
end: |
| 336 |
description: The time the job ended |
| 337 |
returned: always |
| 338 |
type: str |
| 339 |
sample: "2016-11-16 10:38:25.595612" |
| 340 |
delta: |
| 341 |
description: The time elapsed to perform all operations |
| 342 |
returned: always |
| 343 |
type: str |
| 344 |
sample: "0:00:10.469466" |
| 345 |
""" |
| 346 |
import re |
| 347 |
import time |
| 348 |
|
| 349 |
from functools import partial |
| 350 |
|
| 351 |
from ansible.module_utils.basic import AnsibleModule |
| 352 |
from ansible.module_utils.connection import exec_command |
| 353 |
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.config import ( |
| 354 |
NetworkConfig, |
| 355 |
) |
| 356 |
|
| 357 |
from ansible_collections.cisco.ios.plugins.module_utils.network.ios.ios import ( |
| 358 |
get_config, |
| 359 |
load_config, |
| 360 |
) |
| 361 |
|
| 362 |
|
| 363 |
def get_interface_type(interface): |
| 364 |
if interface.upper().startswith("ET"): |
| 365 |
return "ethernet" |
| 366 |
elif interface.upper().startswith("VL"): |
| 367 |
return "svi" |
| 368 |
elif interface.upper().startswith("LO"): |
| 369 |
return "loopback" |
| 370 |
elif interface.upper().startswith("MG"): |
| 371 |
return "management" |
| 372 |
elif interface.upper().startswith("MA"): |
| 373 |
return "management" |
| 374 |
elif interface.upper().startswith("PO"): |
| 375 |
return "portchannel" |
| 376 |
elif interface.upper().startswith("NV"): |
| 377 |
return "nve" |
| 378 |
else: |
| 379 |
return "unknown" |
| 380 |
|
| 381 |
|
| 382 |
def add_command_to_vrf(name, cmd, commands): |
| 383 |
if "vrf definition %s" % name not in commands: |
| 384 |
commands.extend(["vrf definition %s" % name]) |
| 385 |
commands.append(cmd) |
| 386 |
|
| 387 |
|
| 388 |
KEY_TO_COMMAND_MAP = { |
| 389 |
"auto_discovery": "mdt auto-discovery ", |
| 390 |
"default": "mdt default vxlan ", |
| 391 |
"data_mcast": "mdt data vxlan ", |
| 392 |
"data_threshold": "mdt data threshold ", |
| 393 |
"overlay": "mdt overlay ", |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
def add_mdt_commands(afi_dict, vrf_name, commands): |
| 398 |
for key, value in afi_dict["mdt"].items(): |
| 399 |
cmd = KEY_TO_COMMAND_MAP[key] |
| 400 |
|
| 401 |
if key in ["default", "data_mcast"]: |
| 402 |
cmd = cmd + value["vxlan_mcast_group"] |
| 403 |
add_command_to_vrf(vrf_name, cmd, commands) |
| 404 |
elif key == "data_threshold": |
| 405 |
cmd = cmd + str(value) |
| 406 |
add_command_to_vrf(vrf_name, cmd, commands) |
| 407 |
elif key == "auto_discovery": |
| 408 |
if value["vxlan"]["enable"]: |
| 409 |
cmd = cmd + "vxlan" |
| 410 |
if value["vxlan"].get("inter_as"): |
| 411 |
cmd = cmd + " " + "inter-as" |
| 412 |
add_command_to_vrf(vrf_name, cmd, commands) |
| 413 |
elif key == "overlay": |
| 414 |
if value["use_bgp"]["enable"]: |
| 415 |
cmd = cmd + "use-bgp" |
| 416 |
if value["use_bgp"].get("spt_only"): |
| 417 |
cmd = cmd + " " + "spt-only" |
| 418 |
add_command_to_vrf(vrf_name, cmd, commands) |
| 419 |
|
| 420 |
|
| 421 |
def map_obj_to_commands(updates, module): |
| 422 |
commands = list() |
| 423 |
for update in updates: |
| 424 |
want, have = update |
| 425 |
|
| 426 |
def needs_update(want, have, x): |
| 427 |
if isinstance(want.get(x), list) and isinstance(have.get(x), list): |
| 428 |
return ( |
| 429 |
want.get(x) |
| 430 |
and want.get(x) != have.get(x) |
| 431 |
and not all(elem in have.get(x) for elem in want.get(x)) |
| 432 |
) |
| 433 |
return want.get(x) and want.get(x) != have.get(x) |
| 434 |
|
| 435 |
if want["state"] == "absent": |
| 436 |
commands.append("no vrf definition %s" % want["name"]) |
| 437 |
continue |
| 438 |
if not have.get("state"): |
| 439 |
commands.extend(["vrf definition %s" % want["name"]]) |
| 440 |
ipv6 = ( |
| 441 |
len( |
| 442 |
[ |
| 443 |
k |
| 444 |
for k, v in module.params.items() |
| 445 |
if (k.endswith("_ipv6") or k.endswith("_both")) and v |
| 446 |
], |
| 447 |
) |
| 448 |
!= 0 |
| 449 |
) |
| 450 |
ipv4 = ( |
| 451 |
len( |
| 452 |
[ |
| 453 |
k |
| 454 |
for k, v in module.params.items() |
| 455 |
if (k.endswith("_ipv4") or k.endswith("_both")) and v |
| 456 |
], |
| 457 |
) |
| 458 |
!= 0 |
| 459 |
) |
| 460 |
if ipv4: |
| 461 |
commands.extend(["address-family ipv4", "exit"]) |
| 462 |
if ipv6: |
| 463 |
commands.extend(["address-family ipv6", "exit"]) |
| 464 |
if needs_update(want, have, "description"): |
| 465 |
cmd = "description %s" % want["description"] |
| 466 |
add_command_to_vrf(want["name"], cmd, commands) |
| 467 |
if needs_update(want, have, "rd"): |
| 468 |
cmd = "rd %s" % want["rd"] |
| 469 |
add_command_to_vrf(want["name"], cmd, commands) |
| 470 |
if needs_update(want, have, "route_import"): |
| 471 |
for route in want["route_import"]: |
| 472 |
cmd = "route-target import %s" % route |
| 473 |
add_command_to_vrf(want["name"], cmd, commands) |
| 474 |
if needs_update(want, have, "route_export"): |
| 475 |
for route in want["route_export"]: |
| 476 |
cmd = "route-target export %s" % route |
| 477 |
add_command_to_vrf(want["name"], cmd, commands) |
| 478 |
if needs_update(want, have, "route_import_ipv4"): |
| 479 |
cmd = "address-family ipv4" |
| 480 |
add_command_to_vrf(want["name"], cmd, commands) |
| 481 |
for route in want["route_import_ipv4"]: |
| 482 |
cmd = "route-target import %s" % route |
| 483 |
add_command_to_vrf(want["name"], cmd, commands) |
| 484 |
cmd = "exit-address-family" |
| 485 |
add_command_to_vrf(want["name"], cmd, commands) |
| 486 |
if needs_update(want, have, "route_export_ipv4"): |
| 487 |
cmd = "address-family ipv4" |
| 488 |
add_command_to_vrf(want["name"], cmd, commands) |
| 489 |
for route in want["route_export_ipv4"]: |
| 490 |
cmd = "route-target export %s" % route |
| 491 |
add_command_to_vrf(want["name"], cmd, commands) |
| 492 |
cmd = "exit-address-family" |
| 493 |
add_command_to_vrf(want["name"], cmd, commands) |
| 494 |
if needs_update(want, have, "route_import_ipv6"): |
| 495 |
cmd = "address-family ipv6" |
| 496 |
add_command_to_vrf(want["name"], cmd, commands) |
| 497 |
for route in want["route_import_ipv6"]: |
| 498 |
cmd = "route-target import %s" % route |
| 499 |
add_command_to_vrf(want["name"], cmd, commands) |
| 500 |
cmd = "exit-address-family" |
| 501 |
add_command_to_vrf(want["name"], cmd, commands) |
| 502 |
if needs_update(want, have, "route_export_ipv6"): |
| 503 |
cmd = "address-family ipv6" |
| 504 |
add_command_to_vrf(want["name"], cmd, commands) |
| 505 |
for route in want["route_export_ipv6"]: |
| 506 |
cmd = "route-target export %s" % route |
| 507 |
add_command_to_vrf(want["name"], cmd, commands) |
| 508 |
cmd = "exit-address-family" |
| 509 |
add_command_to_vrf(want["name"], cmd, commands) |
| 510 |
if needs_update(want, have, "address_family"): |
| 511 |
for want_mdt in want["address_family"]: |
| 512 |
afi = want_mdt["afi"] |
| 513 |
af_dict = {} |
| 514 |
data_dict = want_mdt["mdt"].pop("data", {}) |
| 515 |
if data_dict: |
| 516 |
if "vxlan_mcast_group" in data_dict: |
| 517 |
want_mdt["mdt"]["data_mcast"] = { |
| 518 |
"vxlan_mcast_group": data_dict["vxlan_mcast_group"], |
| 519 |
} |
| 520 |
if "threshold" in data_dict: |
| 521 |
want_mdt["mdt"]["data_threshold"] = data_dict["threshold"] |
| 522 |
|
| 523 |
for key_in, value_in in want_mdt["mdt"].items(): |
| 524 |
have_mdt = next( |
| 525 |
(i for i in have.get("address_family", {}) if i["afi"] == afi), |
| 526 |
{}, |
| 527 |
) |
| 528 |
|
| 529 |
if needs_update(want_mdt["mdt"], have_mdt.get("mdt", {}), key_in): |
| 530 |
af_dict.update({key_in: value_in}) |
| 531 |
if af_dict: |
| 532 |
cmd = "address-family" + " " + str(afi) |
| 533 |
add_command_to_vrf(want["name"], cmd, commands) |
| 534 |
add_mdt_commands({"mdt": af_dict}, want["name"], commands) |
| 535 |
add_command_to_vrf(want["name"], "exit-address-family", commands) |
| 536 |
|
| 537 |
if want["interfaces"] is not None: |
| 538 |
for intf in set(have.get("interfaces", [])).difference(want["interfaces"]): |
| 539 |
commands.extend(["interface %s" % intf, "no vrf forwarding %s" % want["name"]]) |
| 540 |
for intf in set(want["interfaces"]).difference(have.get("interfaces", [])): |
| 541 |
cfg = get_config(module) |
| 542 |
configobj = NetworkConfig(indent=1, contents=cfg) |
| 543 |
children = configobj["interface %s" % intf].children |
| 544 |
intf_config = "\n".join(children) |
| 545 |
commands.extend(["interface %s" % intf, "vrf forwarding %s" % want["name"]]) |
| 546 |
match = re.search("ip address .+", intf_config, re.M) |
| 547 |
if match: |
| 548 |
commands.append(match.group()) |
| 549 |
return commands |
| 550 |
|
| 551 |
|
| 552 |
def parse_description(configobj, name): |
| 553 |
cfg = configobj["vrf definition %s" % name] |
| 554 |
cfg = "\n".join(cfg.children) |
| 555 |
match = re.search("description (.+)$", cfg, re.M) |
| 556 |
if match: |
| 557 |
return match.group(1) |
| 558 |
|
| 559 |
|
| 560 |
def parse_rd(configobj, name): |
| 561 |
cfg = configobj["vrf definition %s" % name] |
| 562 |
cfg = "\n".join(cfg.children) |
| 563 |
match = re.search("rd (.+)$", cfg, re.M) |
| 564 |
if match: |
| 565 |
return match.group(1) |
| 566 |
|
| 567 |
|
| 568 |
def parse_interfaces(configobj): |
| 569 |
vrf_cfg = "vrf forwarding" |
| 570 |
interfaces = dict() |
| 571 |
for intf in set(re.findall("^interface .+", str(configobj), re.M)): |
| 572 |
for line in configobj[intf].children: |
| 573 |
if vrf_cfg in line: |
| 574 |
try: |
| 575 |
interfaces[line.split()[-1]].append(intf.split(" ")[1]) |
| 576 |
except KeyError: |
| 577 |
interfaces[line.split()[-1]] = [intf.split(" ")[1]] |
| 578 |
return interfaces |
| 579 |
|
| 580 |
|
| 581 |
def parse_import(configobj, name): |
| 582 |
cfg = configobj["vrf definition %s" % name] |
| 583 |
cfg = "\n".join(cfg.children) |
| 584 |
matches = re.findall("route-target\\s+import\\s+(.+)", cfg, re.M) |
| 585 |
return matches |
| 586 |
|
| 587 |
|
| 588 |
def parse_export(configobj, name): |
| 589 |
cfg = configobj["vrf definition %s" % name] |
| 590 |
cfg = "\n".join(cfg.children) |
| 591 |
matches = re.findall("route-target\\s+export\\s+(.+)", cfg, re.M) |
| 592 |
return matches |
| 593 |
|
| 594 |
|
| 595 |
def parse_both(configobj, name, address_family="global"): |
| 596 |
rd_pattern = re.compile("(?P<rd>.+:.+)") |
| 597 |
matches = list() |
| 598 |
export_match = None |
| 599 |
import_match = None |
| 600 |
if address_family == "global": |
| 601 |
export_match = parse_export(configobj, name) |
| 602 |
import_match = parse_import(configobj, name) |
| 603 |
elif address_family == "ipv4": |
| 604 |
export_match = parse_export_ipv4(configobj, name) |
| 605 |
import_match = parse_import_ipv4(configobj, name) |
| 606 |
elif address_family == "ipv6": |
| 607 |
export_match = parse_export_ipv6(configobj, name) |
| 608 |
import_match = parse_import_ipv6(configobj, name) |
| 609 |
if import_match and export_match: |
| 610 |
for ex in export_match: |
| 611 |
exrd = rd_pattern.search(ex) |
| 612 |
exrd = exrd.groupdict().get("rd") |
| 613 |
for im in import_match: |
| 614 |
imrd = rd_pattern.search(im) |
| 615 |
imrd = imrd.groupdict().get("rd") |
| 616 |
if exrd == imrd: |
| 617 |
matches.extend([exrd]) if exrd not in matches else None |
| 618 |
matches.extend([imrd]) if imrd not in matches else None |
| 619 |
return matches |
| 620 |
|
| 621 |
|
| 622 |
def parse_import_ipv4(configobj, name): |
| 623 |
cfg = configobj["vrf definition %s" % name] |
| 624 |
try: |
| 625 |
subcfg = cfg["address-family ipv4"] |
| 626 |
subcfg = "\n".join(subcfg.children) |
| 627 |
matches = re.findall("route-target\\s+import\\s+(.+)", subcfg, re.M) |
| 628 |
return matches |
| 629 |
except KeyError: |
| 630 |
pass |
| 631 |
|
| 632 |
|
| 633 |
def parse_export_ipv4(configobj, name): |
| 634 |
cfg = configobj["vrf definition %s" % name] |
| 635 |
try: |
| 636 |
subcfg = cfg["address-family ipv4"] |
| 637 |
subcfg = "\n".join(subcfg.children) |
| 638 |
matches = re.findall("route-target\\s+export\\s+(.+)", subcfg, re.M) |
| 639 |
return matches |
| 640 |
except KeyError: |
| 641 |
pass |
| 642 |
|
| 643 |
|
| 644 |
def parse_import_ipv6(configobj, name): |
| 645 |
cfg = configobj["vrf definition %s" % name] |
| 646 |
try: |
| 647 |
subcfg = cfg["address-family ipv6"] |
| 648 |
subcfg = "\n".join(subcfg.children) |
| 649 |
matches = re.findall("route-target\\s+import\\s+(.+)", subcfg, re.M) |
| 650 |
return matches |
| 651 |
except KeyError: |
| 652 |
pass |
| 653 |
|
| 654 |
|
| 655 |
def parse_export_ipv6(configobj, name): |
| 656 |
cfg = configobj["vrf definition %s" % name] |
| 657 |
try: |
| 658 |
subcfg = cfg["address-family ipv6"] |
| 659 |
subcfg = "\n".join(subcfg.children) |
| 660 |
matches = re.findall("route-target\\s+export\\s+(.+)", subcfg, re.M) |
| 661 |
return matches |
| 662 |
except KeyError: |
| 663 |
pass |
| 664 |
|
| 665 |
|
| 666 |
def parse_mdt(configobj, name): |
| 667 |
cfg = configobj["vrf definition %s" % name] |
| 668 |
mdt_list = [] |
| 669 |
|
| 670 |
for ip in ["ipv4", "ipv6"]: |
| 671 |
ret_dict = {} |
| 672 |
try: |
| 673 |
subcfg = cfg["address-family " + ip] |
| 674 |
subcfg = "\n".join(subcfg.children) |
| 675 |
except KeyError: |
| 676 |
subcfg = "" |
| 677 |
pass |
| 678 |
|
| 679 |
re1 = re.compile(r"^mdt +auto\-discovery +(?P<option>\S+)(\s+(?P<inter_as>inter\-as))?$") |
| 680 |
re2 = re.compile(r"^mdt +default +vxlan +(?P<mcast_group>\S+)$") |
| 681 |
re3 = re.compile(r"^mdt +data +vxlan +(?P<mcast_group>.+)$") |
| 682 |
re4 = re.compile(r"^mdt +data +threshold +(?P<threshold_value>\d+)$") |
| 683 |
re5 = re.compile(r"^mdt +overlay +(?P<use_bgp>use-bgp)(\s+(?P<spt_only>spt-only))?$") |
| 684 |
|
| 685 |
for line in subcfg.splitlines(): |
| 686 |
line = line.strip() |
| 687 |
m = re1.match(line) |
| 688 |
if m: |
| 689 |
group = m.groupdict() |
| 690 |
ret_dict.setdefault("auto_discovery", {}).setdefault( |
| 691 |
group["option"], |
| 692 |
{}, |
| 693 |
).setdefault("enable", True) |
| 694 |
if group["inter_as"]: |
| 695 |
ret_dict.setdefault("auto_discovery", {}).setdefault( |
| 696 |
group["option"], |
| 697 |
{}, |
| 698 |
).setdefault("inter_as", True) |
| 699 |
continue |
| 700 |
|
| 701 |
m = re2.match(line) |
| 702 |
if m: |
| 703 |
group = m.groupdict() |
| 704 |
ret_dict.setdefault("default", {}).setdefault( |
| 705 |
"vxlan_mcast_group", |
| 706 |
group["mcast_group"], |
| 707 |
) |
| 708 |
continue |
| 709 |
|
| 710 |
m = re3.match(line) |
| 711 |
if m: |
| 712 |
group = m.groupdict() |
| 713 |
ret_dict.setdefault("data_mcast", {}).setdefault( |
| 714 |
"vxlan_mcast_group", |
| 715 |
group["mcast_group"], |
| 716 |
) |
| 717 |
continue |
| 718 |
|
| 719 |
m = re4.match(line) |
| 720 |
if m: |
| 721 |
group = m.groupdict() |
| 722 |
ret_dict.setdefault("data_threshold", int(group["threshold_value"])) |
| 723 |
|
| 724 |
m = re5.match(line) |
| 725 |
if m: |
| 726 |
group = m.groupdict() |
| 727 |
ret_dict.setdefault("overlay", {}).setdefault( |
| 728 |
"use_bgp", |
| 729 |
{}, |
| 730 |
).setdefault("enable", True) |
| 731 |
if group["spt_only"]: |
| 732 |
ret_dict.setdefault("overlay", {}).setdefault( |
| 733 |
"use_bgp", |
| 734 |
{}, |
| 735 |
).setdefault("spt_only", True) |
| 736 |
|
| 737 |
mdt_list.append({"afi": ip, "mdt": ret_dict}) |
| 738 |
return mdt_list |
| 739 |
|
| 740 |
|
| 741 |
def map_config_to_obj(module): |
| 742 |
config = get_config(module) |
| 743 |
configobj = NetworkConfig(indent=1, contents=config) |
| 744 |
match = re.findall("^vrf definition (\\S+)", config, re.M) |
| 745 |
if not match: |
| 746 |
return list() |
| 747 |
instances = list() |
| 748 |
interfaces = parse_interfaces(configobj) |
| 749 |
for item in set(match): |
| 750 |
obj = { |
| 751 |
"name": item, |
| 752 |
"state": "present", |
| 753 |
"description": parse_description(configobj, item), |
| 754 |
"rd": parse_rd(configobj, item), |
| 755 |
"interfaces": interfaces.get(item, []), |
| 756 |
"route_import": parse_import(configobj, item), |
| 757 |
"route_export": parse_export(configobj, item), |
| 758 |
"route_both": parse_both(configobj, item), |
| 759 |
"route_import_ipv4": parse_import_ipv4(configobj, item), |
| 760 |
"route_export_ipv4": parse_export_ipv4(configobj, item), |
| 761 |
"route_both_ipv4": parse_both(configobj, item, address_family="ipv4"), |
| 762 |
"route_import_ipv6": parse_import_ipv6(configobj, item), |
| 763 |
"route_export_ipv6": parse_export_ipv6(configobj, item), |
| 764 |
"route_both_ipv6": parse_both(configobj, item, address_family="ipv6"), |
| 765 |
"address_family": parse_mdt(configobj, item), |
| 766 |
} |
| 767 |
instances.append(obj) |
| 768 |
return instances |
| 769 |
|
| 770 |
|
| 771 |
def get_param_value(key, item, module): |
| 772 |
# if key doesn't exist in the item, get it from module.params |
| 773 |
if not item.get(key): |
| 774 |
value = module.params[key] |
| 775 |
# if key does exist, do a type check on it to validate it |
| 776 |
else: |
| 777 |
value_type = module.argument_spec[key].get("type", "str") |
| 778 |
type_checker = module._CHECK_ARGUMENT_TYPES_DISPATCHER[value_type] |
| 779 |
type_checker(item[key]) |
| 780 |
value = item[key] |
| 781 |
# validate the param value (if validator func exists) |
| 782 |
validator = globals().get("validate_%s" % key) |
| 783 |
if validator: |
| 784 |
validator(value, module) |
| 785 |
return value |
| 786 |
|
| 787 |
|
| 788 |
def map_params_to_obj(module): |
| 789 |
vrfs = module.params.get("vrfs") |
| 790 |
if not vrfs: |
| 791 |
if not module.params["name"] and module.params["purge"]: |
| 792 |
return list() |
| 793 |
elif not module.params["name"]: |
| 794 |
module.fail_json(msg="name is required") |
| 795 |
collection = [{"name": module.params["name"]}] |
| 796 |
else: |
| 797 |
collection = list() |
| 798 |
for item in vrfs: |
| 799 |
if not isinstance(item, dict): |
| 800 |
collection.append({"name": item}) |
| 801 |
elif "name" not in item: |
| 802 |
module.fail_json(msg="name is required") |
| 803 |
else: |
| 804 |
collection.append(item) |
| 805 |
objects = list() |
| 806 |
for item in collection: |
| 807 |
get_value = partial(get_param_value, item=item, module=module) |
| 808 |
item["description"] = get_value("description") |
| 809 |
item["rd"] = get_value("rd") |
| 810 |
item["interfaces"] = get_value("interfaces") |
| 811 |
item["state"] = get_value("state") |
| 812 |
item["route_import"] = get_value("route_import") |
| 813 |
item["route_export"] = get_value("route_export") |
| 814 |
item["route_both"] = get_value("route_both") |
| 815 |
item["route_import_ipv4"] = get_value("route_import_ipv4") |
| 816 |
item["route_export_ipv4"] = get_value("route_export_ipv4") |
| 817 |
item["route_both_ipv4"] = get_value("route_both_ipv4") |
| 818 |
item["route_import_ipv6"] = get_value("route_import_ipv6") |
| 819 |
item["route_export_ipv6"] = get_value("route_export_ipv6") |
| 820 |
item["route_both_ipv6"] = get_value("route_both_ipv6") |
| 821 |
item["address_family"] = get_value("address_family") |
| 822 |
both_addresses_family = ["", "_ipv6", "_ipv4"] |
| 823 |
for address_family in both_addresses_family: |
| 824 |
if item["route_both%s" % address_family]: |
| 825 |
if not item["route_export%s" % address_family]: |
| 826 |
item["route_export%s" % address_family] = list() |
| 827 |
if not item["route_import%s" % address_family]: |
| 828 |
item["route_import%s" % address_family] = list() |
| 829 |
item["route_export%s" % address_family].extend( |
| 830 |
get_value("route_both%s" % address_family), |
| 831 |
) |
| 832 |
item["route_import%s" % address_family].extend( |
| 833 |
get_value("route_both%s" % address_family), |
| 834 |
) |
| 835 |
item["associated_interfaces"] = get_value("associated_interfaces") |
| 836 |
objects.append(item) |
| 837 |
return objects |
| 838 |
|
| 839 |
|
| 840 |
def update_objects(want, have): |
| 841 |
updates = list() |
| 842 |
for entry in want: |
| 843 |
item = next((i for i in have if i["name"] == entry["name"]), None) |
| 844 |
if all((item is None, entry["state"] == "present")): |
| 845 |
updates.append((entry, {})) |
| 846 |
else: |
| 847 |
for key, value in entry.items(): |
| 848 |
if value: |
| 849 |
if isinstance(value, list): |
| 850 |
try: |
| 851 |
if sorted(value) != sorted(item[key]): |
| 852 |
if (entry, item) not in updates: |
| 853 |
updates.append((entry, item)) |
| 854 |
except TypeError: |
| 855 |
if value != item[key]: |
| 856 |
updates.append((entry, item)) |
| 857 |
pass |
| 858 |
elif value != item[key]: |
| 859 |
if (entry, item) not in updates: |
| 860 |
updates.append((entry, item)) |
| 861 |
return updates |
| 862 |
|
| 863 |
|
| 864 |
def check_declarative_intent_params(want, module, result): |
| 865 |
if module.params["associated_interfaces"]: |
| 866 |
if result["changed"]: |
| 867 |
time.sleep(module.params["delay"]) |
| 868 |
name = module.params["name"] |
| 869 |
rc, out, err = exec_command(module, "show vrf | include {0}".format(name)) |
| 870 |
if rc == 0: |
| 871 |
data = out.strip().split() |
| 872 |
if not data: |
| 873 |
return |
| 874 |
vrf = data[0] |
| 875 |
interface = data[-1] |
| 876 |
for w in want: |
| 877 |
if w["name"] == vrf: |
| 878 |
if w.get("associated_interfaces") is None: |
| 879 |
continue |
| 880 |
for i in w["associated_interfaces"]: |
| 881 |
if get_interface_type(i) is not get_interface_type(interface): |
| 882 |
module.fail_json( |
| 883 |
msg="Interface %s not configured on vrf %s" % (interface, name), |
| 884 |
) |
| 885 |
|
| 886 |
|
| 887 |
def main(): |
| 888 |
"""main entry point for module execution""" |
| 889 |
address_family_spec = dict( |
| 890 |
afi=dict(type="str", choices=["ipv4", "ipv6"]), |
| 891 |
mdt=dict( |
| 892 |
type="dict", |
| 893 |
options=dict( |
| 894 |
overlay=dict( |
| 895 |
type="dict", |
| 896 |
options=dict( |
| 897 |
use_bgp=dict( |
| 898 |
type="dict", |
| 899 |
options=dict( |
| 900 |
enable=dict(type="bool"), |
| 901 |
spt_only=dict(type="bool"), |
| 902 |
), |
| 903 |
), |
| 904 |
), |
| 905 |
), |
| 906 |
auto_discovery=dict( |
| 907 |
type="dict", |
| 908 |
options=dict( |
| 909 |
vxlan=dict( |
| 910 |
type="dict", |
| 911 |
options=dict( |
| 912 |
enable=dict(type="bool"), |
| 913 |
inter_as=dict(type="bool"), |
| 914 |
), |
| 915 |
), |
| 916 |
), |
| 917 |
), |
| 918 |
default=dict( |
| 919 |
type="dict", |
| 920 |
options=dict( |
| 921 |
vxlan_mcast_group=dict(type="str"), |
| 922 |
), |
| 923 |
), |
| 924 |
data=dict( |
| 925 |
type="dict", |
| 926 |
options=dict( |
| 927 |
vxlan_mcast_group=dict(type="str"), |
| 928 |
threshold=dict(type="int"), |
| 929 |
), |
| 930 |
), |
| 931 |
), |
| 932 |
), |
| 933 |
) |
| 934 |
|
| 935 |
argument_spec = dict( |
| 936 |
vrfs=dict(type="list", elements="raw"), |
| 937 |
name=dict(type="str"), |
| 938 |
description=dict(type="str"), |
| 939 |
address_family=dict(type="list", elements="dict", options=address_family_spec), |
| 940 |
rd=dict(type="str"), |
| 941 |
route_export=dict(type="list", elements="str"), |
| 942 |
route_import=dict(type="list", elements="str"), |
| 943 |
route_both=dict(type="list", elements="str"), |
| 944 |
route_export_ipv4=dict(type="list", elements="str"), |
| 945 |
route_import_ipv4=dict(type="list", elements="str"), |
| 946 |
route_both_ipv4=dict(type="list", elements="str"), |
| 947 |
route_export_ipv6=dict(type="list", elements="str"), |
| 948 |
route_import_ipv6=dict(type="list", elements="str"), |
| 949 |
route_both_ipv6=dict(type="list", elements="str"), |
| 950 |
interfaces=dict(type="list", elements="str"), |
| 951 |
associated_interfaces=dict(type="list", elements="str"), |
| 952 |
delay=dict(default=10, type="int"), |
| 953 |
purge=dict(type="bool", default=False), |
| 954 |
state=dict(default="present", choices=["present", "absent"]), |
| 955 |
) |
| 956 |
mutually_exclusive = [("name", "vrfs")] |
| 957 |
module = AnsibleModule( |
| 958 |
argument_spec=argument_spec, |
| 959 |
mutually_exclusive=mutually_exclusive, |
| 960 |
supports_check_mode=True, |
| 961 |
) |
| 962 |
result = {"changed": False} |
| 963 |
warnings = list() |
| 964 |
result["warnings"] = warnings |
| 965 |
want = map_params_to_obj(module) |
| 966 |
have = map_config_to_obj(module) |
| 967 |
|
| 968 |
commands = map_obj_to_commands(update_objects(want, have), module) |
| 969 |
if module.params["purge"]: |
| 970 |
want_vrfs = [x["name"] for x in want] |
| 971 |
have_vrfs = [x["name"] for x in have] |
| 972 |
for item in set(have_vrfs).difference(want_vrfs): |
| 973 |
cmd = "no vrf definition %s" % item |
| 974 |
if cmd not in commands: |
| 975 |
commands.append(cmd) |
| 976 |
result["commands"] = commands |
| 977 |
if commands: |
| 978 |
if not module.check_mode: |
| 979 |
load_config(module, commands) |
| 980 |
result["changed"] = True |
| 981 |
check_declarative_intent_params(want, module, result) |
| 982 |
module.exit_json(**result) |
| 983 |
|
| 984 |
|
| 985 |
if __name__ == "__main__": |
| 986 |
main() |