Debian Codegraph @

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