Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2025, Sabari Jaganathan (@sajagana) <[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_bgp_peer
16 version_added: "2.11.0"
17 short_description: Manage L3Out BGP Peer on Cisco Nexus Dashboard Orchestrator (NDO).
18 description:
19 - Manage L3Out BGP Peer on Cisco Nexus Dashboard Orchestrator (NDO).
20 - This module is only supported on ND v3.1 (NDO v4.3) and later.
21 author:
22 - Sabari Jaganathan (@sajagana)
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 default: node_group
51 choices: [ floating_svi, node_group, routed, routed_sub, svi ]
52 node_group:
53 description:
54 - The name of the Node Group Policy.
55 type: str
56 node_id:
57 description:
58 - The ID of the node (border leaf switch).
59 type: str
60 aliases: [ node, border_leaf, anchor_node ]
61 path:
62 description:
63 - The path of the interface.
64 - The path must be an existing physical port (eg. eth1/1).
65 type: str
66 aliases: [ interface ]
67 port_channel:
68 description:
69 - The port channel details.
70 type: dict
71 aliases: [ pc ]
72 suboptions:
73 uuid:
74 description:
75 - The UUID of the port channel.
76 - This parameter or O(port_channel.reference) is required.
77 type: str
78 reference:
79 description:
80 - The reference details of the port channel.
81 - This parameter or O(port_channel.uuid) is required.
82 type: dict
83 aliases: [ ref ]
84 suboptions:
85 name:
86 description:
87 - The name of the port channel.
88 type: str
89 required: true
90 template:
91 description:
92 - The name of the template that contains the port channel.
93 - This parameter or O(port_channel.reference.template_id) is required.
94 type: str
95 template_id:
96 description:
97 - The ID of the template that contains the port channel.
98 - This parameter or O(port_channel.reference.template) is required.
99 type: str
100 virtual_port_channel:
101 description:
102 - The virtual port channel details.
103 type: dict
104 aliases: [ vpc ]
105 suboptions:
106 uuid:
107 description:
108 - The UUID of the virtual port channel.
109 - This parameter or O(virtual_port_channel.reference) is required.
110 type: str
111 reference:
112 description:
113 - The reference details of the virtual port channel.
114 - This parameter or O(virtual_port_channel.uuid) is required.
115 type: dict
116 aliases: [ ref ]
117 suboptions:
118 name:
119 description:
120 - The name of the virtual port channel.
121 type: str
122 required: true
123 template:
124 description:
125 - The name of the template that contains the virtual port channel.
126 - This parameter or O(virtual_port_channel.reference.template_id) is required.
127 type: str
128 template_id:
129 description:
130 - The ID of the template that contains the virtual port channel.
131 - This parameter or O(virtual_port_channel.reference.template) is required.
132 type: str
133 encapsulation_type:
134 description:
135 - The encapsulation type of the interface.
136 type: str
137 aliases: [ encap_type ]
138 choices: [ vlan, vxlan ]
139 encapsulation_value:
140 description:
141 - The encapsulation value of the interface.
142 - The option O(encapsulation_type=vlan), specifies VLAN ID which must be in the range 1 - 4094.
143 - The option O(encapsulation_type=vxlan), specifies VXLAN Network Identifier (VNI) which must be in the range 5000 - 16777215.
144 type: int
145 aliases: [ encap, encapsulation, encapsulation_id ]
146 ipv4_address:
147 description:
148 - The IPv4 address of the L3Out BGP Peer.
149 - Providing an empty string will remove the O(ipv4_address="") from the L3Out BGP Peer.
150 type: str
151 aliases: [ peer_address_ipv4 ]
152 ipv6_address:
153 description:
154 - The IPv6 address of the L3Out BGP Peer.
155 - Providing an empty string will remove the O(ipv6_address="") from the L3Out BGP Peer.
156 type: str
157 aliases: [ peer_address_ipv6 ]
158 remote_asn:
159 description:
160 - The remote autonomous system number (ASN) of the L3Out BGP Peer.
161 - The value must be between 1 and 4294967295.
162 - Providing an empty string will remove the O(remote_asn="") from the L3Out BGP Peer.
163 type: str
164 admin_state:
165 description:
166 - The administrative state of the L3Out BGP Peer.
167 - Defaults to C(enabled) when unset during creation.
168 type: str
169 choices: [ enabled, disabled ]
170 auth_password:
171 description:
172 - The authentication password of the L3Out BGP Peer.
173 type: str
174 ebgp_multi_hop_ttl:
175 description:
176 - The TTL for eBGP multi-hop of the L3Out BGP Peer.
177 - Defaults to 1 when unset during creation.
178 - The value must be between 1 and 255.
179 type: int
180 site_of_origin:
181 description:
182 - The site of origin for the L3Out BGP Peer. The value must adhere to the pattern "extended:as2-nn2:1000:65534".
183 type: str
184 aliases: [ fabric_of_origin ]
185 weight:
186 description:
187 - The weight of the L3Out BGP Peer.
188 - The value must be between 1 and 65535.
189 - Providing an empty string will remove the O(weight="") from the L3Out BGP Peer.
190 type: str
191 allowed_self_as_count:
192 description:
193 - The allowed self-AS count of the L3Out BGP Peer.
194 - Defaults to 3 when unset during creation.
195 - The value must be between 1 and 10.
196 type: int
197 local_asn_config:
198 description:
199 - The configuration for local ASN of the L3Out BGP Peer.
200 type: str
201 choices: [ none, no_prepend, dual_as, replace_as ]
202 local_asn:
203 description:
204 - The local autonomous system number (ASN) of the L3Out BGP Peer.
205 - The value must be between 1 and 4294967295.
206 - Providing an empty string will remove the O(local_asn="") from the L3Out BGP Peer.
207 type: str
208 import_route_map:
209 description:
210 - The name of the import route map.
211 - Providing an empty dictionary will remove the O(import_route_map={}) from the L3Out BGP Peer.
212 - This parameter can be used instead of O(import_route_map_uuid).
213 type: dict
214 suboptions:
215 name:
216 description:
217 - The name of the import route map.
218 type: str
219 template:
220 description:
221 - The template associated with the import route map.
222 - This parameter or O(import_route_map.template_id) is required.
223 type: str
224 template_id:
225 description:
226 - The template ID associated with the the import route map.
227 - This parameter or O(import_route_map.template) is required.
228 type: str
229 import_route_map_uuid:
230 description:
231 - The UUID of the import route map.
232 - Providing an empty string will remove the O(import_route_map_uuid="") from the L3Out BGP Peer.
233 - This parameter can be used instead of O(import_route_map).
234 type: str
235 export_route_map:
236 description:
237 - The name of the export route map.
238 - Providing an empty dictionary will remove the O(export_route_map={}) from the L3Out BGP Peer.
239 - This parameter can be used instead of O(export_route_map_uuid).
240 type: dict
241 suboptions:
242 name:
243 description:
244 - The name of the export route map.
245 type: str
246 template:
247 description:
248 - The template associated with the export route map.
249 - This parameter or O(export_route_map.template_id) is required.
250 type: str
251 template_id:
252 description:
253 - The template ID associated with the the export route map.
254 - This parameter or O(export_route_map.template) is required.
255 type: str
256 export_route_map_uuid:
257 description:
258 - The UUID of the export route map.
259 - Providing an empty string will remove the O(export_route_map_uuid="") from the L3Out BGP Peer.
260 - This parameter can be used instead of O(export_route_map).
261 type: str
262 peer_prefix:
263 description:
264 - The name of the peer prefix.
265 - Providing an empty dictionary will remove the O(peer_prefix={}) from the L3Out BGP Peer.
266 - This parameter can be used instead of O(peer_prefix_uuid).
267 type: dict
268 suboptions:
269 name:
270 description:
271 - The name of the peer prefix.
272 type: str
273 template:
274 description:
275 - The template associated with the peer prefix.
276 - This parameter or O(peer_prefix.template_id) is required.
277 type: str
278 template_id:
279 description:
280 - The template ID associated with the the peer prefix.
281 - This parameter or O(peer_prefix.template) is required.
282 type: str
283 peer_prefix_uuid:
284 description:
285 - The UUID of the peer prefix.
286 - Providing an empty string will remove the O(peer_prefix_uuid="") from the L3Out BGP Peer.
287 - This parameter can be used instead of O(peer_prefix).
288 type: str
289 bgp_controls:
290 description:
291 - The BGP control settings for the peer.
292 type: dict
293 suboptions:
294 state:
295 description:
296 - Use C(enabled) to configure the BGP control settings.
297 - Use C(disabled) to remove the BGP control settings.
298 type: str
299 choices: [ enabled, disabled ]
300 allow_self_as:
301 description:
302 - The allow self AS flag of the BGP control.
303 type: bool
304 override_as:
305 description:
306 - The override AS flag of the BGP control.
307 type: bool
308 disabled_peer_as_check:
309 description:
310 - The disable peer AS check flag of the BGP control.
311 type: bool
312 next_hop_self:
313 description:
314 - The set next hop to self flag of the BGP control.
315 type: bool
316 send_community:
317 description:
318 - The send community flag of the BGP control.
319 type: bool
320 send_extended_community:
321 description:
322 - The send extended community flag of the BGP control.
323 type: bool
324 send_domain_path:
325 description:
326 - The send domain path flag of the BGP control.
327 type: bool
328 peer_controls:
329 description:
330 - The Peer control settings for the BGP peer.
331 type: dict
332 suboptions:
333 state:
334 description:
335 - Use C(enabled) to configure the Peer control settings.
336 - Use C(disabled) to remove the Peer control settings.
337 type: str
338 choices: [ enabled, disabled ]
339 bfd:
340 description:
341 - The Enable Bidirectional Forwarding Detection (BFD) flag of the Peer control.
342 type: bool
343 disable_peer_connected_check:
344 description:
345 - The disable peer connected check flag of the Peer control.
346 type: bool
347 address_families:
348 description:
349 - The address family controls for the BGP peer.
350 type: dict
351 suboptions:
352 state:
353 description:
354 - Use C(enabled) to configure the address control settings.
355 - Use C(disabled) to remove the address control settings.
356 type: str
357 choices: [ enabled, disabled ]
358 multicast:
359 description:
360 - The multicast address flag of the address family.
361 type: bool
362 unicast:
363 description:
364 - The unicast address flag of the address family.
365 type: bool
366 private_as_controls:
367 description:
368 - The private AS control settings for the BGP peer.
369 type: dict
370 suboptions:
371 state:
372 description:
373 - Use C(enabled) to configure the private AS control settings.
374 - Use C(disabled) to remove the private AS control settings.
375 type: str
376 choices: [ enabled, disabled ]
377 remove_all:
378 description:
379 - The remove all flag of the private AS numbers.
380 type: bool
381 replace_with_local_as:
382 description:
383 - The replace private AS with local AS flag of the private AS control.
384 type: bool
385 state:
386 description:
387 - Use C(absent) for removing.
388 - Use C(query) for listing an object or multiple objects.
389 - Use C(present) for creating or updating.
390 type: str
391 choices: [ absent, query, present ]
392 default: query
393 notes:
394 - The O(template) must exist before using this module in your playbook.
395 Use M(cisco.mso.ndo_template) to create the L3Out template.
396 - The O(l3out) must exist before using this module in your playbook.
397 Use M(cisco.mso.ndo_l3out_template) to create the L3Out.
398 - The O(node_group) must exist before using this module in your playbook.
399 Use M(cisco.mso.ndo_l3out_node_group_policy) to create the L3Out Node Group Policy.
400 - The O(peer_prefix) must exist before using it with this module in your playbook.
401 Use M(cisco.mso.ndo_tenant_bgp_peer_prefix_policy) to create the BGP Peer Prefix Policy.
402 seealso:
403 - module: cisco.mso.ndo_template
404 - module: cisco.mso.ndo_l3out_template
405 - module: cisco.mso.ndo_l3out_node_group_policy
406 - module: cisco.mso.ndo_tenant_bgp_peer_prefix_policy
407 extends_documentation_fragment: cisco.mso.modules
408 """
409
410 EXAMPLES = r"""
411 - name: Create an L3Out BGP Peer with minimum configuration
412 cisco.mso.ndo_l3out_bgp_peer:
413 host: mso_host
414 username: admin
415 password: SomeSecretPassword
416 template: l3out_template
417 l3out: l3out_1
418 node_group: node_group_policy_1
419 ipv4_address: "1.1.1.1"
420 ipv6_address: "1::8/16"
421 auth_password: 123
422 state: present
423
424 - name: Create L3Out BGP Peer on a L3out routed interface of type port
425 cisco.mso.ndo_l3out_bgp_peer:
426 host: mso_host
427 username: admin
428 password: SomeSecretPassword
429 template: l3out_template
430 l3out: l3out_1
431 ipv4_address: 5.5.5.5
432 parent_type: routed
433 node_id: 101
434 path: eth1/1
435 state: present
436
437 - name: Create a L3out routed sub-interface of type port channel
438 cisco.mso.ndo_l3out_routed_sub_interface:
439 host: mso_host
440 username: admin
441 password: SomeSecretPassword
442 template: l3out_template
443 l3out: l3out_1
444 ipv4_address: 5.5.5.5
445 parent_type: routed_sub
446 port_channel:
447 uuid: '{{ port_channel_interface_1.current.uuid }}'
448 encapsulation_type: vlan
449 encapsulation_value: 100
450 state: present
451
452 - name: Create L3Out BGP Peer on a L3out svi interface of type virtual port channel
453 cisco.mso.ndo_l3out_bgp_peer:
454 host: mso_host
455 username: admin
456 password: SomeSecretPassword
457 template: l3out_template
458 l3out: l3out_1
459 ipv4_address: 5.5.5.5
460 parent_type: svi
461 virtual_port_channel:
462 uuid: '{{ virtual_port_channel_interface_1.current.uuid }}'
463 encapsulation_type: vlan
464 encapsulation_value: 100
465 state: present
466
467 - name: Create L3Out BGP Peer on a L3out floating svi interface
468 cisco.mso.ndo_l3out_bgp_peer:
469 host: mso_host
470 username: admin
471 password: SomeSecretPassword
472 template: l3out_template
473 l3out: l3out_1
474 ipv4_address: "1.1.1.1"
475 parent_type: floating_svi
476 node_id: 101
477 encapsulation_type: vlan
478 encapsulation_value: 100
479 state: present
480
481 - name: Update an L3Out BGP Peer with full configuration
482 cisco.mso.ndo_l3out_bgp_peer:
483 host: mso_host
484 username: admin
485 password: SomeSecretPassword
486 template: l3out_template
487 l3out: l3out_1
488 node_group: node_group_policy_1
489 ipv4_address: "1.1.1.1"
490 ipv6_address: "1::8/16"
491 remote_asn: 2
492 admin_state: enabled
493 import_route_map:
494 name: ans_route_map
495 template: ansible_test_policy
496 export_route_map:
497 name: ans_route_map_2
498 template: ansible_test_policy
499 peer_prefix:
500 name: ansible_test_bgp_peer_prefix_policy
501 template: ansible_test_policy
502 ebgp_multi_hop_ttl: 1
503 auth_password: 123
504 weight: 2
505 site_of_origin: "extended:as2-nn2:1000:65534"
506 allowed_self_as_count: 3
507 local_asn_config: replace_as
508 local_asn: 1
509 bgp_controls:
510 allow_self_as: true
511 override_as: true
512 disabled_peer_as_check: true
513 next_hop_self: true
514 send_community: true
515 send_extended_community: true
516 send_domain_path: true
517 peer_controls:
518 bfd: true
519 disable_peer_connected_check: true
520 address_families:
521 multicast: true
522 unicast: true
523 private_as_controls:
524 remove_all: true
525 replace_with_local_as: true
526 state: present
527
528 - name: Query an L3Out BGP Peer with IPv4 and IPv6 addresses
529 cisco.mso.ndo_l3out_bgp_peer:
530 host: mso_host
531 username: admin
532 password: SomeSecretPassword
533 template: l3out_template
534 l3out: l3out_1
535 node_group: node_group_policy_1
536 ipv4_address: "1.1.1.1"
537 ipv6_address: "1::8/16"
538 state: query
539 register: query_with_ipv4_and_ipv6
540
541 - name: Query an L3Out BGP Peer with IPv4
542 cisco.mso.ndo_l3out_bgp_peer:
543 host: mso_host
544 username: admin
545 password: SomeSecretPassword
546 template: l3out_template
547 l3out: l3out_1
548 node_group: node_group_policy_1
549 ipv4_address: "1.1.1.1"
550 state: query
551 register: query_with_ipv4
552
553 - name: Query an L3Out BGP Peer with IPv6
554 cisco.mso.ndo_l3out_bgp_peer:
555 host: mso_host
556 username: admin
557 password: SomeSecretPassword
558 template: l3out_template
559 l3out: l3out_1
560 node_group: node_group_policy_1
561 ipv6_address: "1::8/16"
562 state: query
563 register: query_with_ipv6
564
565 - name: Query all L3Out BGP Peer
566 cisco.mso.ndo_l3out_bgp_peer:
567 host: mso_host
568 username: admin
569 password: SomeSecretPassword
570 template: l3out_template
571 l3out: l3out_1
572 node_group: node_group_policy_1
573 state: query
574 register: query_all
575
576 - name: Remove an L3Out BGP Peer with IPv4 and IPv6 addresses
577 cisco.mso.ndo_l3out_bgp_peer:
578 host: mso_host
579 username: admin
580 password: SomeSecretPassword
581 template: l3out_template
582 l3out: l3out_1
583 node_group: node_group_policy_1
584 ipv4_address: "1.1.1.1"
585 ipv6_address: "1::8/16"
586 state: absent
587
588 - name: Remove an L3Out BGP Peer with IPv4
589 cisco.mso.ndo_l3out_bgp_peer:
590 host: mso_host
591 username: admin
592 password: SomeSecretPassword
593 template: l3out_template
594 l3out: l3out_1
595 node_group: node_group_policy_1
596 ipv4_address: "1.1.1.1"
597 state: absent
598
599 - name: Remove an L3Out BGP Peer with IPv6
600 cisco.mso.ndo_l3out_bgp_peer:
601 host: mso_host
602 username: admin
603 password: SomeSecretPassword
604 template: l3out_template
605 l3out: l3out_1
606 node_group: node_group_policy_1
607 ipv6_address: "1::8/16"
608 state: absent
609 """
610
611 RETURN = r"""
612 """
613
614
615 import copy
616 from ansible.module_utils.basic import AnsibleModule
617 from ansible_collections.cisco.mso.plugins.module_utils.mso import (
618 MSOModule,
619 mso_argument_spec,
620 ndo_template_object_spec,
621 ndo_l3out_port_channel_spec,
622 ndo_l3out_virtual_port_channel_spec,
623 )
624 from ansible_collections.cisco.mso.plugins.module_utils.template import KVPair
625 from ansible_collections.cisco.mso.plugins.module_utils.templates import MSOTemplates
626 from ansible_collections.cisco.mso.plugins.module_utils.constants import LOCAL_ASN_CONFIG
627 from ansible_collections.cisco.mso.plugins.module_utils.utils import append_update_ops_data, check_if_all_elements_are_none
628
629
630 def main():
631 argument_spec = mso_argument_spec()
632 argument_spec.update(
633 template=dict(type="str", aliases=["l3out_template"]),
634 template_id=dict(type="str", aliases=["l3out_template_id"]),
635 l3out=dict(type="str"),
636 l3out_uuid=dict(type="str"),
637 # Default is use because the first release of the module only supported node_group type
638 # This way no changes are needed for existing playbooks
639 parent_type=dict(type="str", choices=["floating_svi", "node_group", "routed", "routed_sub", "svi"], default="node_group"),
640 node_group=dict(type="str"),
641 node_id=dict(type="str", aliases=["node", "border_leaf", "anchor_node"]),
642 path=dict(type="str", aliases=["interface"]),
643 port_channel=ndo_l3out_port_channel_spec(micro_bfd=False),
644 virtual_port_channel=ndo_l3out_virtual_port_channel_spec(side_b=False),
645 encapsulation_type=dict(type="str", choices=["vlan", "vxlan"], aliases=["encap_type"]),
646 encapsulation_value=dict(type="int", aliases=["encap", "encapsulation", "encapsulation_id"]),
647 ipv4_address=dict(type="str", aliases=["peer_address_ipv4"]),
648 ipv6_address=dict(type="str", aliases=["peer_address_ipv6"]),
649 remote_asn=dict(type="str"),
650 admin_state=dict(type="str", choices=["enabled", "disabled"]),
651 ebgp_multi_hop_ttl=dict(type="int"),
652 auth_password=dict(type="str", no_log=True),
653 weight=dict(type="str"),
654 site_of_origin=dict(type="str", aliases=["fabric_of_origin"]),
655 allowed_self_as_count=dict(type="int"),
656 local_asn_config=dict(type="str", choices=list(LOCAL_ASN_CONFIG)),
657 local_asn=dict(type="str"),
658 bgp_controls=dict(
659 type="dict",
660 options=dict(
661 state=dict(type="str", choices=["enabled", "disabled"]),
662 allow_self_as=dict(type="bool"),
663 override_as=dict(type="bool"),
664 disabled_peer_as_check=dict(type="bool"),
665 next_hop_self=dict(type="bool"),
666 send_community=dict(type="bool"),
667 send_extended_community=dict(type="bool"),
668 send_domain_path=dict(type="bool"),
669 ),
670 ),
671 peer_controls=dict(
672 type="dict",
673 options=dict(
674 state=dict(type="str", choices=["enabled", "disabled"]),
675 bfd=dict(type="bool"),
676 disable_peer_connected_check=dict(type="bool"),
677 ),
678 ),
679 address_families=dict(
680 type="dict",
681 options=dict(
682 state=dict(type="str", choices=["enabled", "disabled"]),
683 multicast=dict(type="bool"),
684 unicast=dict(type="bool"),
685 ),
686 ),
687 private_as_controls=dict(
688 type="dict",
689 options=dict(
690 state=dict(type="str", choices=["enabled", "disabled"]),
691 remove_all=dict(type="bool"),
692 replace_with_local_as=dict(type="bool"),
693 ),
694 ),
695 import_route_map=ndo_template_object_spec(),
696 import_route_map_uuid=dict(type="str"),
697 export_route_map=ndo_template_object_spec(),
698 export_route_map_uuid=dict(type="str"),
699 peer_prefix=ndo_template_object_spec(),
700 peer_prefix_uuid=dict(type="str"),
701 state=dict(type="str", default="query", choices=["absent", "query", "present"]),
702 )
703
704 module = AnsibleModule(
705 argument_spec=argument_spec,
706 supports_check_mode=True,
707 required_if=[
708 ["state", "present", ["ipv4_address", "ipv6_address"], True],
709 ["state", "absent", ["ipv4_address", "ipv6_address"], True],
710 ["parent_type", "node_group", ["node_group"]],
711 ["parent_type", "routed", ["path", "port_channel"], True],
712 ["parent_type", "routed_sub", ["path", "port_channel"], True],
713 ["parent_type", "routed_sub", ["encapsulation_type", "encapsulation_value"]],
714 ["parent_type", "svi", ["path", "port_channel", "virtual_port_channel"], True],
715 ["parent_type", "svi", ["encapsulation_type", "encapsulation_value"]],
716 ["parent_type", "floating_svi", ["node_id", "encapsulation_type", "encapsulation_value"]],
717 ],
718 required_by={"path": ("node_id")},
719 mutually_exclusive=[
720 ("template", "template_id"),
721 ("l3out", "l3out_uuid"),
722 ("import_route_map", "import_route_map_uuid"),
723 ("export_route_map", "export_route_map_uuid"),
724 ("peer_prefix", "peer_prefix_uuid"),
725 ("port_channel", "path"),
726 ("port_channel", "node_id"),
727 ("port_channel", "virtual_port_channel"),
728 ("virtual_port_channel", "path"),
729 ("virtual_port_channel", "node_id"),
730 ],
731 required_one_of=[
732 ["template", "template_id"],
733 ["l3out", "l3out_uuid"],
734 ],
735 )
736
737 mso = MSOModule(module)
738 mso_templates = MSOTemplates(mso)
739
740 template = module.params.get("template")
741 template_id = module.params.get("template_id")
742 l3out = module.params.get("l3out")
743 l3out_uuid = module.params.get("l3out_uuid")
744 parent_type = module.params.get("parent_type")
745 ipv4_addr = module.params.get("ipv4_address")
746 ipv6_addr = module.params.get("ipv6_address")
747 remote_asn = module.params.get("remote_asn")
748 admin_state = module.params.get("admin_state")
749 auth_password = module.params.get("auth_password")
750 ebgp_multi_hop_ttl = module.params.get("ebgp_multi_hop_ttl")
751 site_of_origin = module.params.get("site_of_origin")
752 weight = module.params.get("weight")
753 allowed_self_as_count = module.params.get("allowed_self_as_count")
754 local_asn_config = LOCAL_ASN_CONFIG.get(module.params.get("local_asn_config"))
755 local_asn = module.params.get("local_asn")
756
757 import_route_map = module.params.get("import_route_map")
758 import_route_map_uuid = module.params.get("import_route_map_uuid")
759 export_route_map = module.params.get("export_route_map")
760 export_route_map_uuid = module.params.get("export_route_map_uuid")
761 peer_prefix = module.params.get("peer_prefix")
762 peer_prefix_uuid = module.params.get("peer_prefix_uuid")
763
764 bgp_controls = module.params.get("bgp_controls")
765 peer_controls = module.params.get("peer_controls")
766 address_families = module.params.get("address_families")
767 private_as_controls = module.params.get("private_as_controls")
768 state = module.params.get("state")
769
770 mso_template = mso_templates.get_template("l3out", template, template_id)
771 mso_template.validate_template("l3out")
772 mso.check_template_when_name_is_provided(import_route_map)
773 mso.check_template_when_name_is_provided(export_route_map)
774 mso.check_template_when_name_is_provided(peer_prefix)
775
776 l3out_object = mso_template.get_l3out_object(uuid=l3out_uuid, name=l3out, fail_module=True)
777 parent_object, parent_path = mso_template.get_parent_details_for_nested_object_in_l3out(mso_templates, l3out_object)
778
779 bgp_peer_objects = get_bgp_peer_by_address(mso_template, parent_object.details.get("bgpPeers", []), ipv4_addr=ipv4_addr, ipv6_addr=ipv6_addr)
780
781 if bgp_peer_objects and (ipv4_addr or ipv6_addr):
782 set_bgp_peer_details(mso_template, parent_type, parent_object.details, bgp_peer_objects.details)
783 mso.existing = copy.deepcopy(bgp_peer_objects.details) # Query a specific object
784 mso.previous = copy.deepcopy(bgp_peer_objects.details) # Query a specific object
785 elif bgp_peer_objects:
786 mso.existing = [set_bgp_peer_details(mso_template, parent_type, parent_object.details, bgp_peer) for bgp_peer in bgp_peer_objects] # Query all objects
787
788 if state != "query":
789 bgp_peer_path = "{0}/bgpPeers/{1}".format(parent_path, bgp_peer_objects.index if bgp_peer_objects else "-")
790
791 ops = []
792
793 if state == "present":
794 peer_prefix_is_empty = check_if_all_elements_are_none(peer_prefix.values()) if peer_prefix else True # peer_prefix is None or empty dict => True
795 import_route_map_is_empty = check_if_all_elements_are_none(import_route_map.values()) if import_route_map else True
796 export_route_map_is_empty = check_if_all_elements_are_none(export_route_map.values()) if export_route_map else True
797
798 mso_templates = MSOTemplates(mso)
799 if not peer_prefix_is_empty:
800 peer_prefix_template = mso_templates.get_template("tenant", peer_prefix.get("template"), peer_prefix.get("template_id"))
801 peer_prefix_uuid = peer_prefix_template.get_template_policy_uuid("tenant", peer_prefix.get("name"), "bgpPeerPrefixPolicies")
802
803 if not import_route_map_is_empty:
804 import_route_map_template = mso_templates.get_template("tenant", import_route_map.get("template"), import_route_map.get("template_id"))
805 import_route_map_uuid = import_route_map_template.get_template_policy_uuid("tenant", import_route_map.get("name"), "routeMapPolicies")
806
807 if not export_route_map_is_empty:
808 export_route_map_template = mso_templates.get_template("tenant", export_route_map.get("template"), export_route_map.get("template_id"))
809 export_route_map_uuid = export_route_map_template.get_template_policy_uuid("tenant", export_route_map.get("name"), "routeMapPolicies")
810
811 mso_values = dict(
812 peerAddressV4=ipv4_addr,
813 peerAddressV6=ipv6_addr,
814 adminState=admin_state,
815 authEnabled=True if auth_password else False,
816 allowedSelfASCount=allowed_self_as_count,
817 ebpgMultiHopTTL=ebgp_multi_hop_ttl,
818 siteOfOrigin=site_of_origin,
819 localAsnConfig=local_asn_config,
820 peerPrefixRef=peer_prefix_uuid,
821 importRouteMapRef=import_route_map_uuid,
822 exportRouteMapRef=export_route_map_uuid,
823 password=dict(value=auth_password) if auth_password is not None else None,
824 peerAsn=int(remote_asn) if remote_asn not in ["", None] else None,
825 weight=int(weight) if weight not in ["", None] else None,
826 localAsn=int(local_asn) if local_asn not in ["", None] else None,
827 )
828
829 if not mso.existing:
830 # BGP Controls
831 if bgp_controls:
832 bgp_controls_dict = dict()
833
834 if bgp_controls["allow_self_as"]:
835 bgp_controls_dict["allowSelfAS"] = bgp_controls["allow_self_as"]
836
837 if bgp_controls["override_as"]:
838 bgp_controls_dict["asOverride"] = bgp_controls["override_as"]
839
840 if bgp_controls["disabled_peer_as_check"]:
841 bgp_controls_dict["disablePeerASCheck"] = bgp_controls["disabled_peer_as_check"]
842
843 if bgp_controls["next_hop_self"]:
844 bgp_controls_dict["nextHopSelf"] = bgp_controls["next_hop_self"]
845
846 if bgp_controls["send_community"]:
847 bgp_controls_dict["sendCommunity"] = bgp_controls["send_community"]
848
849 if bgp_controls["send_extended_community"]:
850 bgp_controls_dict["sendDomainPath"] = bgp_controls["send_extended_community"]
851
852 if bgp_controls["send_domain_path"]:
853 bgp_controls_dict["sendExtendedCommunity"] = bgp_controls["send_domain_path"]
854
855 mso_values["bgpControls"] = bgp_controls_dict
856
857 # Peer Controls
858 if peer_controls:
859 peer_controls_dict = dict()
860
861 if peer_controls["bfd"]:
862 peer_controls_dict["bfd"] = peer_controls["bfd"]
863
864 if peer_controls["disable_peer_connected_check"]:
865 peer_controls_dict["disableConnectedCheck"] = peer_controls["disable_peer_connected_check"]
866
867 mso_values["peerControls"] = peer_controls_dict
868
869 # Address Type Controls
870 if address_families:
871 address_families_dict = dict()
872
873 if address_families["multicast"]:
874 address_families_dict["afMast"] = address_families["multicast"]
875
876 if address_families["unicast"]:
877 address_families_dict["afUcast"] = address_families["unicast"]
878
879 mso_values["addressTypeControls"] = address_families_dict
880
881 # Private AS Controls
882 if private_as_controls:
883 private_as_controls_dict = dict()
884
885 if private_as_controls["remove_all"]:
886 private_as_controls_dict["removeAll"] = private_as_controls["remove_all"]
887
888 if private_as_controls["replace_with_local_as"]:
889 private_as_controls_dict["replaceWithLocalAS"] = private_as_controls["replace_with_local_as"]
890
891 mso_values["privateASControls"] = private_as_controls_dict
892
893 if private_as_controls and (private_as_controls.get("remove_all") or private_as_controls.get("replace_with_local_as")):
894 mso_values["privateASControls"]["removeExclusive"] = True
895
896 mso.sanitize(mso_values)
897 ops.append(dict(op="add", path=bgp_peer_path, value=mso.sent))
898
899 elif mso.existing:
900 required_keys = ["peerPrefixRef", "importRouteMapRef", "exportRouteMapRef"]
901 proposed_payload = copy.deepcopy(mso.existing)
902 mso_values_remove = list()
903
904 if ipv4_addr == "" and "peerAddressV4" in proposed_payload:
905 mso_values_remove.append("peerAddressV4")
906 mso_values.pop("peerAddressV4", None)
907
908 if ipv6_addr == "" and "peerAddressV6" in proposed_payload:
909 mso_values_remove.append("peerAddressV6")
910 mso_values.pop("peerAddressV6", None)
911
912 if remote_asn == "" and "peerAsn" in proposed_payload:
913 mso_values_remove.append("peerAsn")
914 mso_values.pop("peerAsn", None)
915
916 if weight == "" and "weight" in proposed_payload:
917 mso_values_remove.append("weight")
918 mso_values.pop("weight", None)
919
920 if local_asn == "" and "localAsn" in proposed_payload:
921 mso_values_remove.append("localAsn")
922 mso_values.pop("localAsn", None)
923
924 if site_of_origin == "" and "siteOfOrigin" in proposed_payload:
925 mso_values_remove.append("siteOfOrigin")
926 mso_values.pop("siteOfOrigin", None)
927
928 if mso.existing.get("password", {}).get("ref"):
929 mso.existing["password"].pop("ref", None)
930 mso.previous["password"].pop("ref", None)
931
932 if auth_password == "" and "password" in proposed_payload:
933 mso_values["authEnabled"] = False
934 mso_values.pop("password", None)
935 mso_values_remove.append("password")
936 elif auth_password == "" and "password" in mso_values and "password" not in proposed_payload:
937 mso_values.pop("password", None)
938
939 if (peer_prefix_uuid == "" or peer_prefix_is_empty) and "peerPrefixRef" in proposed_payload:
940 mso_values_remove.append("peerPrefixRef")
941 mso_values.pop("peerPrefixRef", None)
942 proposed_payload["peerPrefixRef"] = ""
943 elif (peer_prefix_uuid == "" or peer_prefix_is_empty) and "peerPrefixRef" not in proposed_payload:
944 mso_values.pop("peerPrefixRef", None)
945
946 if (import_route_map_uuid == "" or import_route_map_is_empty) and "importRouteMapRef" in proposed_payload:
947 mso_values_remove.append("importRouteMapRef")
948 mso_values.pop("importRouteMapRef", None)
949 proposed_payload["importRouteMapRef"] = ""
950 elif (import_route_map_uuid == "" or import_route_map_is_empty) and "importRouteMapRef" not in proposed_payload:
951 mso_values.pop("importRouteMapRef", None)
952
953 if (export_route_map_uuid == "" or export_route_map_is_empty) and "exportRouteMapRef" in proposed_payload:
954 mso_values_remove.append("exportRouteMapRef")
955 mso_values.pop("exportRouteMapRef", None)
956 proposed_payload["exportRouteMapRef"] = ""
957 elif (export_route_map_uuid == "" or export_route_map_is_empty) and "exportRouteMapRef" not in proposed_payload:
958 mso_values.pop("exportRouteMapRef", None)
959
960 # BGP Controls
961 if bgp_controls is not None:
962 if bgp_controls.get("state") == "disabled" and proposed_payload.get("bgpControls"):
963 mso_values_remove.append("bgpControls")
964 mso_values.pop("bgpControls", None)
965 proposed_payload["bgpControls"] = {}
966 elif bgp_controls.get("state") != "disabled":
967 if not proposed_payload.get("bgpControls"):
968 mso_values["bgpControls"] = dict()
969 mso_values[("bgpControls", "allowSelfAS")] = bgp_controls.get("allow_self_as")
970 mso_values[("bgpControls", "asOverride")] = bgp_controls.get("override_as")
971 mso_values[("bgpControls", "disablePeerASCheck")] = bgp_controls.get("disabled_peer_as_check")
972 mso_values[("bgpControls", "nextHopSelf")] = bgp_controls.get("next_hop_self")
973 mso_values[("bgpControls", "sendCommunity")] = bgp_controls.get("send_community")
974 mso_values[("bgpControls", "sendExtendedCommunity")] = bgp_controls.get("send_extended_community")
975 mso_values[("bgpControls", "sendDomainPath")] = bgp_controls.get("send_domain_path")
976
977 # Peer Controls
978 if peer_controls is not None:
979 if peer_controls.get("state") == "disabled" and proposed_payload.get("peerControls"):
980 mso_values_remove.append("peerControls")
981 mso_values.pop("peerControls", None)
982 proposed_payload["peerControls"] = {}
983 elif peer_controls.get("state") != "disabled":
984 if not proposed_payload.get("peerControls"):
985 mso_values["peerControls"] = dict()
986
987 mso_values[("peerControls", "bfd")] = peer_controls.get("bfd")
988 mso_values[("peerControls", "disableConnectedCheck")] = peer_controls.get("disable_peer_connected_check")
989
990 # Address Type Controls
991 if address_families is not None:
992 if address_families.get("state") == "disabled" and proposed_payload.get("addressTypeControls"):
993 mso_values_remove.append("addressTypeControls")
994 mso_values.pop("addressTypeControls", None)
995 proposed_payload["addressTypeControls"] = {}
996 elif address_families.get("state") != "disabled":
997 if not proposed_payload.get("addressTypeControls"):
998 mso_values["addressTypeControls"] = dict()
999
1000 mso_values[("addressTypeControls", "afMast")] = address_families.get("multicast")
1001 mso_values[("addressTypeControls", "afUcast")] = address_families.get("unicast")
1002
1003 # Private AS Controls
1004 if private_as_controls is not None:
1005 if private_as_controls.get("state") == "disabled" and proposed_payload.get("privateASControls"):
1006 mso_values_remove.append("privateASControls")
1007 mso_values.pop("privateASControls", None)
1008 proposed_payload["privateASControls"] = {}
1009 elif private_as_controls.get("state") != "disabled":
1010 if not proposed_payload.get("privateASControls"):
1011 mso_values["privateASControls"] = dict()
1012
1013 mso_values[("privateASControls", "removeAll")] = private_as_controls.get("remove_all")
1014 mso_values[("privateASControls", "replaceWithLocalAS")] = private_as_controls.get("replace_with_local_as")
1015
1016 if private_as_controls.get("remove_all") or private_as_controls.get("replace_with_local_as"):
1017 mso_values[("privateASControls", "removeExclusive")] = True
1018
1019 mso.sanitize(proposed_payload, collate=False, required=required_keys)
1020 append_update_ops_data(ops, proposed_payload, bgp_peer_path, mso_values, mso_values_remove)
1021 elif state == "absent":
1022 if mso.existing:
1023 ops.append(dict(op="remove", path=bgp_peer_path))
1024
1025 if not module.check_mode and ops:
1026 mso_template.template = mso.request(mso_template.template_path, method="PATCH", data=ops)
1027 l3out_object = mso_template.get_l3out_object(uuid=l3out_uuid, name=l3out, fail_module=True)
1028 parent_object = mso_template.get_parent_details_for_nested_object_in_l3out(mso_templates, l3out_object)
1029 match = get_bgp_peer_by_address(mso_template, parent_object[0].details.get("bgpPeers", []), ipv4_addr=ipv4_addr, ipv6_addr=ipv6_addr)
1030 if match:
1031 if match.details.get("password", {}).get("ref"):
1032 match.details["password"].pop("ref", None)
1033 set_bgp_peer_details(mso_template, parent_type, parent_object[0].details, match.details)
1034 mso.existing = match.details # When the state is present
1035 else:
1036 mso.existing = {} # When the state is absent
1037 elif module.check_mode and state != "query": # When the state is present/absent with check mode
1038 set_bgp_peer_details(mso_template, parent_type, parent_object.details, mso.proposed)
1039 mso.existing = mso.proposed if state == "present" else {}
1040
1041 mso.exit_json()
1042
1043
1044 def get_bgp_peer_by_address(mso_template, bgp_peers, ipv4_addr=None, ipv6_addr=None, fail_module=False):
1045 if bgp_peers and (ipv4_addr or ipv6_addr): # Query a specific object
1046 kv_list = []
1047 if ipv4_addr:
1048 kv_list.append(KVPair("peerAddressV4", ipv4_addr))
1049 if ipv6_addr:
1050 kv_list.append(KVPair("peerAddressV6", ipv6_addr))
1051 return mso_template.get_object_by_key_value_pairs("L3Out BGP Peer", bgp_peers, kv_list, fail_module)
1052 return bgp_peers # Query all objects
1053
1054
1055 def set_bgp_peer_details(mso_template, parent_type, parent_object, match):
1056 reference_dict = {
1057 "peerPrefix": {
1058 "name": "peerPrefixName",
1059 "reference": "peerPrefixRef",
1060 "type": "bgpPeerPrefixPol",
1061 "template": "peerPrefixTemplateName",
1062 "templateId": "peerPrefixTemplateId",
1063 },
1064 "importRouteMap": {
1065 "name": "importRouteMapName",
1066 "reference": "importRouteMapRef",
1067 "type": "routeMap",
1068 "template": "importRouteMapTemplateName",
1069 "templateId": "importRouteMapTemplateId",
1070 },
1071 "exportRouteMap": {
1072 "name": "exportRouteMapName",
1073 "reference": "exportRouteMapRef",
1074 "type": "routeMap",
1075 "template": "exportRouteMapTemplateName",
1076 "templateId": "exportRouteMapTemplateId",
1077 },
1078 }
1079
1080 mso_template.update_config_with_template_and_references(match, reference_dict)
1081 mso_template.set_parent_details_for_nested_object_in_l3out(parent_type, parent_object, match)
1082 return match
1083
1084
1085 if __name__ == "__main__":
1086 main()