Debian Codegraph @

ansible_collections/dellemc/enterprise_sonic/plugins/modules/sonic_ospfv2.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_ospfv2
9 """
10
11 from __future__ import absolute_import, division, print_function
12 __metaclass__ = type
13
14
15 DOCUMENTATION = """
16 ---
17 module: sonic_ospfv2
18 version_added: '2.5.0'
19 notes:
20 - Supports C(check_mode).
21 short_description: Configure global OSPFv2 protocol settings on SONiC.
22 description:
23 - This module provides configuration management of global OSPFv2 parameters on devices running SONiC.
24 - Configure VRF instance before configuring OSPF in a VRF.
25 author: "Santhosh kumar T (@santhosh-kt)"
26 options:
27 config:
28 description:
29 - Specifies the OSPFv2 related configuration.
30 - I(non_passive_interfaces) and I(passive_interfaces) are mutually exclusive.
31 - When I(default_passive=True), I(passive_interfaces) cannot be configured.
32 - When I(default_passive=False), I(non_passive_interfaces) cannot be configured.
33 type: list
34 elements: dict
35 suboptions:
36 abr_type:
37 description:
38 - Configure router ABR type.
39 - C(cisco) - Cisco implementation type ABR.
40 - C(ibm) - IBM implementation type ABR.
41 - C(shortcut) - Shortcut ABR.
42 - C(standard) - RFC2328 Standard implementation ABR.
43 type: str
44 choices:
45 - cisco
46 - ibm
47 - shortcut
48 - standard
49 auto_cost_reference_bandwidth:
50 description:
51 - Configure interface auto cost reference bandwidth (1 to 4294967).
52 type: int
53 default_metric:
54 description:
55 - Configure metric for redistributed routes (0 to 16777214).
56 type: int
57 default_passive:
58 description:
59 - Suppresses OSPFv2 routing updates on all interfaces.
60 type: bool
61 distance:
62 description:
63 - Configure route administrative distance.
64 type: dict
65 suboptions:
66 all:
67 description:
68 - Distance value for all type of routes (1 to 255).
69 type: int
70 external:
71 description:
72 - Distance value for external routes (1 to 255).
73 type: int
74 inter_area:
75 description:
76 - Distance value for inter-area routes (1 to 255).
77 type: int
78 intra_area:
79 description:
80 - Distance value for intra-area routes (1 to 255).
81 type: int
82 graceful_restart:
83 description:
84 - OSPF non stop forwarding (NSF) also known as OSPF Graceful Restart.
85 type: dict
86 suboptions:
87 enable:
88 description:
89 - Enable graceful restart.
90 type: bool
91 grace_period:
92 description:
93 - Maximum length of the grace period in seconds (1 to 1800).
94 type: int
95 helper:
96 description:
97 - OSPF GR Helper.
98 type: dict
99 suboptions:
100 enable:
101 description:
102 - Enable Helper support.
103 type: bool
104 advertise_router_id:
105 description:
106 - Advertising Router ID.
107 type: list
108 elements: str
109 planned_only:
110 description:
111 - Supported only planned restart.
112 type: bool
113 strict_lsa_checking:
114 description:
115 - Enable strict LSA check.
116 type: bool
117 supported_grace_time:
118 description:
119 - Supported grace interval in seconds (10 to 1800).
120 type: int
121 log_adjacency_changes:
122 description:
123 - Enable OSPFv2 adjacency state logs.
124 type: str
125 choices:
126 - brief
127 - detail
128 max_metric:
129 description:
130 - Enables infinite metric advertising in OSPFv2 LSAs.
131 type: dict
132 suboptions:
133 administrative:
134 description:
135 - Enables administrative type infinite metric advertising.
136 type: bool
137 external_lsa_all:
138 description:
139 - Configure external LSA all prefix max metric advertising.
140 - Configure the maximum metric value (1 to 16777215).
141 type: int
142 external_lsa_connected:
143 description:
144 - Configure external LSA connected prefix max metric advertising.
145 - Configure the maximum metric value (1 to 16777215).
146 type: int
147 on_startup:
148 description:
149 - Enables infinite metric advertising at OSPFv2 router startup (5 to 86400).
150 type: int
151 router_lsa_all:
152 description:
153 - Configure router LSA all link max metric advertising.
154 - Configure the maximum metric value (1 to 16777215).
155 type: int
156 router_lsa_stub:
157 description:
158 - Configure router LSA stub link max metric advertising.
159 - Configure the maximum metric value (1 to 16777215).
160 type: int
161 maximum_paths:
162 description:
163 - Configure maximum number of multiple paths for ECMP support (1 to 256).
164 type: int
165 non_passive_interfaces:
166 description:
167 - Configure non passive interface types.
168 type: list
169 elements: dict
170 suboptions:
171 addresses:
172 description:
173 - Configure Interface IPv4 addresses.
174 type: list
175 elements: str
176 interface:
177 description:
178 - Full name of the Layer 3 interface, i.e. Eth1/1.
179 type: str
180 required: true
181 opaque_lsa_capability:
182 description:
183 - Enables opaque LSA capability.
184 type: bool
185 passive_interfaces:
186 description:
187 - Configure passive interface types.
188 type: list
189 elements: dict
190 suboptions:
191 addresses:
192 description:
193 - Configure Interface IPv4 addresses.
194 type: list
195 elements: str
196 interface:
197 description:
198 - Full name of the Layer 3 interface, i.e. Eth1/1.
199 type: str
200 required: true
201 redistribute:
202 description:
203 - Configure route redistribution into OSPFv2 router.
204 type: list
205 elements: dict
206 suboptions:
207 always:
208 description:
209 - Enable default route redistribution into OSPF always.
210 - Only available for I(protocol=default_route).
211 type: bool
212 metric:
213 description:
214 - Metric value for redistributed routes (0 to 16777214).
215 type: int
216 metric_type:
217 description:
218 - Metric type for redistributed routes.
219 type: int
220 choices:
221 - 1
222 - 2
223 protocol:
224 description:
225 - Configure the type of protocol to redistribute into OSPF.
226 - Deleting I(protocol) alone will also delete all the other configuration under I(redistribute).
227 - C(bgp) - Border Gateway Protocol.
228 - C(connected) - Directly connected or attached subnets and hosts.
229 - C(default_route) - Default routes.
230 - C(kernel) - Kernel routes other than FRR installed routes.
231 - C(static) - Statically configured routes.
232 type: str
233 choices:
234 - bgp
235 - connected
236 - default_route
237 - kernel
238 - static
239 required: true
240 route_map:
241 description:
242 - Route map to filter redistributed routes.
243 - Configure route map before.
244 type: str
245 refresh_timer:
246 description:
247 - Configures LSA refresh interval in seconds (10 to 1800).
248 type: int
249 rfc1583_compatible:
250 description:
251 - Enable OSPFv2 RFC compatibility.
252 type: bool
253 router_id:
254 description:
255 - Configure OSPFv2 router identifier (A.B.C.D).
256 type: str
257 timers:
258 description:
259 - Configures router timers.
260 type: dict
261 suboptions:
262 lsa_min_arrival:
263 description:
264 - LSA minimum arrival timer in milliseconds (0 to 600000).
265 type: int
266 throttle_lsa_all:
267 description:
268 - LSA delay between transmissions in milliseconds (0 to 5000).
269 type: int
270 throttle_spf:
271 description:
272 - OSPFv2 SPF timers.
273 - I(delay_time), I(initial_hold_time) and I(maximum_hold_time) are required together.
274 type: dict
275 suboptions:
276 delay_time:
277 description:
278 - SPF delay time in milliseconds (0 to 600000).
279 type: int
280 initial_hold_time:
281 description:
282 - SPF initial hold time in milliseconds (0 to 600000).
283 type: int
284 maximum_hold_time:
285 description:
286 - SPF maximum hold time in milliseconds (0 to 600000).
287 type: int
288 write_multiplier:
289 description:
290 - Specifies the maximum number of interfaces serviced per write (1 to 100)
291 type: int
292 vrf_name:
293 description:
294 - Specifies the vrf name.
295 type: str
296 default: 'default'
297 state:
298 description:
299 - Specifies the operation to be performed on the OSPFv2 process configured on the device.
300 - In case of merged, the input configuration will be merged with the existing OSPFv2 configuration on the device.
301 - In case of deleted, the existing OSPFv2 configuration will be removed from the device.
302 - In case of overridden, all the existing OSPFv2 configuration will be deleted and the specified input configuration will be installed.
303 - In case of replaced, the existing OSPFv2 configuration on the device will be replaced by the configuration
304 in the playbook for each VRF group configured by the playbook.
305 type: str
306 default: merged
307 choices: ['merged', 'deleted', 'replaced', 'overridden']
308 """
309
310 EXAMPLES = """
311 # Using "deleted" state
312
313 # Before state:
314 # -------------
315 #
316 # sonic# show running-configuration ospf
317 # router ospf vrf Vrf_1
318 # default-metric 100
319 # max-metric router-lsa external-lsa all 2
320 # passive-interface default
321 # timers throttle spf 50 20 10
322 # timers throttle lsa all 300
323 # redistribute bgp metric 15 metric-type 2 route-map RMAP
324 # no passive-interface Eth1/1 2.2.2.2
325 # no passive-interface Eth1/2 2.2.2.2
326 # !
327 # router ospf
328 # ospf router-id 20.20.20.20
329 # distance 30
330 # distance ospf external 20
331 # refresh timer 300
332 # write-multiplier 20
333 # maximum-paths 200
334 # passive-interface Eth1/2 3.3.3.3
335 # passive-interface Eth1/3
336 # !
337 # sonic#
338 # sonic# show running-configuration vrf Vrf_1
339 # !
340 # ip vrf Vrf_1
341 # sonic# show running-configuration ip prefix-list
342 # !
343 # ip prefix-list PRF_LIST seq 1 permit 1.1.1.1/24
344 # ip prefix-list PRF_LIST2 seq 1 permit 1.1.1.1/24
345 # sonic# show running-configuration route-map
346 # !
347 # route-map RMAP permit 1
348 # sonic#
349
350 - name: Delete the OSPFv2 configurations
351 sonic_ospf:
352 config:
353 - vrf_name: 'default'
354 router_id: "20.20.20.20"
355 distance:
356 external: 20
357 default_passive: false
358 maximum_paths: 200
359 passive_interfaces:
360 interfaces:
361 - interface: 'Eth1/3'
362 redistribute:
363 - protocol: "bgp"
364 metric: 15
365 metric_type: 2
366 route_map: "RMAP"
367 refresh_timer: 300
368 - vrf_name: "Vrf_1"
369 timers:
370 throttle_spf:
371 delay_time: 50
372 initial_hold_time: 20
373 maximum_hold_time: 10
374 default_metric: 100
375 max_metric:
376 external_lsa_all: 2
377 non_passive_interfaces:
378 interfaces:
379 - interface: "Eth1/2"
380 addresses:
381 - "2.2.2.2"
382 state: deleted
383
384 # After state:
385 # ------------
386 #
387 # sonic# show running-configuration ospf
388 # router ospf vrf Vrf_1
389 # passive-interface default
390 # timers throttle lsa all 300
391 # no passive-interface Eth1/1 2.2.2.2
392 # no passive-interface Eth1/2 2.2.2.2
393 # !
394 # router ospf
395 # distance 30
396 # write-multiplier 20
397 # passive-interface Eth1/2 3.3.3.3
398 # !
399 # sonic#
400
401
402 # Using "deleted" state
403
404 # Before state:
405 # -------------
406 #
407 # sonic# show running-configuration ospf
408 # router ospf vrf Vrf_1
409 # passive-interface default
410 # timers throttle lsa all 300
411 # no passive-interface Eth1/1 2.2.2.2
412 # no passive-interface Eth1/2 2.2.2.2
413 # !
414 # router ospf
415 # distance 30
416 # write-multiplier 20
417 # passive-interface Eth1/2 3.3.3.3
418 # !
419 # sonic#
420 # sonic# show running-configuration vrf Vrf_1
421 # !
422 # ip vrf Vrf_1
423 # sonic# show running-configuration ip prefix-list
424 # !
425 # ip prefix-list PRF_LIST seq 1 permit 1.1.1.1/24
426 # ip prefix-list PRF_LIST2 seq 1 permit 1.1.1.1/24
427 # sonic# show running-configuration route-map
428 # !
429 # route-map RMAP permit 1
430 # sonic#
431
432 - name: Delete the OSPFv2 configurations
433 sonic_ospf:
434 config:
435 - vrf_name: "Vrf_1"
436 state: deleted
437
438 # After state:
439 # ------------
440 #
441 # sonic# show running-configuration ospf
442 # router ospf
443 # distance 30
444 # write-multiplier 20
445 # passive-interface Eth1/2 3.3.3.3
446 # !
447 # sonic#
448
449
450 # Using "merged" state
451
452 # Before state:
453 # -------------
454 #
455 # sonic# show running-configuration ospf
456 # (No ospf configuration present)
457 # sonic# show running-configuration vrf Vrf_1
458 # !
459 # ip vrf Vrf_1
460 # sonic# show running-configuration ip prefix-list
461 # !
462 # ip prefix-list PRF_LIST seq 1 permit 1.1.1.1/24
463 # ip prefix-list PRF_LIST2 seq 1 permit 1.1.1.1/24
464 # sonic# show running-configuration route-map
465 # !
466 # route-map RMAP permit 1
467 # sonic#
468
469 - name: Add the OSPFv2 configurations
470 sonic_ospf:
471 config:
472 - vrf_name: 'default'
473 router_id: "10.10.10.10"
474 distance:
475 external: 20
476 auto_cost_reference_bandwidth: 100
477 - vrf_name: "Vrf_1"
478 timers:
479 throttle_lsa_all: 300
480 throttle_spf:
481 delay_time: 10
482 initial_hold_time: 20
483 maximum_hold_time: 50
484 redistribute:
485 - protocol: "bgp"
486 metric: 15
487 metric_type: 2
488 route_map: "RMAP"
489 default_passive: true
490 non_passive_interfaces:
491 interfaces:
492 - interface: "Eth1/1"
493 addresses:
494 - "2.2.2.2"
495 state: merged
496
497 # After state:
498 # ------------
499 #
500 # sonic# show running-configuration ospf
501 # router ospf vrf Vrf_1
502 # passive-interface default
503 # timers throttle spf 10 20 50
504 # timers throttle lsa all 300
505 # redistribute bgp metric 15 metric-type 2 route-map RMAP
506 # no passive-interface Eth1/1 2.2.2.2
507 # !
508 # router ospf
509 # auto-cost reference-bandwidth 100
510 # ospf router-id 10.10.10.10
511 # distance ospf external 20
512 # !
513 # sonic#
514
515
516 # Using "merged" state
517
518 # Before state:
519 # -------------
520 #
521 # sonic# show running-configuration ospf
522 # router ospf vrf Vrf_1
523 # passive-interface default
524 # timers throttle spf 10 20 50
525 # timers throttle lsa all 300
526 # redistribute bgp metric 15 metric-type 2 route-map RMAP
527 # no passive-interface Eth1/1 2.2.2.2
528 # !
529 # router ospf
530 # ospf router-id 10.10.10.10
531 # distance ospf external 20
532 # !
533 # sonic#
534 # sonic# show running-configuration vrf Vrf_1
535 # !
536 # ip vrf Vrf_1
537 # sonic# show running-configuration ip prefix-list
538 # !
539 # ip prefix-list PRF_LIST seq 1 permit 1.1.1.1/24
540 # ip prefix-list PRF_LIST2 seq 1 permit 1.1.1.1/24
541 # sonic# show running-configuration route-map
542 # !
543 # route-map RMAP permit 1
544 # sonic#
545
546 - name: Add the OSPFv2 configurations
547 sonic_ospf:
548 config:
549 - vrf_name: 'default'
550 write_multiplier: 20
551 router_id: "20.20.20.20"
552 distance:
553 all: 30
554 default_passive: false
555 graceful_restart:
556 enable: true
557 grace_period: 100
558 helper:
559 enable: true
560 planned_only: true
561 advertise_router_id:
562 - '1.1.1.1'
563 - '2.2.2.2'
564 passive_interfaces:
565 interfaces:
566 - interface: 'Eth1/2'
567 addresses:
568 - '3.3.3.3'
569 - interface: 'Eth1/3'
570 log_adjacency_changes: 'detail'
571 - vrf_name: "Vrf_1"
572 timers:
573 throttle_spf:
574 delay_time: 50
575 initial_hold_time: 20
576 maximum_hold_time: 10
577 max_metric:
578 external_lsa_all: 30
579 log_adjacency_changes: 'brief'
580 default_passive: true
581 non_passive_interfaces:
582 interfaces:
583 - interface: "Eth1/2"
584 addresses:
585 - "2.2.2.2"
586 state: merged
587
588 # After state:
589 # ------------
590 #
591 # sonic# show running-configuration ospf
592 # router ospf vrf Vrf_1
593 # log-adjacency-changes
594 # max-metric router-lsa external-lsa all 30
595 # passive-interface default
596 # timers throttle spf 50 20 10
597 # timers throttle lsa all 300
598 # redistribute bgp metric 15 metric-type 2 route-map RMAP
599 # no passive-interface Eth1/1 2.2.2.2
600 # no passive-interface Eth1/2 2.2.2.2
601 # !
602 # router ospf
603 # ospf router-id 20.20.20.20
604 # distance 30
605 # distance ospf external 20
606 # log-adjacency-changes detail
607 # graceful-restart grace-period 100
608 # graceful-restart helper enable
609 # graceful-restart helper planned-only
610 # graceful-restart helper enable 1.1.1.1
611 # graceful-restart helper enable 2.2.2.2
612 # write-multiplier 20
613 # passive-interface Eth1/2 3.3.3.3
614 # passive-interface Eth1/3
615 # !
616 # sonic#
617
618
619 # Using "replaced" state
620
621 # Before state:
622 # -------------
623 #
624 # sonic# show running-configuration ospf
625 # router ospf vrf Vrf_1
626 # max-metric router-lsa external-lsa all 2
627 # passive-interface default
628 # timers throttle spf 50 20 10
629 # timers throttle lsa all 300
630 # redistribute bgp metric 15 metric-type 2 route-map RMAP
631 # no passive-interface Eth1/1 2.2.2.2
632 # no passive-interface Eth1/2 2.2.2.2
633 # !
634 # router ospf
635 # ospf router-id 20.20.20.20
636 # distance 30
637 # distance ospf external 20
638 # write-multiplier 20
639 # passive-interface Eth1/2 3.3.3.3
640 # passive-interface Eth1/3
641 # !
642 # sonic#
643 # sonic# show running-configuration vrf Vrf_1
644 # !
645 # ip vrf Vrf_1
646 # sonic# show running-configuration ip prefix-list
647 # !
648 # ip prefix-list PRF_LIST seq 1 permit 1.1.1.1/24
649 # ip prefix-list PRF_LIST2 seq 1 permit 1.1.1.1/24
650 # sonic# show running-configuration route-map
651 # !
652 # route-map RMAP permit 1
653 # route-map RMAP2 permit 2
654 # sonic#
655
656 - name: Replace the OSPFv2 vrf default configurations
657 sonic_ospf:
658 config:
659 - vrf_name: 'default'
660 router_id: "20.20.20.20"
661 redistribute:
662 - protocol: "connected"
663 metric: 15
664 metric_type: 2
665 route_map: "RMAP2"
666 - protocol: "default_route"
667 always: true
668 route_map: "RMAP"
669 distance:
670 all: 20
671 abr_type: cisco
672 opaque_lsa_capability: true
673 state: replaced
674
675 # After state:
676 # ------------
677 #
678 # sonic# show running-configuration ospf
679 # router ospf vrf Vrf_1
680 # max-metric router-lsa external-lsa all 2
681 # passive-interface default
682 # timers throttle spf 50 20 10
683 # timers throttle lsa all 300
684 # redistribute bgp metric 15 metric-type 2 route-map RMAP
685 # no passive-interface Eth1/1 2.2.2.2
686 # no passive-interface Eth1/2 2.2.2.2
687 # !
688 # router ospf
689 # capability opaque
690 # ospf router-id 20.20.20.20
691 # default-information originate always route-map RMAP
692 # distance 20
693 # ospf abr-type cisco
694 # redistribute connected metric 15 metric-type 2 route-map RMAP2
695 # !
696
697
698 # Using "overridden" state
699
700 # Before state:
701 # -------------
702 #
703 # sonic# show running-configuration ospf
704 # router ospf vrf Vrf_1
705 # max-metric router-lsa external-lsa all 2
706 # passive-interface default
707 # timers throttle spf 50 20 10
708 # timers throttle lsa all 300
709 # redistribute bgp metric 15 metric-type 2 route-map RMAP
710 # no passive-interface Eth1/1 2.2.2.2
711 # no passive-interface Eth1/2 2.2.2.2
712 # !
713 # router ospf
714 # ospf router-id 20.20.20.20
715 # distance 30
716 # distance ospf external 20
717 # write-multiplier 20
718 # passive-interface Eth1/2 3.3.3.3
719 # passive-interface Eth1/3
720 # !
721 # sonic#
722 # sonic# show running-configuration vrf Vrf_1
723 # !
724 # ip vrf Vrf_1
725 # sonic# show running-configuration ip prefix-list
726 # !
727 # ip prefix-list PRF_LIST seq 1 permit 1.1.1.1/24
728 # ip prefix-list PRF_LIST2 seq 1 permit 1.1.1.1/24
729 # sonic# show running-configuration route-map
730 # !
731 # route-map RMAP permit 1
732 # route-map RMAP2 permit 2
733 # sonic#
734
735 - name: Override the OSPFv2 configurations
736 sonic_ospf:
737 config:
738 - vrf_name: 'default'
739 router_id: "20.20.20.20"
740 redistribute:
741 - protocol: "connected"
742 metric: 15
743 metric_type: 2
744 route_map: "RMAP2"
745 distance:
746 all: 20
747 rfc1583_compatible: true
748 state: overridden
749
750 # After state:
751 # ------------
752 #
753 # sonic# show running-configuration ospf
754 # router ospf
755 # compatible rfc1583
756 # ospf router-id 20.20.20.20
757 # distance 20
758 # redistribute connected metric 15 metric-type 2 route-map RMAP2
759 # !
760 """
761
762 RETURN = """
763 before:
764 description: The configuration prior to the module invocation.
765 returned: always
766 type: list
767 sample: >
768 The configuration returned will always be in the same format
769 as the parameters above.
770 after:
771 description: The resulting configuration module invocation.
772 returned: when changed
773 type: list
774 sample: >
775 The configuration returned will always be in the same format
776 as the parameters above.
777 after_generated:
778 description: The generated configuration module invocation.
779 returned: when C(check_mode)
780 type: list
781 sample: >
782 The configuration returned will always be in the same format
783 as the parameters above.
784 commands:
785 description: The set of commands pushed to the remote device.
786 returned: always
787 type: list
788 sample: ['command 1', 'command 2', 'command 3']
789 """
790
791
792 from ansible.module_utils.basic import AnsibleModule
793 from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.argspec.ospfv2.ospfv2 import Ospfv2Args
794 from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.config.ospfv2.ospfv2 import Ospfv2
795
796
797 def main():
798 """
799 Main entry point for module execution
800
801 :returns: the result from module invocation
802 """
803 module = AnsibleModule(argument_spec=Ospfv2Args.argument_spec,
804 supports_check_mode=True)
805
806 result = Ospfv2(module).execute_module()
807 module.exit_json(**result)
808
809
810 if __name__ == '__main__':
811 main()