Debian Codegraph @

ansible_collections/cisco/aci/plugins/modules/aci_l3out_bgp_peer.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import absolute_import, division, print_function
7
8 __metaclass__ = type
9
10 ANSIBLE_METADATA = {
11 "metadata_version": "1.1",
12 "status": ["preview"],
13 "supported_by": "community",
14 }
15
16 DOCUMENTATION = r"""
17 ---
18 module: aci_l3out_bgp_peer
19 short_description: Manage Layer 3 Outside (L3Out) BGP Peers (bgp:PeerP and bgp:InfraPeerP)
20 description:
21 - Manage L3Out BGP Peers on Cisco ACI fabrics.
22 options:
23 tenant:
24 description:
25 - Name of an existing tenant.
26 type: str
27 aliases: [ tenant_name ]
28 l3out:
29 description:
30 - Name of an existing L3Out.
31 type: str
32 aliases: [ l3out_name ]
33 node_profile:
34 description:
35 - Name of the node profile.
36 type: str
37 aliases: [ node_profile_name, logical_node ]
38 interface_profile:
39 description:
40 - Name of the interface profile.
41 type: str
42 aliases: [ interface_profile_name, logical_interface ]
43 pod_id:
44 description:
45 - Pod to build the interface on.
46 type: str
47 node_id:
48 description:
49 - Node to build the interface on for Port-channels and single ports.
50 - Hyphen separated pair of nodes (e.g. "201-202") for vPCs.
51 type: str
52 path_ep:
53 description:
54 - Path to interface
55 - Interface Port Group name for Port-channels and vPCs
56 - Port number for single ports (e.g. "eth1/12")
57 type: str
58 peer_ip:
59 description:
60 - IP address of the BGP peer.
61 type: str
62 remote_asn:
63 description:
64 - Autonomous System Number of the BGP peer.
65 type: int
66 bgp_controls:
67 description:
68 - BGP Controls
69 type: list
70 elements: str
71 choices: [ send-com, send-ext-com, allow-self-as, as-override, dis-peer-as-check, nh-self, send-domain-path ]
72 peer_controls:
73 description:
74 - Peer Controls
75 type: list
76 elements: str
77 choices: [ bfd, dis-conn-check ]
78 address_type_controls:
79 description:
80 - Address Type Controls
81 type: list
82 elements: str
83 choices: [ af-ucast, af-mcast ]
84 private_asn_controls:
85 description:
86 - Private AS Controls
87 type: list
88 elements: str
89 choices: [ remove-exclusive, remove-all, replace-as ]
90 ttl:
91 description:
92 - eBGP Multihop Time To Live
93 type: int
94 weight:
95 description:
96 - Weight for BGP routes from this neighbor
97 type: int
98 admin_state:
99 description:
100 - Admin state for the BGP session
101 type: str
102 choices: [ enabled, disabled ]
103 allow_self_as_count:
104 description:
105 - Number of allowed self AS.
106 - Only used if C(allow-self-as) is enabled under C(bgp_controls).
107 type: int
108 route_control_profiles:
109 description:
110 - List of dictionaries objects, which is used to bind the BGP Peer Connectivity Profile to Route Control Profile.
111 type: list
112 elements: dict
113 suboptions:
114 tenant:
115 description:
116 - Name of the tenant.
117 type: str
118 required: true
119 profile:
120 description:
121 - Name of the Route Control Profile.
122 type: str
123 required: true
124 l3out:
125 description:
126 - Name of the L3 Out.
127 type: str
128 direction:
129 description:
130 - Name of the Route Control Profile direction.
131 type: str
132 required: true
133 local_as_number_config:
134 description:
135 - The local Autonomous System Number (ASN) configuration of the L3Out BGP Peer.
136 - The APIC defaults to C(none) when unset during creation.
137 type: str
138 choices: [ dual-as, no-prepend, none, replace-as ]
139 aliases: [ local_as_num_config ]
140 local_as_number:
141 description:
142 - The local Autonomous System Number (ASN) of the L3Out BGP Peer.
143 - The APIC defaults to 0 when unset during creation.
144 type: int
145 aliases: [ local_as_num ]
146 bgp_password:
147 description:
148 - Password for the BGP Peer.
149 - Providing the password will always result in a change because the set password cannot be retrieved from APIC.
150 type: str
151 description:
152 description:
153 - Description for the BGP Peer.
154 type: str
155 aliases: [ descr ]
156 transport_data_plane:
157 description:
158 - Transport Data Plane type.
159 type: str
160 choices: [ mpls, sr_mpls ]
161 bgp_peer_prefix_policy:
162 description:
163 - BGP Peer Prefix Policy.
164 - BGP Peer Prefix Policy is only allowed to be configured when I(bgp_infra_peer=true).
165 type: str
166 aliases: [ bgp_peer_prefix_policy_name ]
167 peer_type:
168 description:
169 - BGP Peer type.
170 type: str
171 choices: [ sr_mpls ]
172 bgp_infra_peer:
173 description:
174 - BGP Infra peer (bgp:InfraPeerP).
175 type: bool
176 aliases: [ infra ]
177 state:
178 description:
179 - Use C(present) or C(absent) for adding or removing.
180 - Use C(query) for listing an object or multiple objects.
181 type: str
182 choices: [ absent, present, query ]
183 default: present
184 extends_documentation_fragment:
185 - cisco.aci.aci
186 - cisco.aci.annotation
187
188 seealso:
189 - module: cisco.aci.aci_l3out
190 - module: cisco.aci.aci_l3out_logical_node_profile
191 - name: APIC Management Information Model reference
192 description: More information about the internal APIC classes B(bgp:peerP) and B(bgp:InfraPeerP)
193 link: https://developer.cisco.com/docs/apic-mim-ref/
194 author:
195 - Tim Cragg (@timcragg)
196 - Akini Ross (@akinross)
197 """
198
199 EXAMPLES = r"""
200 - name: Add a new BGP peer on a physical interface
201 cisco.aci.aci_l3out_bgp_peer:
202 host: apic
203 username: admin
204 password: SomeSecretPassword
205 tenant: my_tenant
206 l3out: my_l3out
207 node_profile: my_node_profile
208 interface_profile: my_interface_profile
209 pod_id: 1
210 node_id: 201
211 path_ep: eth1/12
212 peer_ip: 192.168.10.2
213 remote_asn: 65456
214 bgp_controls:
215 - nh-self
216 - send-com
217 - send-ext-com
218 peer_controls:
219 - bfd
220 route_control_profiles:
221 - tenant: "ansible_tenant"
222 profile: "anstest_import"
223 direction: "import"
224 - tenant: "ansible_tenant"
225 profile: "anstest_export"
226 direction: "export"
227 l3out: "anstest_l3out"
228 state: present
229 delegate_to: localhost
230
231 - name: Add a new BGP peer on a vPC
232 cisco.aci.aci_l3out_bgp_peer:
233 host: apic
234 username: admin
235 password: SomeSecretPassword
236 tenant: my_tenant
237 l3out: my_l3out
238 node_profile: my_node_profile
239 interface_profile: my_interface_profile
240 pod_id: 1
241 node_id: 201-202
242 path_ep: my_vpc_ipg
243 peer_ip: 192.168.20.2
244 remote_asn: 65457
245 ttl: 4
246 weight: 50
247 state: present
248 delegate_to: localhost
249
250 - name: Create Infra BGP Peer with password
251 aci_l3out_bgp_peer: &infra_bgp_peer
252 host: apic
253 username: admin
254 password: SomeSecretPassword
255 tenant: infra
256 l3out: ansible_infra_l3out
257 node_profile: ansible_infra_l3out_node_profile
258 ttl: 2
259 bgp_infra_peer: true
260 bgp_password: ansible_test_password
261 peer_ip: 192.168.50.2
262 remote_asn: 65450
263 local_as_number: 65460
264 peer_type: sr_mpls
265 bgp_controls:
266 - send-domain-path
267 transport_data_plane: sr_mpls
268 bgp_peer_prefix_policy: ansible_peer_prefix_profile
269 state: present
270 delegate_to: localhost
271
272 - name: Shutdown a BGP peer
273 cisco.aci.aci_l3out_bgp_peer:
274 host: apic
275 username: admin
276 password: SomeSecretPassword
277 tenant: my_tenant
278 l3out: my_l3out
279 node_profile: my_node_profile
280 interface_profile: my_interface_profile
281 pod_id: 1
282 node_id: 201
283 path_ep: eth1/12
284 peer_ip: 192.168.10.2
285 admin_state: disabled
286 state: present
287 delegate_to: localhost
288
289 - name: Delete a BGP peer
290 cisco.aci.aci_l3out_bgp_peer:
291 host: apic
292 username: admin
293 password: SomeSecretPassword
294 tenant: my_tenant
295 l3out: my_l3out
296 node_profile: my_node_profile
297 interface_profile: my_interface_profile
298 pod_id: 1
299 node_id: 201
300 path_ep: eth1/12
301 peer_ip: 192.168.10.2
302 state: absent
303 delegate_to: localhost
304
305 - name: Add BGP Peer to the Node Profile level
306 cisco.aci.aci_l3out_bgp_peer:
307 host: apic
308 username: admin
309 password: SomeSecretPassword
310 tenant: ansible_tenant
311 l3out: ansible_l3out
312 node_profile: ansible_node_profile
313 peer_ip: 192.168.50.3
314 route_control_profiles:
315 - tenant: "ansible_tenant"
316 profile: "anstest_import"
317 direction: "import"
318 - tenant: "ansible_tenant"
319 profile: "anstest_export"
320 direction: "export"
321 l3out: "anstest_l3out"
322 state: present
323 delegate_to: localhost
324
325 - name: Query a BGP peer
326 cisco.aci.aci_l3out_bgp_peer:
327 host: apic
328 username: admin
329 password: SomeSecretPassword
330 tenant: my_tenant
331 l3out: my_l3out
332 node_profile: my_node_profile
333 interface_profile: my_interface_profile
334 pod_id: 1
335 node_id: 201
336 path_ep: eth1/12
337 peer_ip: 192.168.10.2
338 state: query
339 delegate_to: localhost
340 register: query_result
341
342 - name: Query all BGP peer
343 cisco.aci.aci_l3out_bgp_peer:
344 host: apic
345 username: admin
346 password: SomeSecretPassword
347 state: query
348 delegate_to: localhost
349 register: query_all
350
351 - name: Query all BGP infra peer
352 cisco.aci.aci_l3out_bgp_peer:
353 host: apic
354 username: admin
355 password: SomeSecretPassword
356 bgp_infra_peer: true
357 state: query
358 delegate_to: localhost
359 register: query_all
360 """
361
362 RETURN = r"""
363 current:
364 description: The existing configuration from the APIC after the module has finished
365 returned: success
366 type: list
367 sample:
368 [
369 {
370 "fvTenant": {
371 "attributes": {
372 "descr": "Production environment",
373 "dn": "uni/tn-production",
374 "name": "production",
375 "nameAlias": "",
376 "ownerKey": "",
377 "ownerTag": ""
378 }
379 }
380 }
381 ]
382 error:
383 description: The error information as returned from the APIC
384 returned: failure
385 type: dict
386 sample:
387 {
388 "code": "122",
389 "text": "unknown managed object class foo"
390 }
391 raw:
392 description: The raw output returned by the APIC REST API (xml or json)
393 returned: parse error
394 type: str
395 sample: '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1"><error code="122" text="unknown managed object class foo"/></imdata>'
396 sent:
397 description: The actual/minimal configuration pushed to the APIC
398 returned: info
399 type: list
400 sample:
401 {
402 "fvTenant": {
403 "attributes": {
404 "descr": "Production environment"
405 }
406 }
407 }
408 previous:
409 description: The original configuration from the APIC before the module has started
410 returned: info
411 type: list
412 sample:
413 [
414 {
415 "fvTenant": {
416 "attributes": {
417 "descr": "Production",
418 "dn": "uni/tn-production",
419 "name": "production",
420 "nameAlias": "",
421 "ownerKey": "",
422 "ownerTag": ""
423 }
424 }
425 }
426 ]
427 proposed:
428 description: The assembled configuration from the user-provided parameters
429 returned: info
430 type: dict
431 sample:
432 {
433 "fvTenant": {
434 "attributes": {
435 "descr": "Production environment",
436 "name": "production"
437 }
438 }
439 }
440 filter_string:
441 description: The filter string used for the request
442 returned: failure or debug
443 type: str
444 sample: ?rsp-prop-include=config-only
445 method:
446 description: The HTTP method used for the request to the APIC
447 returned: failure or debug
448 type: str
449 sample: POST
450 response:
451 description: The HTTP response from the APIC
452 returned: failure or debug
453 type: str
454 sample: OK (30 bytes)
455 status:
456 description: The HTTP status from the APIC
457 returned: failure or debug
458 type: int
459 sample: 200
460 url:
461 description: The HTTP url used for the request to the APIC
462 returned: failure or debug
463 type: str
464 sample: https://10.11.12.13/api/mo/uni/tn-production.json
465 """
466
467
468 from ansible.module_utils.basic import AnsibleModule
469 from ansible_collections.cisco.aci.plugins.module_utils.aci import (
470 ACIModule,
471 aci_argument_spec,
472 aci_annotation_spec,
473 route_control_profile_spec,
474 )
475
476
477 def main():
478 argument_spec = aci_argument_spec()
479 argument_spec.update(aci_annotation_spec())
480 argument_spec.update(
481 tenant=dict(type="str", aliases=["tenant_name"]),
482 l3out=dict(type="str", aliases=["l3out_name"]),
483 description=dict(type="str", aliases=["descr"]),
484 node_profile=dict(type="str", aliases=["node_profile_name", "logical_node"]),
485 interface_profile=dict(type="str", aliases=["interface_profile_name", "logical_interface"]),
486 state=dict(type="str", default="present", choices=["absent", "present", "query"]),
487 pod_id=dict(type="str"),
488 node_id=dict(type="str"),
489 path_ep=dict(type="str"),
490 peer_ip=dict(type="str"),
491 remote_asn=dict(type="int"),
492 bgp_controls=dict(
493 type="list",
494 elements="str",
495 choices=[
496 "send-com",
497 "send-ext-com",
498 "allow-self-as",
499 "as-override",
500 "dis-peer-as-check",
501 "nh-self",
502 "send-domain-path",
503 ],
504 ),
505 peer_controls=dict(type="list", elements="str", choices=["bfd", "dis-conn-check"]),
506 address_type_controls=dict(type="list", elements="str", choices=["af-ucast", "af-mcast"]),
507 private_asn_controls=dict(
508 type="list",
509 elements="str",
510 choices=["remove-exclusive", "remove-all", "replace-as"],
511 ),
512 ttl=dict(type="int"),
513 weight=dict(type="int"),
514 admin_state=dict(type="str", choices=["enabled", "disabled"]),
515 allow_self_as_count=dict(type="int"),
516 route_control_profiles=dict(
517 type="list",
518 elements="dict",
519 options=route_control_profile_spec(),
520 ),
521 local_as_number_config=dict(type="str", choices=["dual-as", "no-prepend", "none", "replace-as"], aliases=["local_as_num_config"]),
522 local_as_number=dict(type="int", aliases=["local_as_num"]),
523 bgp_password=dict(type="str", no_log=True),
524 transport_data_plane=dict(type="str", choices=["mpls", "sr_mpls"]),
525 bgp_peer_prefix_policy=dict(type="str", aliases=["bgp_peer_prefix_policy_name"]),
526 peer_type=dict(type="str", choices=["sr_mpls"]),
527 bgp_infra_peer=dict(type="bool", aliases=["infra"]),
528 )
529
530 module = AnsibleModule(
531 argument_spec=argument_spec,
532 supports_check_mode=True,
533 required_if=[
534 ["state", "absent", ["tenant", "l3out", "node_profile", "peer_ip"]],
535 ["state", "present", ["tenant", "l3out", "node_profile", "peer_ip"]],
536 ],
537 required_together=[["interface_profile", "pod_id", "node_id", "path_ep"]],
538 )
539
540 tenant = module.params.get("tenant")
541 l3out = module.params.get("l3out")
542 description = module.params.get("description")
543 node_profile = module.params.get("node_profile")
544 interface_profile = module.params.get("interface_profile")
545 state = module.params.get("state")
546 pod_id = module.params.get("pod_id")
547 node_id = module.params.get("node_id")
548 path_ep = module.params.get("path_ep")
549 peer_ip = module.params.get("peer_ip")
550 remote_asn = module.params.get("remote_asn")
551 bgp_controls = module.params.get("bgp_controls")
552 peer_controls = module.params.get("peer_controls")
553 address_type_controls = sorted(module.params.get("address_type_controls") or [])
554 private_asn_controls = module.params.get("private_asn_controls")
555 ttl = module.params.get("ttl")
556 weight = module.params.get("weight")
557 admin_state = module.params.get("admin_state")
558 allow_self_as_count = module.params.get("allow_self_as_count")
559 route_control_profiles = module.params.get("route_control_profiles")
560 local_as_number_config = module.params.get("local_as_number_config")
561 local_as_number = module.params.get("local_as_number")
562 bgp_password = module.params.get("bgp_password")
563 transport_data_plane = module.params.get("transport_data_plane")
564 peer_type = module.params.get("peer_type")
565 bgp_infra_peer = module.params.get("bgp_infra_peer")
566 bgp_peer_prefix_policy = module.params.get("bgp_peer_prefix_policy")
567
568 aci = ACIModule(module)
569 if node_id:
570 if "-" in node_id:
571 path_type = "protpaths"
572 else:
573 path_type = "paths"
574
575 path_dn = "topology/pod-{0}/{1}-{2}/pathep-[{3}]".format(pod_id, path_type, node_id, path_ep)
576
577 child_configs = []
578 child_classes = ["bgpRsPeerPfxPol", "bgpAsP", "bgpLocalAsnP"]
579 aci_class = "bgpInfraPeerP" if bgp_infra_peer else "bgpPeerP"
580
581 if remote_asn is not None:
582 bgp_as_p = dict(bgpAsP=dict(attributes=dict(asn=remote_asn)))
583 if remote_asn == 0:
584 bgp_as_p["bgpAsP"]["attributes"]["status"] = "deleted"
585 child_configs.append(bgp_as_p)
586
587 if local_as_number_config is not None or local_as_number is not None:
588 bgp_local_asn_p = dict(bgpLocalAsnP=dict(attributes=dict(asnPropagate=local_as_number_config, localAsn=local_as_number)))
589 if local_as_number == 0:
590 bgp_local_asn_p["bgpLocalAsnP"]["attributes"]["status"] = "deleted"
591 child_configs.append(bgp_local_asn_p)
592
593 # BGP Peer Prefix Policy is ony configurable on Infra BGP Peer Profile
594 if bgp_peer_prefix_policy is not None:
595 child_configs.append(dict(bgpRsPeerPfxPol=dict(attributes=dict(tnBgpPeerPfxPolName=bgp_peer_prefix_policy))))
596
597 if route_control_profiles:
598 child_classes.append("bgpRsPeerToProfile")
599 for profile in route_control_profiles:
600 if profile.get("l3out"):
601 route_control_profile_dn = "uni/tn-{0}/out-{1}/prof-{2}".format(
602 profile.get("tenant"),
603 profile.get("l3out"),
604 profile.get("profile"),
605 )
606 else:
607 route_control_profile_dn = "uni/tn-{0}/prof-{1}".format(profile.get("tenant"), profile.get("profile"))
608 child_configs.append(
609 dict(
610 bgpRsPeerToProfile=dict(
611 attributes=dict(
612 direction=profile.get("direction"),
613 tDn=route_control_profile_dn,
614 )
615 )
616 )
617 )
618
619 bgp_peer_profile_dict = dict(
620 aci_class=aci_class,
621 aci_rn="infraPeerP-[{0}]".format(peer_ip) if bgp_infra_peer else "peerP-[{0}]".format(peer_ip),
622 module_object=peer_ip,
623 target_filter={"addr": peer_ip},
624 )
625
626 if interface_profile is None:
627 subclass_3 = bgp_peer_profile_dict
628 subclass_4 = None
629 subclass_5 = None
630 else:
631 subclass_3 = dict(
632 aci_class="l3extLIfP",
633 aci_rn="lifp-{0}".format(interface_profile),
634 module_object=interface_profile,
635 target_filter={"name": interface_profile},
636 )
637 subclass_4 = dict(
638 aci_class="l3extRsPathL3OutAtt",
639 aci_rn="rspathL3OutAtt-[{0}]".format(path_dn),
640 module_object=path_dn,
641 target_filter={"tDn": path_dn},
642 )
643 subclass_5 = bgp_peer_profile_dict
644
645 aci.construct_url(
646 root_class=dict(
647 aci_class="fvTenant",
648 aci_rn="tn-{0}".format(tenant),
649 module_object=tenant,
650 target_filter={"name": tenant},
651 ),
652 subclass_1=dict(
653 aci_class="l3extOut",
654 aci_rn="out-{0}".format(l3out),
655 module_object=l3out,
656 target_filter={"name": l3out},
657 ),
658 subclass_2=dict(
659 aci_class="l3extLNodeP",
660 aci_rn="lnodep-{0}".format(node_profile),
661 module_object=node_profile,
662 target_filter={"name": node_profile},
663 ),
664 subclass_3=subclass_3,
665 subclass_4=subclass_4,
666 subclass_5=subclass_5,
667 child_classes=child_classes,
668 )
669
670 aci.get_existing()
671
672 if state == "present":
673 ctrl, ctrl_ext, peerCtrl, addrTCtrl, privateASctrl = None, None, None, None, None
674 if bgp_controls:
675 if transport_data_plane == "mpls":
676 bgp_controls.append("segment-routing-disable")
677
678 if "send-domain-path" in bgp_controls:
679 ctrl_ext = "send-domain-path"
680 bgp_controls.remove("send-domain-path")
681
682 ctrl = ",".join(bgp_controls)
683
684 if peer_controls:
685 peerCtrl = ",".join(peer_controls)
686 if address_type_controls:
687 addrTCtrl = ",".join(address_type_controls)
688 if private_asn_controls:
689 privateASctrl = ",".join(private_asn_controls)
690
691 class_config = dict(
692 descr=description,
693 addr=peer_ip,
694 ctrl=ctrl,
695 ctrlExt=ctrl_ext,
696 peerCtrl=peerCtrl,
697 addrTCtrl=addrTCtrl,
698 privateASctrl=privateASctrl,
699 ttl=ttl,
700 weight=weight,
701 adminSt=admin_state,
702 allowedSelfAsCnt=allow_self_as_count,
703 peerT=peer_type.replace("_", "-") if peer_type else None,
704 )
705
706 # Only add bgp_password if it is set to handle changed status properly because password is not part of existing config
707 if bgp_password:
708 class_config["password"] = bgp_password
709
710 aci.payload(aci_class=aci_class, class_config=class_config, child_configs=child_configs)
711
712 aci.get_diff(aci_class=aci_class)
713
714 aci.post_config()
715
716 elif state == "absent":
717 aci.delete_config()
718
719 aci.exit_json()
720
721
722 if __name__ == "__main__":
723 main()