| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2025, Akini Ross (@akinross) <[email protected]> |
| 5 |
|
| 6 |
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 7 |
|
| 8 |
|
| 9 |
from __future__ import absolute_import, division, print_function |
| 10 |
|
| 11 |
__metaclass__ = type |
| 12 |
|
| 13 |
ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"} |
| 14 |
|
| 15 |
DOCUMENTATION = r""" |
| 16 |
--- |
| 17 |
module: ndo_l3out_floating_svi_interface |
| 18 |
version_added: "2.12.0" |
| 19 |
short_description: Manage L3Out Floating SVI Interfaces on Cisco Nexus Dashboard Orchestrator (NDO). |
| 20 |
description: |
| 21 |
- Manage L3Out Floating SVI Interfaces on Cisco Nexus Dashboard Orchestrator (NDO). |
| 22 |
- This module is only supported on ND v3.1 (NDO v4.3) and later. |
| 23 |
author: |
| 24 |
- Akini Ross (@akinross) |
| 25 |
options: |
| 26 |
template: |
| 27 |
description: |
| 28 |
- The name of the template. |
| 29 |
- The template must be an L3Out template. |
| 30 |
- This parameter or O(template_id) is required. |
| 31 |
type: str |
| 32 |
aliases: [ l3out_template ] |
| 33 |
template_id: |
| 34 |
description: |
| 35 |
- The ID of the L3Out template. |
| 36 |
- This parameter or O(template) is required. |
| 37 |
type: str |
| 38 |
aliases: [ l3out_template_id ] |
| 39 |
l3out: |
| 40 |
description: |
| 41 |
- The name of the L3Out. |
| 42 |
- This parameter or O(l3out_uuid) is required. |
| 43 |
type: str |
| 44 |
aliases: [ l3out_name ] |
| 45 |
l3out_uuid: |
| 46 |
description: |
| 47 |
- The UUID of the L3Out. |
| 48 |
- This parameter or O(l3out) is required. |
| 49 |
type: str |
| 50 |
node_id: |
| 51 |
description: |
| 52 |
- The ID of the node (border leaf switch) where to deploy the L3Out routing protocol and node-level protocol configurations. |
| 53 |
- When specified, the node configuration is created under the L3Out template when it does not exist. |
| 54 |
- When specified, the node configuration is updated under the L3Out template when it already exists. |
| 55 |
- When specified, the node configuration is deleted under the L3Out template when there are no interfaces referencing it. |
| 56 |
type: str |
| 57 |
aliases: [ node, border_leaf ] |
| 58 |
node_router_id: |
| 59 |
description: |
| 60 |
- The router ID of the node. |
| 61 |
- This parameter is required when the node configuration does not exist under the L3Out template. |
| 62 |
type: str |
| 63 |
aliases: [ router_id ] |
| 64 |
node_group_policy: |
| 65 |
description: |
| 66 |
- The name of the node group policy. |
| 67 |
type: str |
| 68 |
use_router_id_as_loopback: |
| 69 |
description: |
| 70 |
- Whether to use the router ID as the loopback address of the node. |
| 71 |
type: bool |
| 72 |
node_loopback_ip: |
| 73 |
description: |
| 74 |
- The loopback IP address of the node. |
| 75 |
type: str |
| 76 |
aliases: [ loopback_ip ] |
| 77 |
interface_group_policy: |
| 78 |
description: |
| 79 |
- The name of the interface group policy. |
| 80 |
type: str |
| 81 |
ipv4_address: |
| 82 |
description: |
| 83 |
- The IPv4 address of the interface. |
| 84 |
type: str |
| 85 |
ipv6_address: |
| 86 |
description: |
| 87 |
- The IPv6 address of the interface. |
| 88 |
type: str |
| 89 |
ipv6_link_local_address: |
| 90 |
description: |
| 91 |
- The IPv6 link-local address of the interface. |
| 92 |
type: str |
| 93 |
ipv6_dad: |
| 94 |
description: |
| 95 |
- Whether to enable IPv6 Duplicate Address Detection (DAD). |
| 96 |
- If this parameter is unspecified, NDO defaults to O(ipv6_dad=enabled). |
| 97 |
type: str |
| 98 |
choices: [ enabled, disabled ] |
| 99 |
mac: |
| 100 |
description: |
| 101 |
- The MAC address of the interface. |
| 102 |
type: str |
| 103 |
mtu: |
| 104 |
description: |
| 105 |
- The Maximum Transmission Unit (MTU) of the interface. |
| 106 |
- Use O(mtu=inherit) to inherit the value configured under the fabric L2 MTU settings. |
| 107 |
- The value must be 1, or in the range 576 - 9216 or O(mtu=inherit). |
| 108 |
type: str |
| 109 |
target_dscp: |
| 110 |
description: |
| 111 |
- The target Differentiated Services Code Point (DSCP) of the interface. |
| 112 |
- If this parameter is unspecified, NDO defaults to O(target_dscp=unspecified). |
| 113 |
type: str |
| 114 |
choices: |
| 115 |
- af11 |
| 116 |
- af12 |
| 117 |
- af13 |
| 118 |
- af21 |
| 119 |
- af22 |
| 120 |
- af23 |
| 121 |
- af31 |
| 122 |
- af32 |
| 123 |
- af33 |
| 124 |
- af41 |
| 125 |
- af42 |
| 126 |
- af43 |
| 127 |
- cs0 |
| 128 |
- cs1 |
| 129 |
- cs2 |
| 130 |
- cs3 |
| 131 |
- cs4 |
| 132 |
- cs5 |
| 133 |
- cs6 |
| 134 |
- cs7 |
| 135 |
- expedited_forwarding |
| 136 |
- unspecified |
| 137 |
- voice_admit |
| 138 |
encapsulation_scope: |
| 139 |
description: |
| 140 |
- The encapsulation scope of the interface. |
| 141 |
type: str |
| 142 |
aliases: [ encap_scope ] |
| 143 |
choices: [ local, vrf ] |
| 144 |
encapsulation_type: |
| 145 |
description: |
| 146 |
- The encapsulation type of the interface. |
| 147 |
type: str |
| 148 |
aliases: [ encap_type ] |
| 149 |
choices: [ vlan, vxlan ] |
| 150 |
encapsulation_value: |
| 151 |
description: |
| 152 |
- The encapsulation value of the interface. |
| 153 |
- The option O(encapsulation_type=vlan), specifies VLAN ID which must be in the range 1 - 4094. |
| 154 |
- The option O(encapsulation_type=vxlan), specifies VXLAN Network Identifier (VNI) which must be in the range 5000 - 16777215. |
| 155 |
type: int |
| 156 |
aliases: [ encap, encapsulation, encapsulation_id ] |
| 157 |
encapsulation_mode: |
| 158 |
description: |
| 159 |
- The encapsulation mode of the interface. |
| 160 |
type: str |
| 161 |
aliases: [ encap_mode ] |
| 162 |
choices: [ access, trunk, trunk_native ] |
| 163 |
auto_state: |
| 164 |
description: |
| 165 |
- Whether to enable auto state on the interface. |
| 166 |
- Controls whether the SVI comes up or down based on the state of its associated Layer 2 ports. |
| 167 |
type: str |
| 168 |
choices: [ enabled, disabled ] |
| 169 |
state: |
| 170 |
description: |
| 171 |
- Determines the desired state of the resource. |
| 172 |
- Use C(absent) to remove the resource. |
| 173 |
- Use C(query) to list the resource. |
| 174 |
- Use C(present) to create or update the resource. |
| 175 |
type: str |
| 176 |
choices: [ absent, query, present ] |
| 177 |
default: query |
| 178 |
notes: |
| 179 |
- The O(template) or O(template_id) must exist before using this module in your playbook. |
| 180 |
The M(cisco.mso.ndo_template) module can be used for this. |
| 181 |
- The O(l3out) or O(l3out_uuid) must exist before using this module in your playbook. |
| 182 |
The M(cisco.mso.ndo_l3out_template) module can be used for this. |
| 183 |
- The O(node_group_policy) must exist before using this module in your playbook. |
| 184 |
The M(cisco.mso.ndo_l3out_node_group_policy) module can be used for this. |
| 185 |
- The O(interface_group_policy) must exist before using this module in your playbook. |
| 186 |
The M(cisco.mso.ndo_l3out_interface_group_policy) module can be used for this. |
| 187 |
seealso: |
| 188 |
- module: cisco.mso.ndo_template |
| 189 |
- module: cisco.mso.ndo_l3out_template |
| 190 |
- module: cisco.mso.ndo_l3out_node_group_policy |
| 191 |
- module: cisco.mso.ndo_l3out_interface_group_policy |
| 192 |
extends_documentation_fragment: cisco.mso.modules |
| 193 |
""" |
| 194 |
|
| 195 |
EXAMPLES = r""" |
| 196 |
- name: Create a L3Out Floating SVI Interface with L3Out Node configuration |
| 197 |
cisco.mso.ndo_l3out_floating_svi_interface: |
| 198 |
host: mso_host |
| 199 |
username: admin |
| 200 |
password: SomeSecretPassword |
| 201 |
template: l3out_template |
| 202 |
l3out: l3out_name |
| 203 |
node_id: 101 |
| 204 |
node_router_id: 1.1.1.1 |
| 205 |
ipv4_address: 10.0.0.2/24 |
| 206 |
ipv6_address: 2::101/16 |
| 207 |
ipv6_link_local_address: FE80::10 |
| 208 |
ipv6_dad: disabled |
| 209 |
encapsulation_type: vlan |
| 210 |
encapsulation_value: 1200 |
| 211 |
mac: 00:22:BD:F8:19:FE |
| 212 |
mtu: 1000 |
| 213 |
interface_group_policy: interface_group_policy_1 |
| 214 |
node_group_policy: node_group_policy_1 |
| 215 |
target_dscp: af11 |
| 216 |
encapsulation_scope: vrf |
| 217 |
encapsulation_mode: trunk |
| 218 |
auto_state: enabled |
| 219 |
state: present |
| 220 |
|
| 221 |
- name: Update a L3Out Floating SVI Interface and L3Out Node configuration |
| 222 |
cisco.mso.ndo_l3out_floating_svi_interface: |
| 223 |
host: mso_host |
| 224 |
username: admin |
| 225 |
password: SomeSecretPassword |
| 226 |
template_id: "{{ l3out_template.current.templateId }}" |
| 227 |
l3out_uuid: "{{ l3out.current.uuid }}" |
| 228 |
node_id: 101 |
| 229 |
node_router_id: 1.1.1.1 |
| 230 |
encapsulation_type: vlan |
| 231 |
encapsulation_value: 1200 |
| 232 |
ipv4_address: 10.0.1.1/24 |
| 233 |
ipv6_address: 1::102/16 |
| 234 |
ipv6_dad: disabled |
| 235 |
ipv6_link_local_address: FE80::12 |
| 236 |
mac: 00:22:BD:F8:19:CC |
| 237 |
mtu: 1800 |
| 238 |
interface_group_policy: interface_group_policy_2 |
| 239 |
node_group_policy: node_group_policy_2 |
| 240 |
target_dscp: af41 |
| 241 |
encapsulation_scope: local |
| 242 |
encapsulation_mode: access |
| 243 |
auto_state: enabled |
| 244 |
state: present |
| 245 |
|
| 246 |
- name: Query an existing L3Out Floating SVI Interface with L3Out Node configuration |
| 247 |
cisco.mso.ndo_l3out_floating_svi_interface: |
| 248 |
host: mso_host |
| 249 |
username: admin |
| 250 |
password: SomeSecretPassword |
| 251 |
template: l3out_template |
| 252 |
l3out: l3out_name |
| 253 |
node_id: 101 |
| 254 |
encapsulation_type: vlan |
| 255 |
encapsulation_value: 1200 |
| 256 |
state: query |
| 257 |
register: query_one |
| 258 |
|
| 259 |
- name: Query all existing L3Out Floating SVI Interfaces of a L3Out with L3Out Node configuration |
| 260 |
cisco.mso.ndo_l3out_floating_svi_interface: |
| 261 |
host: mso_host |
| 262 |
username: admin |
| 263 |
password: SomeSecretPassword |
| 264 |
template: l3out_template |
| 265 |
l3out: l3out_name |
| 266 |
state: query |
| 267 |
register: query_all |
| 268 |
|
| 269 |
- name: Delete an existing L3Out Floating SVI Interface |
| 270 |
cisco.mso.ndo_l3out_floating_svi_interface: |
| 271 |
host: mso_host |
| 272 |
username: admin |
| 273 |
password: SomeSecretPassword |
| 274 |
template: l3out_template |
| 275 |
l3out: l3out_name |
| 276 |
node_id: 101 |
| 277 |
encapsulation_type: vlan |
| 278 |
encapsulation_value: 1200 |
| 279 |
state: absent |
| 280 |
""" |
| 281 |
|
| 282 |
RETURN = r""" |
| 283 |
""" |
| 284 |
|
| 285 |
from ansible.module_utils.basic import AnsibleModule |
| 286 |
from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec |
| 287 |
from ansible_collections.cisco.mso.plugins.module_utils.templates import MSOTemplates |
| 288 |
from ansible_collections.cisco.mso.plugins.module_utils.constants import TARGET_DSCP_MAP |
| 289 |
from ansible_collections.cisco.mso.plugins.module_utils.utils import append_update_ops_data, delete_none_values |
| 290 |
from ansible_collections.cisco.mso.plugins.module_utils.l3out_node import L3OutNode |
| 291 |
import copy |
| 292 |
|
| 293 |
|
| 294 |
def main(): |
| 295 |
argument_spec = mso_argument_spec() |
| 296 |
argument_spec.update( |
| 297 |
template=dict(type="str", aliases=["l3out_template"]), |
| 298 |
template_id=dict(type="str", aliases=["l3out_template_id"]), |
| 299 |
l3out=dict(type="str", aliases=["l3out_name"]), |
| 300 |
l3out_uuid=dict(type="str"), |
| 301 |
node_id=dict(type="str", aliases=["node", "border_leaf"]), |
| 302 |
node_group_policy=dict(type="str"), |
| 303 |
node_router_id=dict(type="str", aliases=["router_id"]), |
| 304 |
use_router_id_as_loopback=dict(type="bool"), |
| 305 |
node_loopback_ip=dict(type="str", aliases=["loopback_ip"]), |
| 306 |
interface_group_policy=dict(type="str"), |
| 307 |
ipv4_address=dict(type="str"), |
| 308 |
ipv6_address=dict(type="str"), |
| 309 |
ipv6_link_local_address=dict(type="str"), |
| 310 |
ipv6_dad=dict(type="str", choices=["enabled", "disabled"]), |
| 311 |
mac=dict(type="str"), |
| 312 |
mtu=dict(type="str"), |
| 313 |
target_dscp=dict(type="str", choices=list(TARGET_DSCP_MAP)), |
| 314 |
encapsulation_scope=dict(type="str", choices=["local", "vrf"], aliases=["encap_scope"]), |
| 315 |
encapsulation_type=dict(type="str", choices=["vlan", "vxlan"], aliases=["encap_type"]), |
| 316 |
encapsulation_value=dict(type="int", aliases=["encap", "encapsulation", "encapsulation_id"]), |
| 317 |
encapsulation_mode=dict(type="str", choices=["access", "trunk", "trunk_native"], aliases=["encap_mode"]), |
| 318 |
auto_state=dict(type="str", choices=["enabled", "disabled"]), |
| 319 |
state=dict(type="str", default="query", choices=["absent", "query", "present"]), |
| 320 |
) |
| 321 |
|
| 322 |
module = AnsibleModule( |
| 323 |
argument_spec=argument_spec, |
| 324 |
supports_check_mode=True, |
| 325 |
required_if=[ |
| 326 |
["state", "present", ["node_id"]], |
| 327 |
["state", "absent", ["node_id"]], |
| 328 |
], |
| 329 |
required_by={"node_id": ("encapsulation_type", "encapsulation_value")}, |
| 330 |
required_one_of=[ |
| 331 |
["template", "template_id"], |
| 332 |
["l3out", "l3out_uuid"], |
| 333 |
], |
| 334 |
mutually_exclusive=[ |
| 335 |
["template", "template_id"], |
| 336 |
["l3out", "l3out_uuid"], |
| 337 |
], |
| 338 |
) |
| 339 |
|
| 340 |
mso = MSOModule(module) |
| 341 |
mso_templates = MSOTemplates(mso) |
| 342 |
|
| 343 |
template_name = mso.params.get("template") |
| 344 |
template_id = mso.params.get("template_id") |
| 345 |
l3out = mso.params.get("l3out") |
| 346 |
l3out_uuid = mso.params.get("l3out_uuid") |
| 347 |
node_id = mso.params.get("node_id") |
| 348 |
node_router_id = mso.params.get("node_router_id") |
| 349 |
node_group_policy = mso.params.get("node_group_policy") |
| 350 |
use_router_id_as_loopback = mso.params.get("use_router_id_as_loopback") |
| 351 |
node_loopback_ip = mso.params.get("node_loopback_ip") |
| 352 |
interface_group_policy = mso.params.get("interface_group_policy") |
| 353 |
ipv4_address = mso.params.get("ipv4_address") |
| 354 |
ipv6_address = mso.params.get("ipv6_address") |
| 355 |
ipv6_link_local_address = mso.params.get("ipv6_link_local_address") |
| 356 |
ipv6_dad = mso.params.get("ipv6_dad") |
| 357 |
mac = mso.params.get("mac") |
| 358 |
mtu = mso.params.get("mtu") |
| 359 |
target_dscp = mso.params.get("target_dscp") |
| 360 |
encapsulation_scope = mso.params.get("encapsulation_scope") |
| 361 |
encapsulation_type = mso.params.get("encapsulation_type") |
| 362 |
encapsulation_value = mso.params.get("encapsulation_value") |
| 363 |
encap = None |
| 364 |
if encapsulation_type and encapsulation_value: |
| 365 |
encap = {"encapType": encapsulation_type, "value": encapsulation_value} |
| 366 |
encapsulation_mode = mso.params.get("encapsulation_mode") |
| 367 |
if encapsulation_mode == "trunk_native": |
| 368 |
encapsulation_mode = "access8021p" |
| 369 |
auto_state = mso.params.get("auto_state") |
| 370 |
state = mso.params.get("state") |
| 371 |
|
| 372 |
mso_template = mso_templates.get_template("l3out", template_name, template_id) |
| 373 |
mso_template.validate_template("l3out") |
| 374 |
l3out_object = mso_template.get_l3out_object(l3out_uuid, l3out, True) |
| 375 |
|
| 376 |
pod_id = None |
| 377 |
if node_id: |
| 378 |
pod_id = mso.get_site_interface_details( |
| 379 |
site_id=mso_template.template.get("l3outTemplate", {}).get("siteId"), |
| 380 |
node=node_id, |
| 381 |
) |
| 382 |
|
| 383 |
match = mso_template.get_l3out_floating_svi_interface(l3out_object.details, pod_id, node_id, encap) |
| 384 |
if node_id and encap and match: |
| 385 |
set_floating_svi_interface_details(mso_template, match.details, l3out_object) |
| 386 |
mso.existing = mso.previous = copy.deepcopy(match.details) # Query a specific object |
| 387 |
elif match: |
| 388 |
mso.existing = [set_floating_svi_interface_details(mso_template, obj, l3out_object) for obj in match] |
| 389 |
|
| 390 |
l3out_interface_path = "/l3outTemplate/l3outs/{0}/floatingSviInterfaces/{1}".format(l3out_object.index, match.index if match else "-") |
| 391 |
|
| 392 |
ops = [] |
| 393 |
|
| 394 |
if state != "query": |
| 395 |
l3out_node = L3OutNode(mso.params, mso_template, l3out_object, pod_id, node_id) |
| 396 |
|
| 397 |
if state == "present": |
| 398 |
mso_values = { |
| 399 |
"pathType": "anchorNode", |
| 400 |
"group": interface_group_policy, |
| 401 |
"mac": mac, |
| 402 |
"mtu": mtu, |
| 403 |
"targetDscp": target_dscp, |
| 404 |
"encap": encap, |
| 405 |
"nodeID": node_id, |
| 406 |
"podID": pod_id, |
| 407 |
} |
| 408 |
|
| 409 |
if match: |
| 410 |
mso_values[("addresses", "primaryV4")] = ipv4_address |
| 411 |
mso_values[("addresses", "primaryV6")] = ipv6_address |
| 412 |
mso_values[("addresses", "linkLocalV6")] = ipv6_link_local_address |
| 413 |
mso_values[("addresses", "ipV6DAD")] = ipv6_dad |
| 414 |
mso_values[("svi", "encapScope")] = encapsulation_scope |
| 415 |
mso_values[("svi", "mode")] = encapsulation_mode |
| 416 |
mso_values[("svi", "autostate")] = auto_state |
| 417 |
|
| 418 |
mso_values = delete_none_values(mso_values) |
| 419 |
append_update_ops_data(ops, match.details, l3out_interface_path, mso_values) |
| 420 |
mso.sanitize(match.details, collate=True) |
| 421 |
|
| 422 |
else: |
| 423 |
mso_values["addresses"] = { |
| 424 |
"primaryV4": ipv4_address, |
| 425 |
"primaryV6": ipv6_address, |
| 426 |
"linkLocalV6": ipv6_link_local_address, |
| 427 |
"ipV6DAD": ipv6_dad, |
| 428 |
} |
| 429 |
|
| 430 |
mso_values["svi"] = { |
| 431 |
"encapScope": encapsulation_scope, |
| 432 |
"mode": encapsulation_mode, |
| 433 |
"autostate": auto_state, |
| 434 |
} |
| 435 |
|
| 436 |
mso_values = delete_none_values(mso_values) |
| 437 |
mso.sanitize(mso_values) |
| 438 |
ops.append(dict(op="add", path=l3out_interface_path, value=mso.sent)) |
| 439 |
|
| 440 |
# update mso.proposed with interface details that are not included in the interface payload and node details |
| 441 |
mso.proposed["node"] = l3out_node.construct_node_payload() |
| 442 |
set_floating_svi_interface_details(mso_template, mso.proposed, l3out_object) |
| 443 |
|
| 444 |
l3out_node.update_ops(ops) |
| 445 |
|
| 446 |
elif state == "absent": |
| 447 |
if match: |
| 448 |
ops.append(dict(op="remove", path=l3out_interface_path)) |
| 449 |
|
| 450 |
if not mso.module.check_mode and ops: |
| 451 |
ignore_errors = ["node {0}-{1} doesn't have an interface configured".format(pod_id, node_id)] |
| 452 |
remove_ops = {ignore_errors[0]: l3out_node.get_node_remove_op()} |
| 453 |
response = mso.l3out_interface_request(mso_template, ops, ignore_errors, state, remove_ops) |
| 454 |
l3out_object = mso_template.get_l3out_object(l3out_uuid, l3out, True, search_object=response) |
| 455 |
match = mso_template.get_l3out_floating_svi_interface(l3out_object.details, pod_id, node_id, encap) |
| 456 |
if match: |
| 457 |
set_floating_svi_interface_details(mso_template, match.details, l3out_object) |
| 458 |
mso.existing = match.details # When the state is present |
| 459 |
else: |
| 460 |
mso.existing = {} # When the state is absent |
| 461 |
elif mso.module.check_mode and state != "query": # When the state is present/absent with check mode |
| 462 |
mso.existing = mso.proposed if state == "present" else {} |
| 463 |
mso.exit_json() |
| 464 |
|
| 465 |
|
| 466 |
def set_floating_svi_interface_details(mso_template, floating_svi_interface, l3out_object): |
| 467 |
mso_template.update_config_with_port_channel_references(floating_svi_interface) |
| 468 |
mso_template.update_config_with_node_references(floating_svi_interface, l3out_object) |
| 469 |
return floating_svi_interface |
| 470 |
|
| 471 |
|
| 472 |
if __name__ == "__main__": |
| 473 |
main() |