Debian Codegraph @

ansible_collections/cisco/mso/plugins/modules/ndo_l3out_routed_sub_interface.py

raw
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_routed_sub_interface
18 version_added: "2.11.0"
19 short_description: Manage L3Out Routed Sub-Interfaces on Cisco Nexus Dashboard Orchestrator (NDO).
20 description:
21 - Manage L3Out Routed Sub-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 - This parameter is required when O(path) is specified.
54 - When specified, the node configuration is created under the L3Out template when it does not exist.
55 - When specified, the node configuration is updated under the L3Out template when it already exists.
56 - When specified, the node configuration is deleted under the L3Out template when there are no interfaces referencing it.
57 type: str
58 aliases: [ node, border_leaf ]
59 node_router_id:
60 description:
61 - The router ID of the node.
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 path:
78 description:
79 - The path of the interface.
80 - The path must be an existing physical port (eg. eth1/1).
81 type: str
82 aliases: [ interface ]
83 port_channel:
84 description:
85 - The port channel details.
86 type: dict
87 aliases: [ pc ]
88 suboptions:
89 uuid:
90 description:
91 - The UUID of the port channel.
92 - This parameter or O(port_channel.reference) is required.
93 type: str
94 reference:
95 description:
96 - The reference details of the port channel.
97 - This parameter or O(port_channel.uuid) is required.
98 type: dict
99 aliases: [ ref ]
100 suboptions:
101 name:
102 description:
103 - The name of the port channel.
104 type: str
105 required: true
106 template:
107 description:
108 - The name of the template that contains the port channel.
109 - This parameter or O(port_channel.reference.template_id) is required.
110 type: str
111 template_id:
112 description:
113 - The ID of the template that contains the port channel.
114 - This parameter or O(port_channel.reference.template) is required.
115 type: str
116 interface_group_policy:
117 description:
118 - The name of the interface group policy.
119 type: str
120 ipv4_address:
121 description:
122 - The IPv4 address of the interface.
123 type: str
124 ipv6_address:
125 description:
126 - The IPv6 address of the interface.
127 type: str
128 ipv6_link_local_address:
129 description:
130 - The IPv6 link-local address of the interface.
131 type: str
132 ipv6_dad:
133 description:
134 - Whether to enable IPv6 Duplicate Address Detection (DAD).
135 - If this parameter is unspecified during create, NDO defaults to O(ipv6_dad=enabled).
136 type: str
137 choices: [ enabled, disabled ]
138 mac:
139 description:
140 - The MAC address of the interface.
141 type: str
142 mtu:
143 description:
144 - The Maximum Transmission Unit (MTU) of the interface.
145 - Use O(mtu=inherit) to inherit the value configured under the fabric L2 MTU settings.
146 - The value must be 1, or in the range 576 - 9216 or O(mtu=inherit).
147 type: str
148 target_dscp:
149 description:
150 - The target Differentiated Services Code Point (DSCP) of the interface.
151 - If this parameter is unspecified during create, NDO defaults to O(target_dscp=unspecified).
152 type: str
153 choices:
154 - af11
155 - af12
156 - af13
157 - af21
158 - af22
159 - af23
160 - af31
161 - af32
162 - af33
163 - af41
164 - af42
165 - af43
166 - cs0
167 - cs1
168 - cs2
169 - cs3
170 - cs4
171 - cs5
172 - cs6
173 - cs7
174 - expedited_forwarding
175 - unspecified
176 - voice_admit
177 encapsulation_type:
178 description:
179 - The encapsulation type of the interface.
180 type: str
181 aliases: [ encap_type ]
182 choices: [ vlan, vxlan ]
183 encapsulation_value:
184 description:
185 - The encapsulation value of the interface.
186 - The option O(encapsulation_type=vlan), specifies VLAN ID which must be in the range 1 - 4094.
187 - The option O(encapsulation_type=vxlan), specifies VXLAN Network Identifier (VNI) which must be in the range 5000 - 16777215.
188 type: int
189 aliases: [ encap, encapsulation, encapsulation_id ]
190 state:
191 description:
192 - Determines the desired state of the resource.
193 - Use C(absent) to remove the resource.
194 - Use C(query) to list the resource.
195 - Use C(present) to create or update the resource.
196 type: str
197 choices: [ absent, query, present ]
198 default: query
199 notes:
200 - The O(template) or O(template_id) must exist before using this module in your playbook.
201 The M(cisco.mso.ndo_template) module can be used for this.
202 - The O(l3out) or O(l3out_uuid) must exist before using this module in your playbook.
203 The M(cisco.mso.ndo_l3out_template) module can be used for this.
204 - The O(node_group_policy) must exist before using this module in your playbook.
205 The M(cisco.mso.ndo_l3out_node_group_policy) module can be used for this.
206 - The O(interface_group_policy) must exist before using this module in your playbook.
207 The M(cisco.mso.ndo_l3out_interface_group_policy) module can be used for this.
208 - The O(port_channel) must exist before using this module in your playbook.
209 The M(cisco.mso.ndo_port_channel_interface) module can be used for this.
210 - The O(ptp.user_profile) must exist before using this module in your playbook.
211 The M(cisco.mso.ndo_ptp_policy_profiles) module can be used for this.
212 seealso:
213 - module: cisco.mso.ndo_template
214 - module: cisco.mso.ndo_l3out_template
215 - module: cisco.mso.ndo_l3out_node_group_policy
216 - module: cisco.mso.ndo_l3out_interface_group_policy
217 - module: cisco.mso.ndo_port_channel_interface
218 - module: cisco.mso.ndo_ptp_policy_profiles
219 extends_documentation_fragment:
220 - cisco.mso.modules
221 - cisco.mso.l3out_interface_ptp
222 """
223
224 EXAMPLES = r"""
225 - name: Create a L3Out Routed Sub-Interface of type port
226 cisco.mso.ndo_l3out_routed_sub_interface:
227 host: mso_host
228 username: admin
229 password: SomeSecretPassword
230 template: l3out_template
231 l3out: l3out_name
232 ipv4_address: 10.0.0.1/24
233 ipv6_address: 2::101/16
234 ipv6_link_local_address: FE80::10
235 ipv6_dad: enabled
236 mac: 00:22:BD:F8:19:FF
237 mtu: inherit
238 node_id: 101
239 path: eth1/1
240 encapsulation_type: vlan
241 encapsulation_value: 100
242 interface_group_policy: interface_group_policy_name
243 node_router_id: 1.1.1.1
244 node_group_policy: node_group_policy_name
245 use_router_id_as_loopback: true
246 ptp:
247 user_profile:
248 uuid: '{{ ptp_profile.current.uuid }}'
249 state: present
250
251 - name: Create a L3Out Routed Sub-Interface of type port_channel with reference
252 cisco.mso.ndo_l3out_routed_sub_interface:
253 host: mso_host
254 username: admin
255 password: SomeSecretPassword
256 template: l3out_template
257 l3out: l3out_name
258 ipv4_address: 10.1.0.1/24
259 ipv6_address: 1::112/16
260 ipv6_link_local_address: FE80::20
261 ipv6_dad: disabled
262 mac: 00:22:BD:F8:19:EE
263 mtu: 2000
264 port_channel:
265 reference:
266 name: port_channel_interface_name
267 template: fabric_resource_template_name
268 encapsulation_type: vlan
269 encapsulation_value: 100
270 interface_group_policy: interface_group_policy_name
271 node_router_id: 2.2.2.2
272 node_group_policy: node_group_policy_name
273 use_router_id_as_loopback: false
274 node_loopback_ip: 10.0.0.1
275 ptp:
276 mode: unicast_master
277 user_profile:
278 reference:
279 name: ptp_profile_name
280 template: fabric_policy_template_name
281 source_address: 12.0.0.1
282 unicast_destinations:
283 - 12.0.0.2
284 - 12.0.0.3
285 state: present
286
287 - name: Update a L3Out Routed Sub-Interface of type port_channel with uuid
288 cisco.mso.ndo_l3out_routed_sub_interface:
289 host: mso_host
290 username: admin
291 password: SomeSecretPassword
292 template_id: "{{ l3out_template.current.templateId }}"
293 l3out_uuid: "{{ l3out.current.uuid }}"
294 ipv4_address: 10.1.0.1/24
295 ipv6_address: 1::112/16
296 ipv6_link_local_address: FE80::20
297 ipv6_dad: disabled
298 mac: 00:22:BD:F8:19:EE
299 mtu: 2000
300 port_channel:
301 uuid: "{{ port_channel_interface.current.uuid }}"
302 encapsulation_type: vlan
303 encapsulation_value: 100
304 interface_group_policy: interface_group_policy_name
305 node_router_id: 2.2.2.2
306 node_group_policy: node_group_policy_name
307 use_router_id_as_loopback: false
308 node_loopback_ip: 10.0.0.1
309 state: present
310
311 - name: Remove PTP configuration from a L3Out Routed Sub-Interface of type port
312 cisco.mso.ndo_l3out_routed_sub_interface:
313 host: mso_host
314 username: admin
315 password: SomeSecretPassword
316 template: l3out_template
317 l3out: l3out_name
318 node_id: 101
319 path: eth1/1
320 encapsulation_type: vlan
321 encapsulation_value: 100
322 ptp: {}
323 state: present
324
325 - name: Query an existing L3Out Routed Sub-Interface of type port
326 cisco.mso.ndo_l3out_routed_sub_interface:
327 host: mso_host
328 username: admin
329 password: SomeSecretPassword
330 template: l3out_template
331 l3out: l3out_name
332 node_id: 101
333 path: eth1/1
334 encapsulation_type: vlan
335 encapsulation_value: 100
336 state: query
337 register: query_with_name
338
339 - name: Query an existing L3Out Routed Sub-Interface of type port_channel
340 cisco.mso.ndo_l3out_routed_sub_interface:
341 host: mso_host
342 username: admin
343 password: SomeSecretPassword
344 template: l3out_template
345 l3out: l3out_name
346 port_channel:
347 reference:
348 name: port_channel_interface_name
349 template: fabric_resource_template_name
350 encapsulation_type: vlan
351 encapsulation_value: 100
352 state: query
353 register: query_with_name
354
355 - name: Query all existing Routed Sub-Interfaces of a L3Out
356 cisco.mso.ndo_l3out_routed_sub_interface:
357 host: mso_host
358 username: admin
359 password: SomeSecretPassword
360 template: l3out_template
361 l3out: l3out_name
362 state: query
363
364 - name: Delete an existing L3Out Routed Sub-Interface
365 cisco.mso.ndo_l3out_routed_sub_interface:
366 host: mso_host
367 username: admin
368 password: SomeSecretPassword
369 template: l3out_template
370 l3out: l3out_name
371 node_id: 101
372 path: eth1/1
373 encapsulation_type: vlan
374 encapsulation_value: 100
375 state: absent
376 """
377
378 RETURN = r"""
379 """
380
381 from ansible.module_utils.basic import AnsibleModule
382 from ansible_collections.cisco.mso.plugins.module_utils.mso import MSOModule, mso_argument_spec, ndo_l3out_port_channel_spec, ndo_l3out_ptp_spec
383 from ansible_collections.cisco.mso.plugins.module_utils.templates import MSOTemplates
384 from ansible_collections.cisco.mso.plugins.module_utils.constants import TARGET_DSCP_MAP, PTP_MODES
385 from ansible_collections.cisco.mso.plugins.module_utils.utils import append_update_ops_data, delete_none_values, check_if_all_elements_are_none
386 from ansible_collections.cisco.mso.plugins.module_utils.l3out_node import L3OutNode
387 import copy
388
389
390 def main():
391 argument_spec = mso_argument_spec()
392 argument_spec.update(
393 template=dict(type="str", aliases=["l3out_template"]),
394 template_id=dict(type="str", aliases=["l3out_template_id"]),
395 l3out=dict(type="str", aliases=["l3out_name"]),
396 l3out_uuid=dict(type="str"),
397 node_id=dict(type="str", aliases=["node", "border_leaf"]),
398 node_group_policy=dict(type="str"),
399 node_router_id=dict(type="str", aliases=["router_id"]),
400 use_router_id_as_loopback=dict(type="bool"),
401 node_loopback_ip=dict(type="str", aliases=["loopback_ip"]),
402 path=dict(type="str", aliases=["interface"]),
403 port_channel=ndo_l3out_port_channel_spec(micro_bfd=False),
404 interface_group_policy=dict(type="str"),
405 ipv4_address=dict(type="str"),
406 ipv6_address=dict(type="str"),
407 ipv6_link_local_address=dict(type="str"),
408 ipv6_dad=dict(type="str", choices=["enabled", "disabled"]),
409 mac=dict(type="str"),
410 mtu=dict(type="str"),
411 target_dscp=dict(type="str", choices=list(TARGET_DSCP_MAP)),
412 encapsulation_type=dict(type="str", choices=["vlan", "vxlan"], aliases=["encap_type"]),
413 encapsulation_value=dict(type="int", aliases=["encap", "encapsulation", "encapsulation_id"]),
414 ptp=ndo_l3out_ptp_spec(),
415 state=dict(type="str", default="query", choices=["absent", "query", "present"]),
416 )
417
418 module = AnsibleModule(
419 argument_spec=argument_spec,
420 supports_check_mode=True,
421 required_if=[
422 ["state", "present", ["path", "port_channel"], True],
423 ["state", "absent", ["path", "port_channel"], True],
424 ],
425 required_by={
426 "path": ("node_id", "encapsulation_type", "encapsulation_value"),
427 "port_channel": ("encapsulation_type", "encapsulation_value"),
428 },
429 required_one_of=[
430 ["template", "template_id"],
431 ["l3out", "l3out_uuid"],
432 ],
433 mutually_exclusive=[
434 ["template", "template_id"],
435 ["l3out", "l3out_uuid"],
436 ["port_channel", "path"],
437 ["port_channel", "node_id"],
438 ],
439 )
440
441 mso = MSOModule(module)
442 mso_templates = MSOTemplates(mso)
443
444 template_name = mso.params.get("template")
445 template_id = mso.params.get("template_id")
446 l3out = mso.params.get("l3out")
447 l3out_uuid = mso.params.get("l3out_uuid")
448 node_id = mso.params.get("node_id")
449 node_router_id = mso.params.get("node_router_id")
450 node_group_policy = mso.params.get("node_group_policy")
451 use_router_id_as_loopback = mso.params.get("use_router_id_as_loopback")
452 node_loopback_ip = mso.params.get("node_loopback_ip")
453 path = mso.params.get("path")
454 port_channel = mso.params.get("port_channel")
455 interface_group_policy = mso.params.get("interface_group_policy")
456 ipv4_address = mso.params.get("ipv4_address")
457 ipv6_address = mso.params.get("ipv6_address")
458 ipv6_link_local_address = mso.params.get("ipv6_link_local_address")
459 ipv6_dad = mso.params.get("ipv6_dad")
460 mac = mso.params.get("mac")
461 mtu = mso.params.get("mtu")
462 target_dscp = mso.params.get("target_dscp")
463 encapsulation_type = mso.params.get("encapsulation_type")
464 encapsulation_value = mso.params.get("encapsulation_value")
465 encap = None
466 if encapsulation_type and encapsulation_value:
467 encap = {"encapType": encapsulation_type, "value": encapsulation_value}
468 ptp = mso.params.get("ptp")
469 if ptp is not None and check_if_all_elements_are_none(ptp.values()):
470 ptp = {}
471
472 state = mso.params.get("state")
473
474 mso_template = mso_templates.get_template("l3out", template_name, template_id)
475 mso_template.validate_template("l3out")
476 l3out_object = mso_template.get_l3out_object(l3out_uuid, l3out, True)
477
478 port_channel_uuid = None
479 if port_channel:
480 if port_channel.get("uuid"):
481 port_channel_uuid = port_channel.get("uuid")
482 port_channel_match = mso_template.get_template_object_by_uuid("portChannel", port_channel_uuid, True)
483 else:
484 fabric_resource_mso_template = mso_templates.get_template(
485 "fabric_resource",
486 port_channel.get("reference").get("template"),
487 port_channel.get("reference").get("template_id"),
488 fail_module=True,
489 )
490 port_channel_match = fabric_resource_mso_template.get_port_channel(
491 port_channel_uuid,
492 port_channel.get("reference").get("name"),
493 fail_module=True,
494 ).details
495 port_channel_uuid = port_channel_match.get("uuid")
496 node_id = port_channel_match.get("node")
497
498 pod_id = None
499 if path or port_channel:
500 pod_id = mso.get_site_interface_details(
501 site_id=mso_template.template.get("l3outTemplate", {}).get("siteId"),
502 node=node_id,
503 port=path,
504 port_channel_uuid=port_channel_uuid,
505 ).get("pod")
506
507 match = mso_template.get_l3out_routed_sub_interface(l3out_object.details, pod_id, node_id, path, port_channel_uuid, encap)
508 if (path or port_channel) and encap and match:
509 set_routed_sub_interface_details(mso_template, match.details, l3out_object, mso_templates)
510 mso.existing = mso.previous = copy.deepcopy(match.details) # Query a specific object
511 elif match:
512 mso.existing = [set_routed_sub_interface_details(mso_template, obj, l3out_object, mso_templates) for obj in match]
513
514 l3out_sub_interface_path = "/l3outTemplate/l3outs/{0}/subInterfaces/{1}".format(l3out_object.index, match.index if match else "-")
515
516 ops = []
517
518 if state != "query":
519 l3out_node = L3OutNode(mso.params, mso_template, l3out_object, pod_id, node_id)
520
521 if state == "present":
522
523 mso_values = {
524 "path": path,
525 "pathRef": port_channel_uuid,
526 "pathType": "port" if path else "pc",
527 "group": interface_group_policy,
528 "mac": mac,
529 "mtu": mtu,
530 "targetDscp": target_dscp,
531 "encap": encap,
532 }
533
534 ptp_policy_profile_uuid = None
535 if ptp and ptp.get("user_profile") is not None:
536 ptp_user_profile = ptp.get("user_profile")
537 if ptp_user_profile.get("uuid"):
538 ptp_policy_profile_uuid = ptp_user_profile.get("uuid")
539 else:
540 ptp_policy_profile_uuid = (
541 mso_templates.get_template(
542 "fabric_policy",
543 ptp_user_profile.get("reference").get("template"),
544 ptp_user_profile.get("reference").get("template_id"),
545 fail_module=True,
546 )
547 .get_ptp_policy_profile_object(
548 ptp_policy_profile_uuid,
549 ptp_user_profile.get("reference").get("name"),
550 fail_module=True,
551 )
552 .details.get("uuid")
553 )
554
555 if path:
556 mso_values["nodeID"] = node_id
557 mso_values["podID"] = pod_id
558
559 if match:
560 remove_data = []
561 mso_values[("addresses", "primaryV4")] = ipv4_address
562 mso_values[("addresses", "primaryV6")] = ipv6_address
563 mso_values[("addresses", "linkLocalV6")] = ipv6_link_local_address
564 mso_values[("addresses", "ipV6DAD")] = ipv6_dad
565
566 if ptp == {}:
567 remove_data.append("ptpConfig")
568 elif ptp:
569 if not match.details.get("ptpConfig"):
570 mso_values["ptpConfig"] = {}
571 if ptp.get("unicast_destinations") == []:
572 remove_data.append(("ptpConfig", "destIPs"))
573 elif ptp.get("unicast_destinations"):
574 mso_values[("ptpConfig", "destIPs")] = ptp.get("unicast_destinations")
575 mso_values[("ptpConfig", "mode")] = PTP_MODES.get(ptp.get("mode"))
576 mso_values[("ptpConfig", "srcIP")] = ptp.get("source_address")
577 mso_values[("ptpConfig", "ptpProfileRef")] = ptp_policy_profile_uuid
578
579 append_update_ops_data(ops, match.details, l3out_sub_interface_path, mso_values, remove_data)
580 mso.sanitize(match.details, collate=True, unwanted=remove_data)
581
582 else:
583 mso_values["addresses"] = {
584 "primaryV4": ipv4_address,
585 "primaryV6": ipv6_address,
586 "linkLocalV6": ipv6_link_local_address,
587 "ipV6DAD": ipv6_dad,
588 }
589
590 if ptp:
591 mso_values["ptpConfig"] = {
592 "mode": PTP_MODES.get(ptp.get("mode")),
593 "ptpProfileRef": ptp_policy_profile_uuid,
594 "srcIP": ptp.get("source_address"),
595 "destIPs": ptp.get("unicast_destinations"),
596 }
597
598 mso_values = delete_none_values(mso_values)
599 mso.sanitize(mso_values)
600 ops.append(dict(op="add", path=l3out_sub_interface_path, value=mso.sent))
601
602 # update mso.proposed with interface details that are not included in the interface payload and node details
603 mso.proposed["node"] = l3out_node.construct_node_payload()
604
605 set_routed_sub_interface_details(mso_template, mso.proposed, l3out_object, mso_templates)
606 l3out_node.update_ops(ops)
607
608 elif state == "absent":
609 if match:
610 ops.append(dict(op="remove", path=l3out_sub_interface_path))
611
612 if not mso.module.check_mode and ops:
613 ignore_errors = ["node {0}-{1} doesn't have an interface configured".format(pod_id, node_id)]
614 remove_ops = {ignore_errors[0]: l3out_node.get_node_remove_op()}
615 response = mso.l3out_interface_request(mso_template, ops, ignore_errors, state, remove_ops)
616 l3out_object = mso_template.get_l3out_object(l3out_uuid, l3out, True, search_object=response)
617 match = mso_template.get_l3out_routed_sub_interface(l3out_object.details, pod_id, node_id, path, port_channel_uuid, encap)
618 if match:
619 set_routed_sub_interface_details(mso_template, match.details, l3out_object, mso_templates)
620 mso.existing = match.details # When the state is present
621 else:
622 mso.existing = {} # When the state is absent
623 elif mso.module.check_mode and state != "query": # When the state is present/absent with check mode
624 mso.existing = mso.proposed if state == "present" else {}
625 mso.exit_json()
626
627
628 def set_routed_sub_interface_details(mso_template, routed_sub_interface, l3out_object, mso_templates):
629 mso_template.update_config_with_port_channel_references(routed_sub_interface)
630 mso_template.update_config_with_node_references(routed_sub_interface, l3out_object)
631 mso_template.update_config_with_ptp_references(routed_sub_interface, mso_templates)
632 return routed_sub_interface
633
634
635 if __name__ == "__main__":
636 main()