Debian Codegraph @

ansible_collections/dellemc/enterprise_sonic/plugins/modules/sonic_dhcp_relay.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_dhcp_relay
9 """
10
11 from __future__ import absolute_import, division, print_function
12 __metaclass__ = type
13
14 DOCUMENTATION = """
15 ---
16 module: sonic_dhcp_relay
17 version_added: '2.1.0'
18 notes:
19 - Tested against Enterprise SONiC Distribution by Dell Technologies.
20 - Supports C(check_mode).
21 short_description: Manage DHCP and DHCPv6 relay configurations on SONiC
22 description:
23 - This module provides configuration management of DHCP and DHCPv6 relay
24 parameters on Layer 3 interfaces of devices running SONiC.
25 - Layer 3 interface and VRF name need to be created earlier in the device.
26 author: 'Arun Saravanan Balachandran (@ArunSaravananBalachandran)'
27 options:
28 config:
29 description:
30 - Specifies the DHCP and DHCPv6 relay configurations.
31 type: list
32 elements: dict
33 suboptions:
34 name:
35 description:
36 - Full name of the Layer 3 interface, i.e. Eth1/1.
37 type: str
38 required: true
39 ipv4:
40 description:
41 - DHCP relay configurations to be set for the interface mentioned in name option.
42 type: dict
43 suboptions:
44 server_addresses:
45 description:
46 - List of DHCP server IPv4 addresses.
47 type: list
48 elements: dict
49 suboptions:
50 address:
51 description:
52 - IPv4 address of the DHCP server.
53 type: str
54 vrf_name:
55 description:
56 - Specifies name of the VRF in which the DHCP server resides.
57 - This option is not used with state I(deleted).
58 type: str
59 source_interface:
60 description:
61 - Specifies the DHCP relay source interface.
62 type: str
63 max_hop_count:
64 description:
65 - Specifies the maximum hop count for DHCP relay packets.
66 - The range is from 1 to 16.
67 type: int
68 link_select:
69 description:
70 - Enable link selection suboption.
71 type: bool
72 vrf_select:
73 description:
74 - Enable VRF selection suboption.
75 type: bool
76 circuit_id:
77 description:
78 - Specifies the DHCP relay circuit-id format.
79 - C(%h:%p) - Hostname followed by interface name eg. sonic:Vlan100
80 - C(%i) - Name of the physical interface eg. Eth1/2
81 - C(%p) - Name of the interface eg. Vlan100
82 type: str
83 choices:
84 - '%h:%p'
85 - '%i'
86 - '%p'
87 policy_action:
88 description:
89 - Specifies the policy for handling of DHCP relay options.
90 type: str
91 choices:
92 - append
93 - discard
94 - replace
95 ipv6:
96 description:
97 - DHCPv6 relay configurations to be set for the interface mentioned in name option.
98 type: dict
99 suboptions:
100 server_addresses:
101 description:
102 - List of DHCPv6 server IPv6 addresses.
103 type: list
104 elements: dict
105 suboptions:
106 address:
107 description:
108 - IPv6 address of the DHCPv6 server.
109 type: str
110 vrf_name:
111 description:
112 - Specifies name of the VRF in which the DHCPv6 server resides.
113 - This option is used only with state I(merged).
114 type: str
115 source_interface:
116 description:
117 - Specifies the DHCPv6 relay source interface.
118 type: str
119 max_hop_count:
120 description:
121 - Specifies the maximum hop count for DHCPv6 relay packets.
122 - The range is from 1 to 16.
123 type: int
124 vrf_select:
125 description:
126 - Enable VRF selection suboption.
127 type: bool
128 state:
129 description:
130 - The state of the configuration after module completion.
131 - C(merged) - Merges provided DHCP and DHCPv6 relay configuration with on-device configuration.
132 - C(deleted) - Deletes on-device DHCP and DHCPv6 relay configuration.
133 - C(replaced) - Replaces on-device DHCP and DHCPv6 relay configuration of the specified interfaces with provided configuration.
134 - C(overridden) - Overrides all on-device DHCP and DHCPv6 relay configurations with the provided configuration.
135 type: str
136 choices:
137 - merged
138 - deleted
139 - replaced
140 - overridden
141 default: merged
142 """
143
144 EXAMPLES = """
145 # Using "deleted" state
146 #
147 # Before state:
148 # -------------
149 #
150 # sonic# show running-configuration interface
151 # !
152 # interface Eth1/1
153 # mtu 9100
154 # speed 400000
155 # fec RS
156 # no shutdown
157 # ip address 81.1.1.1/24
158 # ip dhcp-relay 91.1.1.1 92.1.1.1 vrf VrfReg1
159 # ip dhcp-relay max-hop-count 5
160 # ip dhcp-relay vrf-select
161 # ip dhcp-relay policy-action append
162 # ipv6 address 81::1/24
163 # ipv6 dhcp-relay 91::1 92::1
164 # ipv6 dhcp-relay max-hop-count 5
165 # !
166 # interface Eth1/2
167 # mtu 9100
168 # speed 400000
169 # fec RS
170 # no shutdown
171 # ip address 61.1.1.1/24
172 # ip dhcp-relay 71.1.1.1 72.1.1.1 73.1.1.1
173 # ip dhcp-relay source-interface Vlan100
174 # ip dhcp-relay link-select
175 # ip dhcp-relay circuit-id %h:%p
176 # !
177
178 - name: Delete DHCP and DHCPv6 relay configurations
179 dellemc.enterprise_sonic.sonic_dhcp_relay:
180 config:
181 - name: 'Eth1/1'
182 ipv4:
183 server_addresses:
184 - address: '92.1.1.1'
185 vrf_select: true
186 max_hop_count: 5
187 ipv6:
188 server_addresses:
189 - address: '91::1'
190 - address: '92::1'
191 - name: 'Eth1/2'
192 ipv4:
193 server_addresses:
194 - address: '71.1.1.1'
195 - address: '72.1.1.1'
196 source_interface: 'Vlan100'
197 link_select: true
198 circuit_id: '%h:%p'
199 state: deleted
200
201 # After state:
202 # ------------
203 #
204 # sonic# show running-configuration interface
205 # !
206 # interface Eth1/1
207 # mtu 9100
208 # speed 400000
209 # fec RS
210 # no shutdown
211 # ip address 81.1.1.1/24
212 # ip dhcp-relay 91.1.1.1 vrf VrfReg1
213 # ip dhcp-relay policy-action append
214 # ipv6 address 81::1/24
215 # !
216 # interface Eth1/2
217 # mtu 9100
218 # speed 400000
219 # fec RS
220 # no shutdown
221 # ip address 61.1.1.1/24
222 # ip dhcp-relay 73.1.1.1
223 # !
224
225
226 # Using "deleted" state
227 #
228 # NOTE: Support is provided in the dhcp_relay resource module for deletion of all attributes for a
229 # given address family (IPv4 or IPv6) by using a "special" YAML sequence specifying a server address list
230 # containing a single "blank" IP address under the target address family. The following example shows
231 # a task using this syntax for deletion of all DHCP (IPv4) configurations for an interface, but the
232 # equivalent syntax is supported for DHCPv6 (IPv6) as well.
233 #
234 # Before state:
235 # -------------
236 #
237 # sonic# show running-configuration interface
238 # !
239 # interface Eth1/1
240 # mtu 9100
241 # speed 400000
242 # fec RS
243 # no shutdown
244 # ip address 81.1.1.1/24
245 # ip dhcp-relay 91.1.1.1 92.1.1.1 vrf VrfReg1
246 # ip dhcp-relay max-hop-count 5
247 # ip dhcp-relay vrf-select
248 # ip dhcp-relay policy-action append
249 # ipv6 address 81::1/24
250 # ipv6 dhcp-relay 91::1 92::1
251 # ipv6 dhcp-relay max-hop-count 5
252 # !
253 # interface Eth1/2
254 # mtu 9100
255 # speed 400000
256 # fec RS
257 # no shutdown
258 # ip address 61.1.1.1/24
259 # ip dhcp-relay 71.1.1.1 72.1.1.1 73.1.1.1
260 # ip dhcp-relay source-interface Vlan100
261 # ip dhcp-relay link-select
262 # ip dhcp-relay circuit-id %h:%p
263 # !
264
265 - name: Delete all IPv4 DHCP relay configurations for interface Eth1/1
266 dellemc.enterprise_sonic.sonic_dhcp_relay:
267 config:
268 - name: 'Eth1/1'
269 ipv4:
270 server_addresses:
271 - address:
272 state: deleted
273
274 # After state:
275 # ------------
276 #
277 # sonic# show running-configuration interface
278 # !
279 # interface Eth1/1
280 # mtu 9100
281 # speed 400000
282 # fec RS
283 # no shutdown
284 # ip address 81.1.1.1/24
285 # ipv6 address 81::1/24
286 # ipv6 dhcp-relay 91::1 92::1
287 # ipv6 dhcp-relay max-hop-count 5
288 # !
289 # interface Eth1/2
290 # mtu 9100
291 # speed 400000
292 # fec RS
293 # no shutdown
294 # ip address 61.1.1.1/24
295 # ip dhcp-relay 71.1.1.1 72.1.1.1 73.1.1.1
296 # ip dhcp-relay source-interface Vlan100
297 # ip dhcp-relay link-select
298 # ip dhcp-relay circuit-id %h:%p
299 # !
300
301
302 # Using "deleted" state
303 #
304 # Before state:
305 # -------------
306 #
307 # sonic# show running-configuration interface
308 # !
309 # interface Eth1/1
310 # mtu 9100
311 # speed 400000
312 # fec RS
313 # no shutdown
314 # ip address 81.1.1.1/24
315 # ip dhcp-relay 91.1.1.1 92.1.1.1 vrf VrfReg1
316 # ip dhcp-relay max-hop-count 5
317 # ip dhcp-relay vrf-select
318 # ip dhcp-relay policy-action append
319 # ipv6 address 81::1/24
320 # ipv6 dhcp-relay 91::1 92::1
321 # ipv6 dhcp-relay max-hop-count 5
322 # !
323 # interface Eth1/2
324 # mtu 9100
325 # speed 400000
326 # fec RS
327 # no shutdown
328 # ip address 61.1.1.1/24
329 # ip dhcp-relay 71.1.1.1 72.1.1.1 73.1.1.1
330 # ip dhcp-relay source-interface Vlan100
331 # ip dhcp-relay link-select
332 # ip dhcp-relay circuit-id %h:%p
333 # !
334
335 - name: Delete all DHCP and DHCPv6 relay configurations for interface Eth1/1
336 dellemc.enterprise_sonic.sonic_dhcp_relay:
337 config:
338 - name: 'Eth1/1'
339 state: deleted
340
341 # After state:
342 # ------------
343 #
344 # sonic# show running-configuration interface
345 # !
346 # interface Eth1/1
347 # mtu 9100
348 # speed 400000
349 # fec RS
350 # no shutdown
351 # ip address 81.1.1.1/24
352 # ipv6 address 81::1/24
353 # !
354 # interface Eth1/2
355 # mtu 9100
356 # speed 400000
357 # fec RS
358 # no shutdown
359 # ip address 61.1.1.1/24
360 # ip dhcp-relay 71.1.1.1 72.1.1.1 73.1.1.1
361 # ip dhcp-relay source-interface Vlan100
362 # ip dhcp-relay link-select
363 # ip dhcp-relay circuit-id %h:%p
364 # !
365
366
367 # Using "deleted" state
368 #
369 # Before state:
370 # -------------
371 #
372 # sonic# show running-configuration interface
373 # !
374 # interface Eth1/1
375 # mtu 9100
376 # speed 400000
377 # fec RS
378 # no shutdown
379 # ip address 81.1.1.1/24
380 # ip dhcp-relay 91.1.1.1 92.1.1.1 vrf VrfReg1
381 # ip dhcp-relay max-hop-count 5
382 # ip dhcp-relay vrf-select
383 # ip dhcp-relay policy-action append
384 # ipv6 address 81::1/24
385 # ipv6 dhcp-relay 91::1 92::1
386 # ipv6 dhcp-relay max-hop-count 5
387 # !
388 # interface Eth1/2
389 # mtu 9100
390 # speed 400000
391 # fec RS
392 # no shutdown
393 # ip address 61.1.1.1/24
394 # ip dhcp-relay 71.1.1.1 72.1.1.1 73.1.1.1
395 # ip dhcp-relay source-interface Vlan100
396 # ip dhcp-relay link-select
397 # ip dhcp-relay circuit-id %h:%p
398 # !
399
400 - name: Delete all DHCP and DHCPv6 relay configurations
401 dellemc.enterprise_sonic.sonic_dhcp_relay:
402 config:
403 state: deleted
404
405 # After state:
406 # ------------
407 #
408 # sonic# show running-configuration interface
409 # !
410 # interface Eth1/1
411 # mtu 9100
412 # speed 400000
413 # fec RS
414 # no shutdown
415 # ip address 81.1.1.1/24
416 # ipv6 address 81::1/24
417 # !
418 # interface Eth1/2
419 # mtu 9100
420 # speed 400000
421 # fec RS
422 # no shutdown
423 # ip address 61.1.1.1/24
424 # !
425
426
427 # Using "merged" state
428 #
429 # Before state:
430 # -------------
431 #
432 # sonic# show running-configuration interface
433 # !
434 # interface Eth1/1
435 # mtu 9100
436 # speed 400000
437 # fec RS
438 # no shutdown
439 # ip address 81.1.1.1/24
440 # ipv6 address 81::1/24
441 # !
442 # interface Eth1/2
443 # mtu 9100
444 # speed 400000
445 # fec RS
446 # no shutdown
447 # ip address 61.1.1.1/24
448 # ip dhcp-relay 71.1.1.1 72.1.1.1
449 # !
450
451 - name: Add DHCP and DHCPv6 relay configurations
452 dellemc.enterprise_sonic.sonic_dhcp_relay:
453 config:
454 - name: 'Eth1/1'
455 ipv4:
456 server_addresses:
457 - address: '91.1.1.1'
458 - address: '92.1.1.1'
459 vrf_name: 'VrfReg1'
460 vrf_select: true
461 max_hop_count: 5
462 policy_action: 'append'
463 ipv6:
464 server_addresses:
465 - address: '91::1'
466 - address: '92::1'
467 max_hop_count: 5
468 - name: 'Eth1/2'
469 ipv4:
470 server_addresses:
471 - address: '73.1.1.1'
472 source_interface: 'Vlan100'
473 link_select: true
474 circuit_id: '%h:%p'
475 state: merged
476
477 # After state:
478 # ------------
479 #
480 # sonic# show running-configuration interface
481 # !
482 # interface Eth1/1
483 # mtu 9100
484 # speed 400000
485 # fec RS
486 # no shutdown
487 # ip address 81.1.1.1/24
488 # ip dhcp-relay 91.1.1.1 92.1.1.1 vrf VrfReg1
489 # ip dhcp-relay max-hop-count 5
490 # ip dhcp-relay vrf-select
491 # ip dhcp-relay policy-action append
492 # ipv6 address 81::1/24
493 # ipv6 dhcp-relay 91::1 92::1
494 # ipv6 dhcp-relay max-hop-count 5
495 # !
496 # interface Eth1/2
497 # mtu 9100
498 # speed 400000
499 # fec RS
500 # no shutdown
501 # ip address 61.1.1.1/24
502 # ip dhcp-relay 71.1.1.1 72.1.1.1 73.1.1.1
503 # ip dhcp-relay source-interface Vlan100
504 # ip dhcp-relay link-select
505 # ip dhcp-relay circuit-id %h:%p
506 # !
507
508
509 # Using "replaced" state
510 #
511 # Before state:
512 # -------------
513 #
514 # sonic# show running-configuration interface
515 # !
516 # interface Eth1/1
517 # mtu 9100
518 # speed 400000
519 # fec RS
520 # no shutdown
521 # ip address 81.1.1.1/24
522 # ip dhcp-relay 91.1.1.1 92.1.1.1 vrf VrfReg1
523 # ip dhcp-relay max-hop-count 5
524 # ip dhcp-relay vrf-select
525 # ip dhcp-relay policy-action append
526 # ipv6 address 81::1/24
527 # ipv6 dhcp-relay 91::1 92::1
528 # ipv6 dhcp-relay max-hop-count 5
529 # !
530 # interface Eth1/2
531 # mtu 9100
532 # speed 400000
533 # fec RS
534 # no shutdown
535 # ip address 61.1.1.1/24
536 # ip dhcp-relay 71.1.1.1 72.1.1.1 73.1.1.1
537 # ip dhcp-relay source-interface Vlan100
538 # ip dhcp-relay link-select
539 # ip dhcp-relay circuit-id %h:%p
540 # ipv6 address 61::1/24
541 # ipv6 dhcp-relay 71::1 72::1
542 # !
543 # interface Eth1/3
544 # mtu 9100
545 # speed 400000
546 # fec RS
547 # shutdown
548 # ip address 41.1.1.1/24
549 # ip dhcp-relay 51.1.1.1 52.1.1.1
550 # ip dhcp-relay circuit-id %h:%p
551 # ipv6 address 41::1/24
552 # ipv6 dhcp-relay 51::1 52::1
553 # !
554
555 - name: Replace DHCP and DHCPv6 relay configurations of specified interfaces
556 dellemc.enterprise_sonic.sonic_dhcp_relay:
557 config:
558 - name: 'Eth1/1'
559 ipv4:
560 server_addresses:
561 - address: '91.1.1.1'
562 - address: '93.1.1.1'
563 - address: '95.1.1.1'
564 vrf_name: 'VrfReg1'
565 vrf_select: true
566 ipv6:
567 server_addresses:
568 - address: '93::1'
569 - address: '94::1'
570 source_interface: 'Vlan100'
571 - name: 'Eth1/2'
572 ipv4:
573 server_addresses:
574 - address: '73.1.1.1'
575 circuit_id: '%h:%p'
576 state: replaced
577
578 # After state:
579 # ------------
580 #
581 # sonic# show running-configuration interface
582 # !
583 # interface Eth1/1
584 # mtu 9100
585 # speed 400000
586 # fec RS
587 # no shutdown
588 # ip address 81.1.1.1/24
589 # ip dhcp-relay 91.1.1.1 93.1.1.1 95.1.1.1 vrf VrfReg1
590 # ip dhcp-relay vrf-select
591 # ipv6 address 81::1/24
592 # ipv6 dhcp-relay 93::1 94::1
593 # ipv6 dhcp-relay source-interface Vlan100
594 # !
595 # interface Eth1/2
596 # mtu 9100
597 # speed 400000
598 # fec RS
599 # no shutdown
600 # ip address 61.1.1.1/24
601 # ip dhcp-relay 73.1.1.1
602 # ip dhcp-relay circuit-id %h:%p
603 # ipv6 address 61::1/24
604 # !
605 # interface Eth1/3
606 # mtu 9100
607 # speed 400000
608 # fec RS
609 # shutdown
610 # ip address 41.1.1.1/24
611 # ip dhcp-relay 51.1.1.1 52.1.1.1
612 # ip dhcp-relay circuit-id %h:%p
613 # ipv6 address 41::1/24
614 # ipv6 dhcp-relay 51::1 52::1
615 # !
616
617
618 # Using "overridden" state
619 #
620 # Before state:
621 # -------------
622 #
623 # sonic# show running-configuration interface
624 # !
625 # interface Eth1/1
626 # mtu 9100
627 # speed 400000
628 # fec RS
629 # no shutdown
630 # ip address 81.1.1.1/24
631 # ip dhcp-relay 91.1.1.1 92.1.1.1 vrf VrfReg1
632 # ip dhcp-relay max-hop-count 5
633 # ip dhcp-relay vrf-select
634 # ip dhcp-relay policy-action append
635 # ipv6 address 81::1/24
636 # ipv6 dhcp-relay 91::1 92::1
637 # ipv6 dhcp-relay max-hop-count 5
638 # !
639 # interface Eth1/2
640 # mtu 9100
641 # speed 400000
642 # fec RS
643 # no shutdown
644 # ip address 61.1.1.1/24
645 # ip dhcp-relay 71.1.1.1 72.1.1.1 73.1.1.1
646 # ip dhcp-relay source-interface Vlan100
647 # ip dhcp-relay link-select
648 # ip dhcp-relay circuit-id %h:%p
649 # ipv6 address 61::1/24
650 # ipv6 dhcp-relay 71::1 72::1
651 # !
652 # interface Eth1/3
653 # mtu 9100
654 # speed 400000
655 # fec RS
656 # shutdown
657 # ip address 41.1.1.1/24
658 # ip dhcp-relay 51.1.1.1 52.1.1.1
659 # ip dhcp-relay circuit-id %h:%p
660 # ipv6 address 41::1/24
661 # ipv6 dhcp-relay 51::1 52::1
662 # !
663
664 - name: Override DHCP and DHCPv6 relay configurations
665 dellemc.enterprise_sonic.sonic_dhcp_relay:
666 config:
667 - name: 'Eth1/1'
668 ipv4:
669 server_addresses:
670 - address: '91.1.1.1'
671 - address: '93.1.1.1'
672 - address: '95.1.1.1'
673 vrf_name: 'VrfReg1'
674 vrf_select: true
675 ipv6:
676 server_addresses:
677 - address: '93::1'
678 - address: '94::1'
679 source_interface: 'Vlan100'
680 - name: 'Eth1/2'
681 ipv4:
682 server_addresses:
683 - address: '73.1.1.1'
684 circuit_id: '%h:%p'
685 state: overridden
686
687 # After state:
688 # ------------
689 #
690 # sonic# show running-configuration interface
691 # !
692 # interface Eth1/1
693 # mtu 9100
694 # speed 400000
695 # fec RS
696 # no shutdown
697 # ip address 81.1.1.1/24
698 # ip dhcp-relay 91.1.1.1 93.1.1.1 95.1.1.1 vrf VrfReg1
699 # ip dhcp-relay vrf-select
700 # ipv6 address 81::1/24
701 # ipv6 dhcp-relay 93::1 94::1
702 # ipv6 dhcp-relay source-interface Vlan100
703 # !
704 # interface Eth1/2
705 # mtu 9100
706 # speed 400000
707 # fec RS
708 # no shutdown
709 # ip address 61.1.1.1/24
710 # ip dhcp-relay 73.1.1.1
711 # ip dhcp-relay circuit-id %h:%p
712 # ipv6 address 61::1/24
713 # !
714 # interface Eth1/3
715 # mtu 9100
716 # speed 400000
717 # fec RS
718 # shutdown
719 # ip address 41.1.1.1/24
720 # ipv6 address 41::1/24
721 # !
722 """
723
724 RETURN = """
725 before:
726 description: The configuration prior to the module invocation.
727 returned: always
728 type: list
729 sample: >
730 The configuration returned will always be in the same format
731 as the parameters above.
732 after:
733 description: The resulting configuration module invocation.
734 returned: when changed
735 type: list
736 sample: >
737 The configuration returned will always be in the same format
738 as the parameters above.
739 after_generated:
740 description: The generated configuration module invocation.
741 returned: when C(check_mode)
742 type: list
743 sample: >
744 The configuration returned will always be in the same format
745 as the parameters above.
746 commands:
747 description: The set of commands pushed to the remote device.
748 returned: always
749 type: list
750 sample: ['command 1', 'command 2', 'command 3']
751 """
752
753
754 from ansible.module_utils.basic import AnsibleModule
755 from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.argspec.dhcp_relay.dhcp_relay import Dhcp_relayArgs
756 from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.config.dhcp_relay.dhcp_relay import Dhcp_relay
757
758
759 def main():
760 """
761 Main entry point for module execution
762
763 :returns: the result form module invocation
764 """
765 module = AnsibleModule(argument_spec=Dhcp_relayArgs.argument_spec,
766 supports_check_mode=True)
767
768 result = Dhcp_relay(module).execute_module()
769 module.exit_json(**result)
770
771
772 if __name__ == '__main__':
773 main()