Debian Codegraph @

ansible_collections/dellemc/enterprise_sonic/plugins/modules/sonic_ntp.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_ntp
9 """
10
11 from __future__ import absolute_import, division, print_function
12 __metaclass__ = type
13
14
15 DOCUMENTATION = """
16 ---
17 module: sonic_ntp
18 version_added: 2.0.0
19 notes:
20 - Supports C(check_mode).
21 short_description: Manage NTP configuration on SONiC.
22 description:
23 - This module provides configuration management of NTP for devices running SONiC.
24 author: "M. Zhang (@mingjunzhang2019)"
25 options:
26 config:
27 description:
28 - Specifies NTP related configurations.
29 type: dict
30 suboptions:
31 source_interfaces:
32 type: list
33 elements: str
34 description:
35 - List of names of NTP source interfaces.
36 enable_ntp_auth:
37 type: bool
38 description:
39 - Enable or disable NTP authentication.
40 trusted_keys:
41 type: list
42 elements: int
43 description:
44 - List of trusted NTP authentication keys.
45 vrf:
46 type: str
47 description:
48 - VRF name on which NTP is enabled.
49 servers:
50 type: list
51 elements: dict
52 description:
53 - List of NTP servers.
54 - minpoll and maxpoll are required to be configured together.
55 suboptions:
56 address:
57 type: str
58 description:
59 - IPv4/IPv6 address or host name of NTP server.
60 required: true
61 key_id:
62 type: int
63 description:
64 - NTP authentication key used by server.
65 - Key_id can not be deleted.
66 minpoll:
67 type: int
68 description:
69 - Minimum poll interval to poll NTP server.
70 - minpoll can not be deleted.
71 maxpoll:
72 type: int
73 description:
74 - Maximum poll interval to poll NTP server.
75 - maxpoll can not be deleted.
76 prefer:
77 type: bool
78 description:
79 - Indicates whether this server should be preferred.
80 - prefer can not be deleted.
81 ntp_keys:
82 type: list
83 elements: dict
84 description:
85 - List of NTP authentication keys.
86 suboptions:
87 key_id:
88 type: int
89 description:
90 - NTP authentication key identifier.
91 required: true
92 key_type:
93 type: str
94 description:
95 - NTP authentication key type.
96 - key_type can not be deleted.
97 - When "state" is "merged", "key_type" is required.
98 choices:
99 - NTP_AUTH_SHA1
100 - NTP_AUTH_MD5
101 - NTP_AUTH_SHA2_256
102 key_value:
103 type: str
104 description:
105 - NTP authentication key value.
106 - key_value can not be deleted.
107 - When "state" is "merged", "key_value" is required.
108 encrypted:
109 type: bool
110 description:
111 - NTP authentication key_value is encrypted.
112 - encrypted can not be deleted.
113 - When "state" is "merged", "encrypted" is required.
114
115 state:
116 description:
117 - The state of the configuration after module completion.
118 type: str
119 choices:
120 - merged
121 - replaced
122 - overridden
123 - deleted
124 default: merged
125 """
126
127 EXAMPLES = """
128 # Using "deleted" state
129 #
130 # Before state:
131 # -------------
132 #
133 # sonic# show ntp server
134 # ----------------------------------------------------------------------------
135 # NTP Servers minpoll maxpoll Prefer Authentication key ID
136 # ----------------------------------------------------------------------------
137 # 10.11.0.1 6 10 False
138 # 10.11.0.2 5 9 False
139 # dell.com 6 9 False
140 # dell.org 7 10 True
141 #
142 - name: Delete NTP server configuration
143 sonic_ntp:
144 config:
145 servers:
146 - address: 10.11.0.2
147 - address: dell.org
148 state: deleted
149
150 # After state:
151 # ------------
152 #
153 # sonic# show ntp server
154 # ----------------------------------------------------------------------------
155 # NTP Servers minpoll maxpoll Prefer Authentication key ID
156 # ----------------------------------------------------------------------------
157 # 10.11.0.1 6 10 False
158 # dell.com 6 9 False
159 #
160 #
161 # Using "deleted" state
162 #
163 # Before state:
164 # -------------
165 #
166 # sonic# show ntp global
167 # ----------------------------------------------
168 # NTP Global Configuration
169 # ----------------------------------------------
170 # NTP source-interfaces: Ethernet0, Ethernet4, Ethernet8, Ethernet16
171 #
172 - name: Delete NTP source-interface configuration
173 sonic_ntp:
174 config:
175 source_interfaces:
176 - Ethernet8
177 - Ethernet16
178 state: deleted
179
180 # After state:
181 # ------------
182 #
183 # sonic# show ntp global
184 # ----------------------------------------------
185 # NTP Global Configuration
186 # ----------------------------------------------
187 # NTP source-interfaces: Ethernet0, Ethernet4
188 #
189 #
190 # Using "deleted" state
191 #
192 # Before state:
193 # -------------
194 #
195 # sonic# show running-configuration | grep ntp
196 # ntp authentication-key 8 sha1 U2FsdGVkX1/NpJrdOeyMeUHEkSohY6azY9VwbAqXRTY= encrypted
197 # ntp authentication-key 10 md5 U2FsdGVkX1/Gxds/5pscCvIKbVngGaKka4SQineS51Y= encrypted
198 # ntp authentication-key 20 sha2-256 U2FsdGVkX1/eAzKj1teKhYWD7tnzOsYOijGeFAT0rKM= encrypted
199 #
200 - name: Delete NTP key configuration
201 sonic_ntp:
202 config:
203 ntp_keys:
204 - key_id: 10
205 - key_id: 20
206 state: deleted
207 #
208 # After state:
209 # ------------
210 #
211 # sonic# show running-configuration | grep ntp
212 # ntp authentication-key 8 sha1 U2FsdGVkX1/NpJrdOeyMeUHEkSohY6azY9VwbAqXRTY= encrypted
213 #
214 #
215 # Using "merged" state
216 #
217 # Before state:
218 # -------------
219 #
220 # sonic# show ntp server
221 # ----------------------------------------------------------------------------
222 # NTP Servers minpoll maxpoll Prefer Authentication key ID
223 # ----------------------------------------------------------------------------
224 # 10.11.0.1 6 10 False
225 # dell.com 6 9 False
226 #
227 - name: Merge NTP server configuration
228 sonic_ntp:
229 config:
230 servers:
231 - address: 10.11.0.2
232 minpoll: 5
233 - address: dell.org
234 minpoll: 7
235 maxpoll: 10
236 prefer: true
237 state: merged
238
239 # After state:
240 # ------------
241 #
242 # sonic# show ntp server
243 # ----------------------------------------------------------------------------
244 # NTP Servers minpoll maxpoll Prefer Authentication key ID
245 # ----------------------------------------------------------------------------
246 # 10.11.0.1 6 10 Flase
247 # 10.11.0.2 5 10 Flase
248 # dell.com 6 9 Flase
249 # dell.org 7 10 True
250 #
251 #
252 # Using "merged" state
253 #
254 # Before state:
255 # -------------
256 #
257 # sonic# show ntp global
258 # ----------------------------------------------
259 # NTP Global Configuration
260 # ----------------------------------------------
261 # NTP source-interfaces: Ethernet0, Ethernet4
262 #
263 - name: Merge NTP source-interface configuration
264 sonic_ntp:
265 config:
266 source_interfaces:
267 - Ethernet8
268 - Ethernet16
269 state: merged
270
271 # After state:
272 # ------------
273 #
274 # sonic# show ntp global
275 # ----------------------------------------------
276 # NTP Global Configuration
277 # ----------------------------------------------
278 # NTP source-interfaces: Ethernet0, Ethernet4, Ethernet8, Ethernet16
279 #
280 #
281 # Using "merged" state
282 #
283 # Before state:
284 # -------------
285 #
286 # sonic# show running-configuration | grep ntp
287 # ntp authentication-key 8 sha1 U2FsdGVkX1/NpJrdOeyMeUHEkSohY6azY9VwbAqXRTY= encrypted
288 #
289 - name: Merge NTP key configuration
290 sonic_ntp:
291 config:
292 ntp_keys:
293 - key_id: 10
294 key_type: NTP_AUTH_MD5
295 key_value: dellemc10
296 encrypted: false
297 - key_id: 20
298 key_type: NTP_AUTH_SHA2_256
299 key_value: dellemc20
300 encrypted: false
301 state: merged
302 #
303 # After state:
304 # ------------
305 #
306 # sonic# show running-configuration | grep ntp
307 # ntp authentication-key 8 sha1 U2FsdGVkX1/NpJrdOeyMeUHEkSohY6azY9VwbAqXRTY= encrypted
308 # ntp authentication-key 10 md5 U2FsdGVkX1/Gxds/5pscCvIKbVngGaKka4SQineS51Y= encrypted
309 # ntp authentication-key 20 sha2-256 U2FsdGVkX1/eAzKj1teKhYWD7tnzOsYOijGeFAT0rKM= encrypted
310 #
311 # Using "replaced" state
312 #
313 # Before state:
314 # -------------
315 #
316 # sonic# show ntp server
317 # ----------------------------------------------------------------------------
318 # NTP Servers minpoll maxpoll Prefer Authentication key ID
319 # ----------------------------------------------------------------------------
320 # 10.11.0.1 6 10 False
321 # dell.com 6 9 False
322 #
323 - name: Replace NTP server configuration
324 sonic_ntp:
325 config:
326 servers:
327 - address: 10.11.0.2
328 minpoll: 5
329 maxpoll: 9
330 - address: dell.com
331 minpoll: 7
332 maxpoll: 10
333 prefer: true
334 state: replaced
335 #
336 # After state:
337 # ------------
338 #
339 # sonic# show ntp server
340 # ----------------------------------------------------------------------------
341 # NTP Servers minpoll maxpoll Prefer Authentication key ID
342 # ----------------------------------------------------------------------------
343 # 10.11.0.1 6 10 False
344 # 10.11.0.2 5 9 False
345 # dell.com 7 10 True
346 #
347 # Using "overridden" state
348 #
349 # Before state:
350 # -------------
351 #
352 # sonic# show ntp server
353 # ----------------------------------------------------------------------------
354 # NTP Servers minpoll maxpoll Prefer Authentication key ID
355 # ----------------------------------------------------------------------------
356 # 10.11.0.1 6 10 False
357 # dell.com 6 9 False
358 #
359 # sonic# show ntp global
360 # ----------------------------------------------
361 # NTP Global Configuration
362 # ----------------------------------------------
363 # NTP source-interfaces: Ethernet0, Ethernet4
364 #
365 - name: Overridden NTP configuration
366 sonic_ntp:
367 config:
368 servers:
369 - address: 10.11.0.2
370 minpoll: 5
371 - address: dell.com
372 minpoll: 7
373 maxpoll: 10
374 prefer: true
375 state: overridden
376 #
377 # After state:
378 # ------------
379 #
380 # After state:
381 # ------------
382 #
383 # sonic# show ntp server
384 # ----------------------------------------------------------------------------
385 # NTP Servers minpoll maxpoll Prefer Authentication key ID
386 # ----------------------------------------------------------------------------
387 # 10.11.0.2 5 10 False
388 # dell.com 7 10 True
389 #
390 # sonic# show ntp global
391 #
392 """
393
394 RETURN = """
395 before:
396 description: The configuration prior to the module invocation.
397 returned: always
398 type: list
399 sample: >
400 The configuration returned will always be in the same format
401 as the parameters above.
402 after:
403 description: The resulting configuration module invocation.
404 returned: when changed
405 type: list
406 sample: >
407 The configuration returned will always be in the same format
408 as the parameters above.
409 after_generated:
410 description: The generated configuration module invocation.
411 returned: when C(check_mode)
412 type: list
413 sample: >
414 The configuration returned will always be in the same format
415 as the parameters above.
416 commands:
417 description: The set of commands pushed to the remote device.
418 returned: always
419 type: list
420 sample: ['command 1', 'command 2', 'command 3']
421 """
422
423
424 from ansible.module_utils.basic import AnsibleModule
425 from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.argspec.ntp.ntp import NtpArgs
426 from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.config.ntp.ntp import Ntp
427
428
429 def main():
430 """
431 Main entry point for module execution
432
433 :returns: the result form module invocation
434 """
435 module = AnsibleModule(argument_spec=NtpArgs.argument_spec,
436 supports_check_mode=True)
437
438 result = Ntp(module).execute_module()
439 module.exit_json(**result)
440
441
442 if __name__ == '__main__':
443 main()