Debian Codegraph @

ansible_collections/cisco/mso/plugins/modules/ndo_l3out_secondary_ip.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 from __future__ import absolute_import, division, print_function
9
10 __metaclass__ = type
11
12 ANSIBLE_METADATA = {"metadata_version": "1.1", "status": ["preview"], "supported_by": "community"}
13
14 DOCUMENTATION = r"""
15 module: ndo_l3out_secondary_ip
16 version_added: "2.12.0"
17 short_description: Manage L3Out Secondary IP Address on Cisco Nexus Dashboard Orchestrator (NDO).
18 description:
19 - Manage L3Out L3Out Secondary IP Address on Cisco Nexus Dashboard Orchestrator (NDO).
20 - This module is only supported on ND v3.1 (NDO v4.3) and later.
21 author:
22 - Akini Ross (@akinross)
23 options:
24 template:
25 description:
26 - The name of the L3Out template.
27 - This parameter or O(template_id) is required.
28 type: str
29 aliases: [ l3out_template ]
30 template_id:
31 description:
32 - The ID of the L3Out template.
33 - This parameter or O(template) is required.
34 type: str
35 aliases: [ l3out_template_id ]
36 l3out:
37 description:
38 - The name of the L3Out.
39 - This parameter or O(l3out_uuid) is required.
40 type: str
41 l3out_uuid:
42 description:
43 - The UUID of the L3Out.
44 - This parameter or O(l3out) is required.
45 type: str
46 parent_type:
47 description:
48 - The parent type of the L3Out.
49 type: str
50 required: true
51 choices: [ floating_svi, routed, routed_sub, svi, floating_svi_path_attributes ]
52 node_id:
53 description:
54 - The ID of the node (border leaf switch).
55 type: str
56 aliases: [ node, border_leaf, anchor_node ]
57 path:
58 description:
59 - The path of the interface.
60 - The path must be an existing physical port (eg. eth1/1).
61 type: str
62 aliases: [ interface ]
63 port_channel:
64 description:
65 - The port channel details.
66 type: dict
67 aliases: [ pc ]
68 suboptions:
69 uuid:
70 description:
71 - The UUID of the port channel.
72 - This parameter or O(port_channel.reference) is required.
73 type: str
74 reference:
75 description:
76 - The reference details of the port channel.
77 - This parameter or O(port_channel.uuid) is required.
78 type: dict
79 aliases: [ ref ]
80 suboptions:
81 name:
82 description:
83 - The name of the port channel.
84 type: str
85 required: true
86 template:
87 description:
88 - The name of the template that contains the port channel.
89 - This parameter or O(port_channel.reference.template_id) is required.
90 type: str
91 template_id:
92 description:
93 - The ID of the template that contains the port channel.
94 - This parameter or O(port_channel.reference.template) is required.
95 type: str
96 virtual_port_channel:
97 description:
98 - The virtual port channel details.
99 type: dict
100 aliases: [ vpc ]
101 suboptions:
102 uuid:
103 description:
104 - The UUID of the virtual port channel.
105 - This parameter or O(virtual_port_channel.reference) is required.
106 type: str
107 reference:
108 description:
109 - The reference details of the virtual port channel.
110 - This parameter or O(virtual_port_channel.uuid) is required.
111 type: dict
112 aliases: [ ref ]
113 suboptions:
114 name:
115 description:
116 - The name of the virtual port channel.
117 type: str
118 required: true
119 template:
120 description:
121 - The name of the template that contains the virtual port channel.
122 - This parameter or O(virtual_port_channel.reference.template_id) is required.
123 type: str
124 template_id:
125 description:
126 - The ID of the template that contains the virtual port channel.
127 - This parameter or O(virtual_port_channel.reference.template) is required.
128 type: str
129 side_b:
130 description:
131 - Whether the side of the virtual port channel interface is side B.
132 type: bool
133 default: false
134 encapsulation_type:
135 description:
136 - The encapsulation type of the interface.
137 type: str
138 aliases: [ encap_type ]
139 choices: [ vlan, vxlan ]
140 encapsulation_value:
141 description:
142 - The encapsulation value of the interface.
143 - The option O(encapsulation_type=vlan), specifies VLAN ID which must be in the range 1 - 4094.
144 - The option O(encapsulation_type=vxlan), specifies VXLAN Network Identifier (VNI) which must be in the range 5000 - 16777215.
145 type: int
146 aliases: [ encap, encapsulation, encapsulation_id ]
147 domain_type:
148 description:
149 - The type of the domain.
150 type: str
151 choices: [ vmm, physical ]
152 domain_provider:
153 description:
154 - The provider of the domain.
155 type: str
156 choices: [ cloudfoundry, kubernetes, microsoft, openshift, openstack, redhat, vmware, nutanix ]
157 domain:
158 description:
159 - The name of the domain.
160 type: str
161 aliases: [ domain_name ]
162 secondary_address:
163 description:
164 - The secondary IP address.
165 type: str
166 aliases: [ ipv4, ipv6, ip ]
167 dhcp_relay:
168 description:
169 - Whether to enable Dynamic Host Configuration Protocol (DHCP) relay.
170 - If this parameter is unspecified, NDO defaults to O(dhcp_relay=false).
171 type: bool
172 nd_ra_prefix:
173 description:
174 - Whether to enable Neighbor Discovery (ND) Router Advertisement (RA).
175 - If this parameter is unspecified, NDO defaults to O(nd_ra_prefix=false).
176 type: bool
177 ipv6_dad:
178 description:
179 - Whether to enable IPv6 Duplicate Address Detection (DAD).
180 - If this parameter is unspecified, NDO defaults to O(ipv6_dad=disabled) when O(parent_type=floating_svi_path_attributes).
181 - If this parameter is unspecified, NDO defaults to O(ipv6_dad=enabled) for all other parent types.
182 type: str
183 choices: [ enabled, disabled ]
184 state:
185 description:
186 - Use C(absent) for removing.
187 - Use C(query) for listing an object or multiple objects.
188 - Use C(present) for creating or updating.
189 type: str
190 choices: [ absent, query, present ]
191 default: query
192 notes:
193 - The O(template) must exist before using this module in your playbook.
194 Use M(cisco.mso.ndo_template) to create the L3Out template.
195 - The O(l3out) must exist before using this module in your playbook.
196 Use M(cisco.mso.ndo_l3out_template) to create the L3Out.
197 - The parent object defined by O(parent_type) must exist before using this module in your playbook.
198 Use M(cisco.mso.ndo_l3out_routed_interface) to create the L3Out Routed Interface.
199 Use M(cisco.mso.ndo_l3out_routed_sub_interface) to create the L3Out Routed Sub-Interface.
200 Use M(cisco.mso.ndo_l3out_svi_interface) to create the L3Out SVI Interface.
201 Use M(cisco.mso.ndo_l3out_floating_svi_interface) to create the L3Out Floating SVI Interface.
202 Use M(cisco.mso.ndo_l3out_floating_svi_interface_path_attributes) to create the L3Out Floating SVI Interface Path Attributes.
203
204 seealso:
205 - module: cisco.mso.ndo_template
206 - module: cisco.mso.ndo_l3out_template
207 - module: cisco.mso.ndo_l3out_routed_interface
208 - module: cisco.mso.ndo_l3out_routed_sub_interface
209 - module: cisco.mso.ndo_l3out_svi_interface
210 - module: cisco.mso.ndo_l3out_floating_svi_interface
211 - module: cisco.mso.ndo_l3out_floating_svi_interface_path_attributes
212 extends_documentation_fragment: cisco.mso.modules
213 """
214
215 EXAMPLES = r"""
216 - name: Create a secondary address on a L3out Routed Interface of type port
217 cisco.mso.ndo_l3out_secondary_ip:
218 host: mso_host
219 username: admin
220 password: SomeSecretPassword
221 template: l3out_template
222 l3out: l3out_1
223 node_id: 101
224 path: eth1/3
225 parent_type: routed
226 secondary_address: 3::102/16
227 state: present
228
229 - name: Create a secondary address on a L3out Routed Interface of type port channel
230 cisco.mso.ndo_l3out_secondary_ip:
231 host: mso_host
232 username: admin
233 password: SomeSecretPassword
234 template: l3out_template
235 l3out: l3out_1
236 port_channel:
237 uuid: '{{ port_channel_interface_3.current.uuid }}'
238 parent_type: routed
239 secondary_address: 10.2.0.2/24
240 state: present
241
242 - name: Create a secondary address on a L3out SVI Interface of type port
243 cisco.mso.ndo_l3out_secondary_ip:
244 host: mso_host
245 username: admin
246 password: SomeSecretPassword
247 template: l3out_template
248 l3out: l3out_1
249 node_id: 101
250 path: eth1/1
251 encapsulation_type: vlan
252 encapsulation_value: 103
253 parent_type: svi
254 secondary_address: 10.0.0.2/24
255 state: present
256
257 - name: Create a secondary address on a L3out SVI Interface of type port channel
258 cisco.mso.ndo_l3out_secondary_ip:
259 host: mso_host
260 username: admin
261 password: SomeSecretPassword
262 template: l3out_template
263 l3out: l3out_1
264 port_channel:
265 uuid: '{{ port_channel_interface_1.current.uuid }}'
266 encapsulation_type: vxlan
267 encapsulation_value: 50001
268 parent_type: svi
269 secondary_address: 1::102/16
270 state: present
271
272 - name: Create a secondary address on a L3out SVI Interface of type virtual port channel side a
273 cisco.mso.ndo_l3out_secondary_ip:
274 host: mso_host
275 username: admin
276 password: SomeSecretPassword
277 template: l3out_template
278 l3out: l3out_1
279 virtual_port_channel:
280 uuid: '{{ virtual_port_channel_interface_1.current.uuid }}'
281 encapsulation_type: vlan
282 encapsulation_value: 1000
283 parent_type: svi
284 secondary_address: 10.0.2.3/24
285 state: present
286
287 - name: Create a secondary address on a L3out SVI Interface of type virtual port channel side b
288 cisco.mso.ndo_l3out_secondary_ip:
289 host: mso_host
290 username: admin
291 password: SomeSecretPassword
292 template: l3out_template
293 l3out: l3out_1
294 virtual_port_channel:
295 uuid: '{{ virtual_port_channel_interface_1.current.uuid }}'
296 side_b: true
297 encapsulation_type: vlan
298 encapsulation_value: 1000
299 parent_type: svi
300 secondary_address: 10.0.2.4/24
301 state: present
302
303 - name: Create a secondary address on a L3out Routed Sub-Interface of type port
304 cisco.mso.ndo_l3out_secondary_ip:
305 host: mso_host
306 username: admin
307 password: SomeSecretPassword
308 template: l3out_template
309 l3out: l3out_1
310 node_id: 101
311 path: eth1/2
312 encapsulation_type: vlan
313 encapsulation_value: 101
314 parent_type: routed_sub
315 secondary_address: 10.1.0.2/24
316 state: present
317
318 - name: Create a secondary address on a L3out Routed Sub-Interface of type port channel
319 cisco.mso.ndo_l3out_secondary_ip:
320 host: mso_host
321 username: admin
322 password: SomeSecretPassword
323 template: l3out_template
324 l3out: l3out_1
325 port_channel:
326 uuid: '{{ port_channel_interface_2.current.uuid }}'
327 encapsulation_type: vlan
328 encapsulation_value: 101
329 parent_type: routed_sub
330 secondary_address: 2::102/16
331 state: present
332
333 - name: Create a secondary address on a L3Out Floating SVI Interface
334 cisco.mso.ndo_l3out_secondary_ip:
335 host: mso_host
336 username: admin
337 password: SomeSecretPassword
338 template: l3out_template
339 l3out: l3out_1
340 node_id: 101
341 encapsulation_type: vlan
342 encapsulation_value: 100
343 parent_type: floating_svi
344 secondary_address: 10.4.0.3/24
345 state: present
346
347 - name: Create a secondary address on a L3Out Floating SVI Interface path attributes
348 cisco.mso.ndo_l3out_secondary_ip:
349 host: mso_host
350 username: admin
351 password: SomeSecretPassword
352 template: l3out_template
353 l3out: l3out_1
354 node_id: 101
355 encapsulation_type: vlan
356 encapsulation_value: 100
357 domain_type: vmm
358 domain_provider: vmware
359 domain: ansible_test_vmm
360 parent_type: floating_svi_path_attributes
361 secondary_address: 10.4.0.4/24
362 state: present
363
364 - name: Update a secondary address on a L3out Routed Interface of type port
365 cisco.mso.ndo_l3out_secondary_ip:
366 host: mso_host
367 username: admin
368 password: SomeSecretPassword
369 template: l3out_template
370 l3out: l3out_1
371 node_id: 101
372 path: eth1/3
373 parent_type: routed
374 secondary_address: 3::102/16
375 dhcp_relay: true
376 ipv6_dad: disabled
377 nd_ra_prefix: true
378
379 - name: Get a secondary address on a L3out Routed Interface of type port
380 cisco.mso.ndo_l3out_secondary_ip:
381 host: mso_host
382 username: admin
383 password: SomeSecretPassword
384 template: l3out_template
385 l3out: l3out_1
386 node_id: 101
387 path: eth1/3
388 parent_type: routed
389 secondary_address: 3::102/16
390 state: query
391 register: query_secondary_address
392
393 - name: Get all secondary addresses on a L3out Routed Interface of type port
394 cisco.mso.ndo_l3out_secondary_ip:
395 host: mso_host
396 username: admin
397 password: SomeSecretPassword
398 template: l3out_template
399 l3out: l3out_1
400 node_id: 101
401 path: eth1/3
402 parent_type: routed
403 state: query
404 register: query_all
405
406 - name: Delete a secondary address on a L3out Routed Interface of type port
407 cisco.mso.ndo_l3out_secondary_ip:
408 host: mso_host
409 username: admin
410 password: SomeSecretPassword
411 template: l3out_template
412 l3out: l3out_1
413 node_id: 101
414 path: eth1/3
415 parent_type: routed
416 secondary_address: 3::102/16
417 state: absent
418 """
419
420 RETURN = r"""
421 """
422
423
424 import copy
425 from ansible.module_utils.basic import AnsibleModule
426 from ansible_collections.cisco.mso.plugins.module_utils.mso import (
427 MSOModule,
428 mso_argument_spec,
429 ndo_l3out_port_channel_spec,
430 ndo_l3out_virtual_port_channel_spec,
431 )
432 from ansible_collections.cisco.mso.plugins.module_utils.templates import MSOTemplates
433 from ansible_collections.cisco.mso.plugins.module_utils.constants import DOMAIN_TYPE_MAP, VM_DOMAIN_PROVIDER_MAP
434 from ansible_collections.cisco.mso.plugins.module_utils.utils import append_update_ops_data
435
436
437 def main():
438 argument_spec = mso_argument_spec()
439 argument_spec.update(
440 template=dict(type="str", aliases=["l3out_template"]),
441 template_id=dict(type="str", aliases=["l3out_template_id"]),
442 l3out=dict(type="str"),
443 l3out_uuid=dict(type="str"),
444 parent_type=dict(type="str", choices=["floating_svi", "routed", "routed_sub", "svi", "floating_svi_path_attributes"], required=True),
445 node_id=dict(type="str", aliases=["node", "border_leaf", "anchor_node"]),
446 path=dict(type="str", aliases=["interface"]),
447 port_channel=ndo_l3out_port_channel_spec(micro_bfd=False),
448 virtual_port_channel=ndo_l3out_virtual_port_channel_spec(side_b=False, secondary_address=True),
449 encapsulation_type=dict(type="str", choices=["vlan", "vxlan"], aliases=["encap_type"]),
450 encapsulation_value=dict(type="int", aliases=["encap", "encapsulation", "encapsulation_id"]),
451 domain_type=dict(type="str", choices=list(DOMAIN_TYPE_MAP)),
452 domain_provider=dict(type="str", choices=list(VM_DOMAIN_PROVIDER_MAP)),
453 domain=dict(type="str", aliases=["domain_name"]),
454 secondary_address=dict(type="str", aliases=["ip", "ipv4", "ipv6"]),
455 dhcp_relay=dict(type="bool"),
456 nd_ra_prefix=dict(type="bool"),
457 ipv6_dad=dict(type="str", choices=["enabled", "disabled"]),
458 state=dict(type="str", default="query", choices=["absent", "query", "present"]),
459 )
460
461 module = AnsibleModule(
462 argument_spec=argument_spec,
463 supports_check_mode=True,
464 required_if=[
465 ["state", "present", ["secondary_address"]],
466 ["state", "absent", ["secondary_address"]],
467 ["parent_type", "routed", ["path", "port_channel"], True],
468 ["parent_type", "routed_sub", ["path", "port_channel"], True],
469 ["parent_type", "routed_sub", ["encapsulation_type", "encapsulation_value"]],
470 ["parent_type", "svi", ["path", "port_channel", "virtual_port_channel"], True],
471 ["parent_type", "svi", ["encapsulation_type", "encapsulation_value"]],
472 ["parent_type", "floating_svi", ["node_id", "encapsulation_type", "encapsulation_value"]],
473 ["parent_type", "floating_svi_path_attributes", ["node_id", "encapsulation_type", "encapsulation_value", "domain", "domain_type"]],
474 ],
475 required_by={"path": ("node_id")},
476 mutually_exclusive=[
477 ("template", "template_id"),
478 ("l3out", "l3out_uuid"),
479 ("port_channel", "path"),
480 ("port_channel", "node_id"),
481 ("port_channel", "virtual_port_channel"),
482 ("virtual_port_channel", "path"),
483 ("virtual_port_channel", "node_id"),
484 ],
485 required_one_of=[
486 ["template", "template_id"],
487 ["l3out", "l3out_uuid"],
488 ],
489 )
490
491 mso = MSOModule(module)
492 mso_templates = MSOTemplates(mso)
493
494 template = module.params.get("template")
495 template_id = module.params.get("template_id")
496 l3out = module.params.get("l3out")
497 l3out_uuid = module.params.get("l3out_uuid")
498 parent_type = module.params.get("parent_type")
499 secondary_address = module.params.get("secondary_address")
500 dhcp_relay = module.params.get("dhcp_relay")
501 nd_ra_prefix = module.params.get("nd_ra_prefix")
502 ipv6_dad = module.params.get("ipv6_dad")
503 side_b = None
504 virtual_port_channel = module.params.get("virtual_port_channel")
505 if virtual_port_channel:
506 side_b = virtual_port_channel.get("side_b")
507
508 state = module.params.get("state")
509
510 mso_template = mso_templates.get_template("l3out", template, template_id)
511 mso_template.validate_template("l3out")
512
513 l3out_object = mso_template.get_l3out_object(uuid=l3out_uuid, name=l3out, fail_module=True)
514 parent_object, parent_path = mso_template.get_parent_details_for_nested_object_in_l3out(mso_templates, l3out_object)
515 match = mso_template.get_l3out_secondary_address(
516 parent_object.details,
517 parent_type,
518 secondary_address,
519 side_b,
520 )
521 if match and (secondary_address):
522 set_secondary_ip_details(mso_template, parent_type, parent_object.details, match.details, side_b)
523 mso.existing = mso.previous = copy.deepcopy(match.details)
524 elif match:
525 mso.existing = [
526 set_secondary_ip_details(mso_template, parent_type, parent_object.details, bgp_peer, side_b) for bgp_peer in match
527 ] # Query all objects
528
529 if parent_type == "floating_svi_path_attributes":
530 secondary_path = "secondaryAddresses"
531 elif side_b:
532 secondary_path = "sideBAddresses/secondary"
533 else:
534 secondary_path = "addresses/secondary"
535
536 secondary_ip_path = "{0}/{1}/{2}".format(parent_path, secondary_path, match.index if match else "-")
537 ops = []
538
539 if state == "present":
540 mso_values = {"address": secondary_address, "dhcpRelay": dhcp_relay, "v6RAPrefix": nd_ra_prefix, "ipV6DAD": ipv6_dad}
541 mso.sanitize(mso_values)
542 if match:
543 append_update_ops_data(ops, match.details, secondary_ip_path, mso_values)
544 mso.sanitize(match.details, collate=True)
545 else:
546 mso.sanitize(mso_values)
547 ops.append(dict(op="add", path=secondary_ip_path, value=mso.sent))
548 set_secondary_ip_details(mso_template, parent_type, parent_object.details, mso.proposed, side_b)
549
550 elif state == "absent":
551 if mso.existing:
552 ops.append(dict(op="remove", path=secondary_ip_path))
553
554 if not module.check_mode and ops:
555 mso_template.template = mso.request(mso_template.template_path, method="PATCH", data=ops)
556 l3out_object = mso_template.get_l3out_object(uuid=l3out_uuid, name=l3out, fail_module=True)
557 parent_object = mso_template.get_parent_details_for_nested_object_in_l3out(mso_templates, l3out_object)
558 match = mso_template.get_l3out_secondary_address(parent_object[0].details, parent_type, secondary_address, side_b)
559 if match:
560 set_secondary_ip_details(mso_template, parent_type, parent_object[0].details, match.details, side_b)
561 mso.existing = match.details # When the state is present
562 else:
563 mso.existing = {} # When the state is absent
564 elif module.check_mode and state != "query": # When the state is present/absent with check mode
565 mso.existing = mso.proposed if state == "present" else {}
566 mso.exit_json()
567
568
569 def set_secondary_ip_details(mso_template, parent_type, parent_object, match_object, side_b):
570 if side_b is not None:
571 match_object["virtualPortChannelSide"] = "B" if side_b else "A"
572 mso_template.update_config_with_template_and_references(match_object)
573 mso_template.set_parent_details_for_nested_object_in_l3out(parent_type, parent_object, match_object)
574 return match_object
575
576
577 if __name__ == "__main__":
578 main()