Debian Codegraph @

ansible_collections/dellemc/enterprise_sonic/plugins/modules/sonic_bgp_neighbors_af.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # Copyright 2025 Dell Inc. or its subsidiaries. All Rights Reserved.
4 # GNU General Public License v3.0+
5 # (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 """
8 The module file for sonic_bgp_neighbors_af
9 """
10
11 from __future__ import absolute_import, division, print_function
12 __metaclass__ = type
13
14
15 DOCUMENTATION = """
16 ---
17 module: sonic_bgp_neighbors_af
18 version_added: 1.0.0
19 notes:
20 - Tested against Enterprise SONiC Distribution by Dell Technologies.
21 - Supports C(check_mode).
22 author: Niraimadaiselvam M (@niraimadaiselvamm)
23 short_description: Manage the BGP neighbor address-family and its parameters
24 description:
25 - This module provides configuration management of BGP neighbors address-family parameters on devices running Enterprise SONiC.
26 - bgp_as, vrf_name and neighbors need be created in advance on the device.
27 options:
28 config:
29 description:
30 - Specifies the BGP neighbors address-family related configuration.
31 type: list
32 elements: dict
33 suboptions:
34 bgp_as:
35 description:
36 - Specifies the BGP autonomous system (AS) number which is already configured on the device.
37 type: str
38 required: true
39 vrf_name:
40 description:
41 - Specifies the VRF name which is already configured on the device.
42 type: str
43 default: 'default'
44 neighbors:
45 description:
46 - Specifies BGP neighbor related configurations in address-family configuration mode.
47 type: list
48 elements: dict
49 suboptions:
50 neighbor:
51 description:
52 - Neighbor router address which is already configured on the device.
53 type: str
54 required: True
55 address_family:
56 description:
57 - Specifies BGP address-family related configurations.
58 - afi and safi are required together.
59 type: list
60 elements: dict
61 suboptions:
62 afi:
63 description:
64 - Type of address-family to configure.
65 type: str
66 choices:
67 - ipv4
68 - ipv6
69 - l2vpn
70 required: True
71 safi:
72 description:
73 - Specifies the type of cast for the address-family.
74 type: str
75 choices:
76 - unicast
77 - evpn
78 default: unicast
79 activate:
80 description:
81 - Enables the address-family for this neighbor.
82 type: bool
83 allowas_in:
84 description:
85 - Criterion for accepting received advertisements containing the AS number
86 - of this BGP router intance in the AS PATH of received advertisements.
87 - The 'origin' option can not be set to true when a 'value' is set.
88 type: dict
89 suboptions:
90 origin:
91 description:
92 - Accept this BGP router instance's set AS as the origin.
93 type: bool
94 value:
95 description:
96 - Accept up to this number of occurrences of this BGP router's
97 - set AS in the AS-PATH of received advertisements.
98 - (Specify a number in the range 1-10.)
99 type: int
100 fabric_external:
101 description:
102 - Configure a neighbor as fabric-external.
103 - Fabric external is supported only for l2vpn address family.
104 type: bool
105 ip_afi:
106 description:
107 - Common configuration attributes for IPv4 and IPv6 unicast address families.
108 type: dict
109 suboptions:
110 default_policy_name:
111 description:
112 - Specifies routing policy definition.
113 type: str
114 send_default_route:
115 description:
116 - Enable or disable sending of default-route to the neighbor.
117 type: bool
118 default: False
119 prefix_limit:
120 description:
121 - Specifies prefix limit attributes for ipv4-unicast and ipv6-unicast.
122 type: dict
123 suboptions:
124 max_prefixes:
125 description:
126 - Maximum number of prefixes that will be accepted from the neighbor.
127 type: int
128 prevent_teardown:
129 description:
130 - Enable or disable teardown of BGP session when maximum prefix limit is exceeded.
131 type: bool
132 default: False
133 warning_threshold:
134 description:
135 - Threshold on number of prefixes that can be received from a neighbor before generation of warning messages.
136 - Expressed as a percentage of max-prefixes.
137 type: int
138 restart_timer:
139 description:
140 - Time interval in seconds after which the BGP session is re-established after being torn down.
141 type: int
142 discard_extra:
143 description:
144 - Enable or disable discard extra of BGP session when maximum prefix limit is exceeded.
145 type: bool
146 default: False
147 prefix_list_in:
148 description:
149 - Inbound route filtering policy for a neighbor.
150 type: str
151 prefix_list_out:
152 description:
153 - Outbound route filtering policy for a neighbor.
154 type: str
155 route_map:
156 description:
157 - Specifies the route-map.
158 type: list
159 elements: dict
160 suboptions:
161 name:
162 description:
163 - Specifies the name of the route-map.
164 type: str
165 direction:
166 description:
167 - Specifies the direction of the route-map.
168 type: str
169 route_reflector_client:
170 description:
171 - Specifies a neighbor as a route-reflector client.
172 type: bool
173 route_server_client:
174 description:
175 - Specifies a neighbor as a route-server client.
176 type: bool
177 state:
178 description:
179 - Specifies the operation to be performed on the BGP process that is configured on the device.
180 - In case of merged, the input configuration is merged with the existing BGP configuration on the device.
181 - In case of deleted, the existing BGP configuration is removed from the device.
182 default: merged
183 type: str
184 choices: ['merged', 'deleted', 'replaced', 'overridden']
185 """
186
187 EXAMPLES = """
188 # Using "deleted" state
189 #
190 # Before state:
191 # -------------
192 #
193 # sonic# show running-configuration bgp neighbor vrf default
194 # !
195 # neighbor interface Eth1/3
196 # !
197 # address-family ipv4 unicast
198 # activate
199 # allowas-in 4
200 # route-map aa in
201 # route-map aa out
202 # route-reflector-client
203 # route-server-client
204 # send-community both
205 # !
206
207 - name: Delete specific BGP neighbors AF attributes
208 dellemc.enterprise_sonic.sonic_bgp_neighbors_af:
209 config:
210 - bgp_as: 4
211 neighbors:
212 - neighbor: Eth1/3
213 address_family:
214 - afi: ipv4
215 safi: unicast
216 allowas_in:
217 value: 4
218 route_map:
219 - name: aa
220 direction: in
221 - name: aa
222 direction: out
223 route_reflector_client: true
224 route_server_client: true
225 state: deleted
226
227 # After state:
228 # ------------
229 #
230 # sonic# show running-configuration bgp neighbor vrf default
231 # !
232 # neighbor interface Eth1/3
233 # !
234 # address-family ipv4 unicast
235 # send-community both
236 # !
237
238
239 # Using "merged" state
240 #
241 # Before state:
242 # -------------
243 #
244 # sonic# show running-configuration bgp neighbor vrf default 1.1.1.1
245 # (No bgp neighbor configuration present)
246 - name: "Configure BGP neighbor prefix-list attributes"
247 dellemc.enterprise_sonic.sonic_bgp_neighbors_af:
248 config:
249 - bgp_as: 51
250 neighbors:
251 - neighbor: 1.1.1.1
252 address_family:
253 - afi: ipv4
254 safi: unicast
255 ip_afi:
256 default_policy_name: rmap_reg1
257 send_default_route: true
258 prefix_limit:
259 max_prefixes: 2
260 discard_extra: true
261 warning_threshold: 60
262 prefix_list_in: p1
263 prefix_list_out: p2
264 state: merged
265 # After state:
266 # ------------
267 #
268 # sonic# show running-configuration bgp neighbor vrf default 1.1.1.1
269 # !
270 # neighbor 1.1.1.1
271 # !
272 # address-family ipv4 unicast
273 # default-originate route-map rmap_reg1
274 # prefix-list p1 in
275 # prefix-list p2 out
276 # send-community both
277 # maximum-prefix 2 60 discard-extra
278
279
280 # Using "deleted" state
281 #
282 # Before state:
283 # -------------
284 #
285 # sonic# show running-configuration bgp neighbor vrf default
286 # !
287 # neighbor 1.1.1.1
288 # !
289 # address-family ipv6 unicast
290 # default-originate route-map rmap_reg2
291 # prefix-list p1 in
292 # prefix-list p2 out
293 # send-community both
294 # maximum-prefix 5 90 restart 2
295
296 - name: Delete BGP neighbors AF prefix-list attributes
297 dellemc.enterprise_sonic.sonic_bgp_neighbors_af:
298 config:
299 - bgp_as: 51
300 neighbors:
301 - neighbor: 1.1.1.1
302 address_family:
303 - afi: ipv6
304 safi: unicast
305 ip_afi:
306 default_policy_name: rmap_reg2
307 send_default_route: true
308 prefix_limit:
309 max_prefixes: 5
310 warning_threshold: 90
311 restart-timer: 2
312 prefix_list_in: p1
313 prefix_list_out: p2
314 state: deleted
315
316 # After state:
317 # ------------
318 #
319 # sonic# show running-configuration bgp neighbor vrf default
320 # !
321 # neighbor 1.1.1.1
322 # !
323
324
325 # Using "deleted" state
326 #
327 # Before state:
328 # -------------
329 #
330 # sonic# show running-configuration bgp neighbor vrf default
331 # !
332 # neighbor interface Eth1/3
333 # !
334 # address-family ipv4 unicast
335 # activate
336 # allowas-in 4
337 # route-map aa in
338 # route-map aa out
339 # route-reflector-client
340 # route-server-client
341 # send-community both
342 # !
343 # neighbor interface Eth1/5
344 # !
345 # address-family ipv4 unicast
346 # activate
347 # allowas-in origin
348 # send-community both
349 # !
350
351 - name: Delete all BGP neighbors AF configuration
352 dellemc.enterprise_sonic.sonic_bgp_neighbors_af:
353 config:
354 state: deleted
355
356 # After state:
357 # ------------
358 #
359 # sonic# show running-configuration bgp neighbor vrf default
360 # (No bgp neighbor configuration present)
361
362
363 # Using "merged" state
364 #
365 # Before state:
366 # -------------
367 #
368 # sonic# show running-configuration bgp neighbor vrf default
369 # !
370 # neighbor interface Eth1/3
371 # !
372
373 - name: Merge BGP neighbors AF configuration
374 dellemc.enterprise_sonic.sonic_bgp_neighbors_af:
375 config:
376 - bgp_as: 4
377 neighbors:
378 - neighbor: Eth1/3
379 address_family:
380 - afi: ipv4
381 safi: unicast
382 allowas_in:
383 value: 4
384 route_map:
385 - name: aa
386 direction: in
387 - name: aa
388 direction: out
389 route_reflector_client: true
390 route_server_client: true
391 state: merged
392
393 # After state:
394 # ------------
395 #
396 # sonic# show running-configuration bgp neighbor vrf default
397 # !
398 # neighbor interface Eth1/3
399 # !
400 # address-family ipv4 unicast
401 # activate
402 # allowas-in 4
403 # route-map aa in
404 # route-map aa out
405 # route-reflector-client
406 # route-server-client
407 # send-community both
408 # !
409
410
411 # Using "merged" state
412 #
413 # Before state:
414 # -------------
415 #
416 # sonic# show running-configuration bgp neighbor vrf default
417 # (No bgp neighbor configuration present)
418
419 - name: Configure BGP neighbors AF prefix-list attributes
420 dellemc.enterprise_sonic.sonic_bgp_neighbors_af:
421 config:
422 - bgp_as: 51
423 neighbors:
424 - neighbor: 1.1.1.1
425 address_family:
426 - afi: ipv4
427 safi: unicast
428 ip_afi:
429 default_policy_name: rmap_reg1
430 send_default_route: true
431 prefix_limit:
432 max_prefixes: 1
433 prevent_teardown: true
434 warning_threshold: 80
435 prefix_list_in: p1
436 prefix_list_out: p2
437 state: merged
438
439 # After state:
440 # ------------
441 #
442 # sonic# show running-configuration bgp neighbor vrf default
443 # !
444 # neighbor 1.1.1.1
445 # !
446 # address-family ipv4 unicast
447 # default-originate route-map rmap_reg1
448 # prefix-list p1 in
449 # prefix-list p2 out
450 # send-community both
451 # maximum-prefix 1 80 warning-only
452 # !
453
454
455 # Using "replaced" state
456 #
457 # Before state:
458 # -------------
459 #
460 # sonic# show running-configuration bgp neighbor vrf default
461 # !
462 # neighbor 1.1.1.1
463 # !
464 # address-family ipv6 unicast
465 # default-originate route-map rmap_reg2
466 # prefix-list p1 in
467 # prefix-list p2 out
468 # send-community both
469 # maximum-prefix 5 90 restart 2
470 # !
471
472 - name: Replace BGP neighbors AF attributes
473 dellemc.enterprise_sonic.sonic_bgp_neighbors_af:
474 config:
475 - bgp_as: 51
476 neighbors:
477 - neighbor: 1.1.1.1
478 address_family:
479 - afi: ipv4
480 safi: unicast
481 ip_afi:
482 default_policy_name: rmap_reg1
483 send_default_route: true
484 prefix_limit:
485 max_prefixes: 1
486 prevent_teardown: true
487 warning_threshold: 80
488 prefix_list_in: p1
489 prefix_list_out: p2
490 state: replaced
491
492 # After state:
493 # ------------
494 #
495 # sonic# show running-configuration bgp neighbor vrf default
496 # !
497 # neighbor 1.1.1.1
498 # !
499 # address-family ipv4 unicast
500 # default-originate route-map rmap_reg1
501 # prefix-list p1 in
502 # prefix-list p2 out
503 # send-community both
504 # maximum-prefix 1 80 warning-only
505 # !
506
507
508 # Using "overridden" state
509 #
510 # Before state:
511 # -------------
512 #
513 # sonic# show running-configuration bgp neighbor vrf default
514 # !
515 # neighbor 1.1.1.1
516 # !
517 # address-family ipv6 unicast
518 # default-originate route-map rmap_reg2
519 # prefix-list p1 in
520 # prefix-list p2 out
521 # send-community both
522 # maximum-prefix 5 90 restart 2
523 # !
524
525 - name: Override BGP neighbors AF configuration
526 dellemc.enterprise_sonic.sonic_bgp_neighbors_af:
527 config:
528 - bgp_as: 51
529 neighbors:
530 - neighbor: 2.2.2.2
531 address_family:
532 - afi: ipv4
533 safi: unicast
534 ip_afi:
535 default_policy_name: rmap_reg1
536 send_default_route: true
537 prefix_limit:
538 max_prefixes: 1
539 prevent_teardown: true
540 warning_threshold: 80
541 prefix_list_in: p1
542 prefix_list_out: p2
543 state: overridden
544
545 # After state:
546 # ------------
547 #
548 # sonic# show running-configuration bgp neighbor vrf default
549 # !
550 # neighbor 2.2.2.2
551 # !
552 # address-family ipv4 unicast
553 # default-originate route-map rmap_reg1
554 # prefix-list p1 in
555 # prefix-list p2 out
556 # send-community both
557 # maximum-prefix 1 80 warning-only
558 # !
559 """
560
561 RETURN = """
562 before:
563 description: The configuration prior to the module invocation.
564 returned: always
565 type: list
566 sample: >
567 The configuration returned is always in the same format
568 as the parameters above.
569 after:
570 description: The resulting configuration module invocation.
571 returned: when changed
572 type: list
573 sample: >
574 The configuration returned is always in the same format
575 as the parameters above.
576 after_generated:
577 description: The generated configuration module invocation.
578 returned: when C(check_mode)
579 type: list
580 sample: >
581 The configuration returned will always be in the same format
582 as the parameters above.
583 commands:
584 description: The set of commands pushed to the remote device.
585 returned: always
586 type: list
587 sample: ['command 1', 'command 2', 'command 3']
588 """
589
590
591 from ansible.module_utils.basic import AnsibleModule
592 from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.argspec.bgp_neighbors_af.bgp_neighbors_af import Bgp_neighbors_afArgs
593 from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.config.bgp_neighbors_af.bgp_neighbors_af import Bgp_neighbors_af
594
595
596 def main():
597 """
598 Main entry point for module execution
599
600 :returns: the result form module invocation
601 """
602 module = AnsibleModule(argument_spec=Bgp_neighbors_afArgs.argument_spec,
603 supports_check_mode=True)
604
605 result = Bgp_neighbors_af(module).execute_module()
606 module.exit_json(**result)
607
608
609 if __name__ == '__main__':
610 main()