Debian Codegraph @

ansible_collections/dellemc/enterprise_sonic/plugins/modules/sonic_ssh.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_ssh
9 """
10
11 from __future__ import absolute_import, division, print_function
12 __metaclass__ = type
13
14 DOCUMENTATION = """
15 ---
16 module: sonic_ssh
17 version_added: '3.0.0'
18 notes:
19 - Tested against Enterprise SONiC Distribution by Dell Technologies.
20 - Supports C(check_mode).
21
22 short_description: Manage SSH configurations on SONiC
23 description:
24 - This module provides SSH configuration management to specify the
25 algorithms used for SSH connection in devices running SONiC.
26 author: 'Balasubramaniam Koundappa(@balasubramaniam-k)'
27 options:
28 config:
29 description:
30 - SSH clients and servers use the following configurations for SSH connections.
31 type: dict
32 suboptions:
33 client:
34 description:
35 - SSH client configuration
36 type: dict
37 suboptions:
38 cipher:
39 description:
40 - Cipher algorithm used in SSH connection for encryption.
41 When configured, this value is used by SSH clients
42 which communicate with the server.
43 - Specify as a comma separated list.
44 - Options are aes128-ctr, aes192-ctr, aes256-ctr
45 - [email protected], [email protected]
46 - and [email protected]
47 type: str
48 kex:
49 description:
50 - KEX algorithm used in SSH connection for key exchange.
51 When configured, this value is used by SSH clients
52 which communicate with the server.
53 - Specify as a comma separated list.
54 - Options are curve25519-sha256, [email protected]
55 - ecdh-sha2-nistp256, ecdh-sha2-nistp384, ecdh-sha2-nistp521,
56 - diffie-hellman-group-exchange-sha256,
57 - diffie-hellman-group16-sha512,
58 - diffie-hellman-group18-sha512 and
59 - diffie-hellman-group14-sha256
60 type: str
61 mac:
62 description:
63 - MAC algorithm used in SSH connection for generating and
64 verifying Message Authentication Codes. When configured,
65 this value is used by SSH clients which communicate with
66 the server.
67 - Specify as a comma separated list.
68 - Options are [email protected],
69 - [email protected],
70 - [email protected], [email protected],
71 - hmac-sha2-256 and hmac-sha2-512
72 type: str
73 state:
74 description:
75 - The state specifies the type of configuration update to be performed on the device.
76 - If the state is "merged", merge specified attributes with existing configured attributes.
77 - For "deleted", delete the specified attributes from existing configuration.
78 - For "replaced", replace on-device SSH configuration with the specified configuration.
79 - For "overridden", override on-device SSH configurations with the specified configuration.
80 type: str
81 choices:
82 - merged
83 - deleted
84 - replaced
85 - overridden
86 default: merged
87 """
88
89 EXAMPLES = """
90 # Using "deleted" state
91 #
92 # Before state:
93 # -------------
94 #
95 # sonic# show running-configuration | grep "ip ssh client"
96 # ip ssh client ciphers aes192-ctr,[email protected]
97 # ip ssh client macs [email protected],[email protected]
98 # ip ssh client kexalgorithms curve25519-sha256,diffie-hellman-group16-sha512
99 # sonic#
100
101 - name: Delete specified SSH configurations
102 dellemc.enterprise_sonic.sonic_ssh:
103 config:
104 client:
105 cipher: 'aes192-ctr,[email protected]'
106 mac: '[email protected],[email protected]'
107 state: deleted
108
109 # After state:
110 # ------------
111 #
112 # sonic# show running-configuration | grep "ip ssh client"
113 # ip ssh client kexalgorithms curve25519-sha256,diffie-hellman-group16-sha512
114 # sonic#
115
116
117 # Using "deleted" state
118 #
119 # Before state:
120 # -------------
121 #
122 # sonic# show running-configuration | grep "ip ssh client"
123 # ip ssh client ciphers aes192-ctr,[email protected]
124 # ip ssh client kexalgorithms curve25519-sha256,diffie-hellman-group16-sha512
125 # ip ssh client macs [email protected],[email protected]
126 # sonic#
127
128 - name: Delete all SSH configurations
129 dellemc.enterprise_sonic.sonic_ssh:
130 config:
131 state: deleted
132
133 # After state:
134 # ------------
135 #
136 # sonic# show running-configuration | grep "ip ssh client"
137 # (No "ip ssh client" configuration present)
138 # sonic#
139
140
141 # Using "merged" state
142 #
143 # Before state:
144 # -------------
145 #
146 # sonic# show running-configuration | grep "ip ssh client"
147 # sonic
148 # (No "ip ssh client" configuration present)
149
150 - name: Modify SSH configurations
151 dellemc.enterprise_sonic.sonic_ssh:
152 config:
153 client:
154 cipher: 'aes192-ctr,[email protected]'
155 mac: '[email protected],[email protected]'
156 kex: 'curve25519-sha256,diffie-hellman-group16-sha512'
157 state: merged
158
159 # After state:
160 # ------------
161 #
162 # sonic# show running-configuration | grep "ip ssh client"
163 # ip ssh client ciphers aes192-ctr,[email protected]
164 # ip ssh client kexalgorithms curve25519-sha256,diffie-hellman-group16-sha512
165 # ip ssh client macs [email protected],[email protected]
166 # sonic#
167
168
169 # Using "replaced" state
170 #
171 # Before state:
172 # -------------
173 #
174 # sonic# show running-configuration | grep "ip ssh client"
175 # ip ssh client ciphers aes192-ctr,[email protected]
176 # ip ssh client kexalgorithms curve25519-sha256,diffie-hellman-group16-sha512
177 # ip ssh client macs [email protected],[email protected]
178 # sonic#
179
180 - name: Modify SSH configurations
181 dellemc.enterprise_sonic.sonic_ssh:
182 config:
183 client:
184 cipher: 'aes256-ctr'
185 kex: 'curve25519-sha256,diffie-hellman-group16-sha512'
186 state: replaced
187
188 # After state:
189 # ------------
190 #
191 # sonic# show running-configuration | grep "ip ssh client"
192 # ip ssh client ciphers aes256-ctr
193 # ip ssh client kexalgorithms curve25519-sha256,diffie-hellman-group16-sha512
194 # sonic#
195
196
197 # Using "overridden" state
198 #
199 # Before state:
200 # -------------
201 #
202 # sonic# show running-configuration | grep "ip ssh client"
203 # ip ssh client ciphers aes192-ctr,[email protected]
204 # ip ssh client kexalgorithms curve25519-sha256,diffie-hellman-group16-sha512
205 # ip ssh client macs [email protected],[email protected]
206 # sonic#
207
208 - name: Modify SSH configurations
209 dellemc.enterprise_sonic.sonic_ssh:
210 config:
211 client:
212 cipher: 'aes256-ctr'
213 mac: '[email protected],[email protected]'
214 state: overridden
215
216 # After state:
217 # ------------
218 #
219 # sonic# show running-configuration | grep "ip ssh client"
220 # ip ssh client ciphers aes256-ctr
221 # ip ssh client macs [email protected],[email protected]
222 # sonic#
223 """
224
225 RETURN = """
226 before:
227 description: The configuration prior to the model invocation.
228 returned: always
229 type: dict
230 sample: >
231 The configuration returned will always be in the same format
232 as the parameters above.
233 after:
234 description: The resulting configuration model invocation.
235 returned: when changed
236 type: dict
237 sample: >
238 The configuration returned will always be in the same format
239 as the parameters above.
240 after_generated:
241 description: The generated configuration from module invocation.
242 returned: when C(check_mode)
243 type: dict
244 sample: >
245 The configuration returned will always be in the same format
246 as the parameters above.
247 commands:
248 description: The set of commands pushed to the remote device.
249 returned: always
250 type: list
251 sample: ['command 1', 'command 2', 'command 3']
252 """
253
254
255 from ansible.module_utils.basic import AnsibleModule
256 from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.argspec.ssh.ssh import SshArgs
257 from ansible_collections.dellemc.enterprise_sonic.plugins.module_utils.network.sonic.config.ssh.ssh import Ssh
258
259
260 def main():
261 """
262 Main entry point for module execution
263
264 :returns: the result form module invocation
265 """
266 module = AnsibleModule(argument_spec=SshArgs.argument_spec,
267 supports_check_mode=True)
268
269 result = Ssh(module).execute_module()
270 module.exit_json(**result)
271
272
273 if __name__ == '__main__':
274 main()