Debian Codegraph @

ansible_collections/dellemc/unity/plugins/modules/cifsserver.py

raw symbols SCIP
1 #!/usr/bin/python
2 # Copyright: (c) 2022-2025, Dell Technologies
3 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4
5 """Ansible module for managing CIFS server on Unity"""
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11 DOCUMENTATION = r'''
12 module: cifsserver
13 version_added: '1.4.0'
14 short_description: Manage CIFS server on Unity storage system
15 description:
16 - Managing the CIFS server on the Unity storage system includes creating CIFS server, getting CIFS server details
17 and deleting CIFS server.
18
19 extends_documentation_fragment:
20 - dellemc.unity.unity
21
22 author:
23 - Akash Shendge (@shenda1) <[email protected]>
24
25 options:
26 nas_server_name:
27 description:
28 - Name of the NAS server on which CIFS server will be hosted.
29 type: str
30 nas_server_id:
31 description:
32 - ID of the NAS server on which CIFS server will be hosted.
33 type: str
34 netbios_name:
35 description:
36 - The computer name of the SMB server in Windows network.
37 type: str
38 workgroup:
39 description:
40 - Standalone SMB server workgroup.
41 type: str
42 local_password:
43 description:
44 - Standalone SMB server administrator password.
45 type: str
46 domain:
47 description:
48 - The domain name where the SMB server is registered in Active Directory.
49 type: str
50 domain_username:
51 description:
52 - Active Directory domain user name.
53 type: str
54 domain_password:
55 description:
56 - Active Directory domain password.
57 type: str
58 cifs_server_name:
59 description:
60 - The name of the CIFS server.
61 type: str
62 cifs_server_id:
63 description:
64 - The ID of the CIFS server.
65 type: str
66 interfaces:
67 description:
68 - List of file IP interfaces that service CIFS protocol of SMB server.
69 type: list
70 elements: str
71 unjoin_cifs_server_account:
72 description:
73 - Keep SMB server account unjoined in Active Directory after deletion.
74 - C(false) specifies keep SMB server account joined after deletion.
75 - C(true) specifies unjoin SMB server account from Active Directory before deletion.
76 type: bool
77 state:
78 description:
79 - Define whether the CIFS server should exist or not.
80 choices: [absent, present]
81 required: true
82 type: str
83 notes:
84 - The I(check_mode) is supported.
85 '''
86
87 EXAMPLES = r'''
88 - name: Create CIFS server belonging to Active Directory
89 cifsserver:
90 unispherehost: "{{unispherehost}}"
91 username: "{{username}}"
92 password: "{{password}}"
93 validate_certs: "{{validate_certs}}"
94 nas_server_name: "test_nas1"
95 cifs_server_name: "test_cifs"
96 domain: "ad_domain"
97 domain_username: "domain_username"
98 domain_password: "domain_password"
99 state: "present"
100
101 - name: Get CIFS server details using CIFS server ID
102 cifsserver:
103 unispherehost: "{{unispherehost}}"
104 username: "{{username}}"
105 password: "{{password}}"
106 validate_certs: "{{validate_certs}}"
107 cifs_server_id: "cifs_37"
108 state: "present"
109
110 - name: Get CIFS server details using NAS server name
111 cifsserver:
112 unispherehost: "{{unispherehost}}"
113 username: "{{username}}"
114 password: "{{password}}"
115 validate_certs: "{{validate_certs}}"
116 nas_server_name: "test_nas1"
117 state: "present"
118
119 - name: Delete CIFS server
120 cifsserver:
121 unispherehost: "{{unispherehost}}"
122 username: "{{username}}"
123 password: "{{password}}"
124 validate_certs: "{{validate_certs}}"
125 cifs_server_id: "cifs_37"
126 unjoin_cifs_server_account: true
127 domain_username: "domain_username"
128 domain_password: "domain_password"
129 state: "absent"
130
131 - name: Create standalone CIFS server
132 cifsserver:
133 unispherehost: "{{unispherehost}}"
134 username: "{{username}}"
135 password: "{{password}}"
136 validate_certs: "{{validate_certs}}"
137 netbios_name: "ANSIBLE_CIFS"
138 workgroup: "ansible"
139 local_password: "Password123!"
140 nas_server_name: "test_nas1"
141 state: "present"
142
143 - name: Get CIFS server details using netbios name
144 cifsserver:
145 unispherehost: "{{unispherehost}}"
146 username: "{{username}}"
147 password: "{{password}}"
148 validate_certs: "{{validate_certs}}"
149 netbios_name: "ANSIBLE_CIFS"
150 state: "present"
151
152 - name: Delete standalone CIFS server
153 cifsserver:
154 unispherehost: "{{unispherehost}}"
155 username: "{{username}}"
156 password: "{{password}}"
157 validate_certs: "{{validate_certs}}"
158 cifs_server_id: "cifs_40"
159 state: "absent"
160 '''
161
162 RETURN = r'''
163 changed:
164 description: Whether or not the resource has changed.
165 returned: always
166 type: bool
167 sample: true
168
169 cifs_server_details:
170 description: Details of the CIFS server.
171 returned: When CIFS server exists
172 type: dict
173 contains:
174 id:
175 description: Unique identifier of the CIFS server instance.
176 type: str
177 name:
178 description: User-specified name for the SMB server.
179 type: str
180 netbios_name:
181 description: Computer Name of the SMB server in windows network.
182 type: str
183 description:
184 description: Description of the SMB server.
185 type: str
186 domain:
187 description: Domain name where SMB server is registered in Active Directory.
188 type: str
189 workgroup:
190 description: Windows network workgroup for the SMB server.
191 type: str
192 is_standalone:
193 description: Indicates whether the SMB server is standalone.
194 type: bool
195 nasServer:
196 description: Information about the NAS server in the storage system.
197 type: dict
198 contains:
199 UnityNasServer:
200 description: Information about the NAS server in the storage system.
201 type: dict
202 contains:
203 id:
204 description: Unique identifier of the NAS server instance.
205 type: str
206 file_interfaces:
207 description: The file interfaces associated with the NAS server.
208 type: dict
209 contains:
210 UnityFileInterfaceList:
211 description: List of file interfaces associated with the NAS server.
212 type: list
213 contains:
214 UnityFileInterface:
215 description: Details of file interface associated with the NAS server.
216 type: dict
217 contains:
218 id:
219 description: Unique identifier of the file interface.
220 type: str
221 smb_multi_channel_supported:
222 description: Indicates whether the SMB 3.0+ multichannel feature is supported.
223 type: bool
224 smb_protocol_versions:
225 description: Supported SMB protocols, such as 1.0, 2.0, 2.1, 3.0, and so on.
226 type: list
227 smbca_supported:
228 description: Indicates whether the SMB server supports continuous availability.
229 type: bool
230 sample: {
231 "description": null,
232 "domain": "xxx.xxx.xxx.com",
233 "existed": true,
234 "file_interfaces": {
235 "UnityFileInterfaceList": [
236 {
237 "UnityFileInterface": {
238 "hash": -9223363258905013637,
239 "id": "if_43"
240 }
241 }
242 ]
243 },
244 "hash": -9223363258905010379,
245 "health": {
246 "UnityHealth": {
247 "hash": 8777949765559
248 }
249 },
250 "id": "cifs_40",
251 "is_standalone": false,
252 "last_used_organizational_unit": "ou=Computers,ou=Dell NAS servers",
253 "name": "ansible_cifs",
254 "nas_server": {
255 "UnityNasServer": {
256 "hash": 8777949765531,
257 "id": "nas_18"
258 }
259 },
260 "netbios_name": "ANSIBLE_CIFS",
261 "smb_multi_channel_supported": true,
262 "smb_protocol_versions": [
263 "1.0",
264 "2.0",
265 "2.1",
266 "3.0"
267 ],
268 "smbca_supported": true,
269 "workgroup": null
270 }
271 '''
272
273 from ansible.module_utils.basic import AnsibleModule
274 from ansible_collections.dellemc.unity.plugins.module_utils.storage.dell import utils
275
276 LOG = utils.get_logger('cifsserver')
277
278
279 application_type = "Ansible/1.7.1"
280
281
282 class CIFSServer(object):
283 """Class with CIFS server operations"""
284
285 def __init__(self):
286 """Define all parameters required by this module"""
287 self.module_params = utils.get_unity_management_host_parameters()
288 self.module_params.update(get_cifs_server_parameters())
289
290 mutually_exclusive = [['nas_server_name', 'nas_server_id'], ['cifs_server_id', 'cifs_server_name'],
291 ['cifs_server_id', 'netbios_name']]
292 required_one_of = [['cifs_server_id', 'cifs_server_name', 'netbios_name', 'nas_server_name', 'nas_server_id']]
293
294 # initialize the Ansible module
295 self.module = AnsibleModule(
296 argument_spec=self.module_params,
297 supports_check_mode=True,
298 mutually_exclusive=mutually_exclusive,
299 required_one_of=required_one_of
300 )
301 utils.ensure_required_libs(self.module)
302
303 self.unity_conn = utils.get_unity_unisphere_connection(
304 self.module.params, application_type)
305 LOG.info('Check Mode Flag %s', self.module.check_mode)
306
307 def get_details(self, cifs_server_id=None, cifs_server_name=None, netbios_name=None, nas_server_id=None):
308 """Get CIFS server details.
309 :param: cifs_server_id: The ID of the CIFS server
310 :param: cifs_server_name: The name of the CIFS server
311 :param: netbios_name: Name of the SMB server in windows network
312 :param: nas_server_id: The ID of the NAS server
313 :return: Dict containing CIFS server details if exists
314 """
315
316 LOG.info("Getting CIFS server details")
317 id_or_name = get_id_name(cifs_server_id, cifs_server_name, netbios_name, nas_server_id)
318
319 try:
320 if cifs_server_id:
321 cifs_server_details = self.unity_conn.get_cifs_server(_id=cifs_server_id)
322 return process_response(cifs_server_details)
323
324 if cifs_server_name:
325 cifs_server_details = self.unity_conn.get_cifs_server(name=cifs_server_name)
326 return process_response(cifs_server_details)
327
328 if netbios_name:
329 cifs_server_details = self.unity_conn.get_cifs_server(netbios_name=netbios_name)
330 if len(cifs_server_details) > 0:
331 return process_dict(cifs_server_details._get_properties())
332
333 if nas_server_id:
334 cifs_server_details = self.unity_conn.get_cifs_server(nas_server=nas_server_id)
335 if len(cifs_server_details) > 0:
336 return process_dict(cifs_server_details._get_properties())
337 return None
338 except utils.HttpError as e:
339 if e.http_status == 401:
340 msg = "Failed to get CIFS server: %s due to incorrect " \
341 "username/password error: %s" % (id_or_name, str(e))
342 else:
343 msg = "Failed to get CIFS server: %s with error: %s" % (id_or_name, str(e))
344 except utils.UnityResourceNotFoundError:
345 msg = "CIFS server with ID %s not found" % cifs_server_id
346 LOG.info(msg)
347 return None
348 except utils.StoropsConnectTimeoutError as e:
349 msg = "Failed to get CIFS server: %s with error: %s. Please check unispherehost IP: %s" % (
350 id_or_name, str(e), self.module.params['unispherehost'])
351 except Exception as e:
352 msg = "Failed to get details of CIFS server: %s with error: %s" % (id_or_name, str(e))
353 LOG.error(msg)
354 self.module.fail_json(msg=msg)
355
356 def get_cifs_server_instance(self, cifs_server_id):
357 """Get CIFS server instance.
358 :param: cifs_server_id: The ID of the CIFS server
359 :return: Return CIFS server instance if exists
360 """
361
362 try:
363 cifs_server_obj = utils.UnityCifsServer.get(cli=self.unity_conn._cli, _id=cifs_server_id)
364 return cifs_server_obj
365
366 except Exception as e:
367 error_msg = "Failed to get the CIFS server %s instance" \
368 " with error %s" % (cifs_server_id, str(e))
369 LOG.error(error_msg)
370 self.module.fail_json(msg=error_msg)
371
372 def delete_cifs_server(self, cifs_server_id, skip_unjoin=None, domain_username=None, domain_password=None):
373 """Delete CIFS server.
374 :param: cifs_server_id: The ID of the CIFS server
375 :param: skip_unjoin: Flag indicating whether to unjoin SMB server account from AD before deletion
376 :param: domain_username: The domain username
377 :param: domain_password: The domain password
378 :return: Return True if CIFS server is deleted
379 """
380
381 LOG.info("Deleting CIFS server")
382 try:
383 if not self.module.check_mode:
384 cifs_obj = self.get_cifs_server_instance(cifs_server_id=cifs_server_id)
385 cifs_obj.delete(skip_domain_unjoin=skip_unjoin, username=domain_username, password=domain_password)
386 return True
387
388 except Exception as e:
389 msg = "Failed to delete CIFS server: %s with error: %s" % (cifs_server_id, str(e))
390 LOG.error(msg)
391 self.module.fail_json(msg=msg)
392
393 def get_nas_server_id(self, nas_server_name):
394 """Get NAS server ID.
395 :param: nas_server_name: The name of NAS server
396 :return: Return NAS server ID if exists
397 """
398
399 LOG.info("Getting NAS server ID")
400 try:
401 obj_nas = self.unity_conn.get_nas_server(name=nas_server_name)
402 return obj_nas.get_id()
403
404 except Exception as e:
405 msg = "Failed to get details of NAS server: %s with error: %s" % (nas_server_name, str(e))
406 LOG.error(msg)
407 self.module.fail_json(msg=msg)
408
409 def is_modify_interfaces(self, cifs_server_details):
410 """Check if modification is required in existing interfaces
411 :param: cifs_server_details: CIFS server details
412 :return: Flag indicating if modification is required
413 """
414
415 existing_interfaces = []
416 if cifs_server_details['file_interfaces']['UnityFileInterfaceList']:
417 for interface in cifs_server_details['file_interfaces']['UnityFileInterfaceList']:
418 existing_interfaces.append(interface['UnityFileInterface']['id'])
419
420 for interface in self.module.params['interfaces']:
421 if interface not in existing_interfaces:
422 return True
423 return False
424
425 def is_modification_required(self, cifs_server_details):
426 """Check if modification is required in existing CIFS server
427 :param: cifs_server_details: CIFS server details
428 :return: Flag indicating if modification is required
429 """
430
431 LOG.info("Checking if any modification is required")
432 param_list = ['netbios_name', 'workgroup']
433 for param in param_list:
434 if self.module.params[param] is not None and cifs_server_details[param] is not None and \
435 self.module.params[param].upper() != cifs_server_details[param]:
436 return True
437
438 # Check for domain
439 if self.module.params['domain'] is not None and cifs_server_details['domain'] is not None and \
440 self.module.params['domain'] != cifs_server_details['domain']:
441 return True
442
443 # Check file interfaces
444 if self.module.params['interfaces'] is not None:
445 return self.is_modify_interfaces(cifs_server_details)
446 return False
447
448 def create_cifs_server(self, nas_server_id, interfaces=None, netbios_name=None, cifs_server_name=None, domain=None,
449 domain_username=None, domain_password=None, workgroup=None, local_password=None):
450 """Create CIFS server.
451 :param: nas_server_id: The ID of NAS server
452 :param: interfaces: List of file interfaces
453 :param: netbios_name: Name of the SMB server in windows network
454 :param: cifs_server_name: Name of the CIFS server
455 :param: domain: The domain name where the SMB server is registered in Active Directory
456 :param: domain_username: The domain username
457 :param: domain_password: The domain password
458 :param: workgroup: Standalone SMB server workgroup
459 :param: local_password: Standalone SMB server admin password
460 :return: Return True if CIFS server is created
461 """
462
463 LOG.info("Creating CIFS server")
464 try:
465 if not self.module.check_mode:
466 utils.UnityCifsServer.create(cli=self.unity_conn._cli, nas_server=nas_server_id, interfaces=interfaces,
467 netbios_name=netbios_name, name=cifs_server_name, domain=domain,
468 domain_username=domain_username, domain_password=domain_password,
469 workgroup=workgroup, local_password=local_password)
470 return True
471 except Exception as e:
472 msg = "Failed to create CIFS server with error: %s" % (str(e))
473 LOG.error(msg)
474 self.module.fail_json(msg=msg)
475
476 def validate_params(self):
477 """Validate the parameters
478 """
479
480 param_list = ['nas_server_id', 'nas_server_name', 'domain', 'cifs_server_id', 'cifs_server_name',
481 'local_password', 'netbios_name', 'workgroup', 'domain_username', 'domain_password']
482
483 msg = "Please provide valid {0}"
484 for param in param_list:
485 if self.module.params[param] is not None and len(self.module.params[param].strip()) == 0:
486 errmsg = msg.format(param)
487 self.module.fail_json(msg=errmsg)
488
489 def perform_module_operation(self):
490 """
491 Perform different actions on CIFS server module based on parameters
492 passed in the playbook
493 """
494 cifs_server_id = self.module.params['cifs_server_id']
495 cifs_server_name = self.module.params['cifs_server_name']
496 nas_server_id = self.module.params['nas_server_id']
497 nas_server_name = self.module.params['nas_server_name']
498 netbios_name = self.module.params['netbios_name']
499 workgroup = self.module.params['workgroup']
500 local_password = self.module.params['local_password']
501 domain = self.module.params['domain']
502 domain_username = self.module.params['domain_username']
503 domain_password = self.module.params['domain_password']
504 interfaces = self.module.params['interfaces']
505 unjoin_cifs_server_account = self.module.params['unjoin_cifs_server_account']
506 state = self.module.params['state']
507
508 # result is a dictionary that contains changed status and CIFS server details
509 result = dict(
510 changed=False,
511 cifs_server_details={}
512 )
513
514 # Validate the parameters
515 self.validate_params()
516
517 if nas_server_name is not None:
518 nas_server_id = self.get_nas_server_id(nas_server_name)
519
520 cifs_server_details = self.get_details(cifs_server_id=cifs_server_id,
521 cifs_server_name=cifs_server_name,
522 netbios_name=netbios_name,
523 nas_server_id=nas_server_id)
524
525 # Check if modification is required
526 if cifs_server_details:
527 if cifs_server_id is None:
528 cifs_server_id = cifs_server_details['id']
529 modify_flag = self.is_modification_required(cifs_server_details)
530 if modify_flag:
531 self.module.fail_json(msg="Modification is not supported through Ansible module")
532
533 if not cifs_server_details and state == 'present':
534 if not nas_server_id:
535 self.module.fail_json(msg="Please provide nas server id/name to create CIFS server.")
536
537 if any([netbios_name, workgroup, local_password]) and not all([netbios_name, workgroup, local_password]):
538 msg = "netbios_name, workgroup and local_password " \
539 "are required to create standalone CIFS server."
540 LOG.error(msg)
541 self.module.fail_json(msg=msg)
542
543 result['changed'] = self.create_cifs_server(nas_server_id, interfaces, netbios_name,
544 cifs_server_name, domain, domain_username, domain_password,
545 workgroup, local_password)
546
547 if state == 'absent' and cifs_server_details:
548 skip_unjoin = None
549 if unjoin_cifs_server_account is not None:
550 skip_unjoin = not unjoin_cifs_server_account
551 result['changed'] = self.delete_cifs_server(cifs_server_id, skip_unjoin, domain_username,
552 domain_password)
553
554 if state == 'present':
555 result['cifs_server_details'] = self.get_details(cifs_server_id=cifs_server_id,
556 cifs_server_name=cifs_server_name,
557 netbios_name=netbios_name,
558 nas_server_id=nas_server_id)
559 LOG.info("Process Dict: %s", result['cifs_server_details'])
560 self.module.exit_json(**result)
561
562
563 def get_id_name(cifs_server_id=None, cifs_server_name=None, netbios_name=None, nas_server_id=None):
564 """Get the id_or_name.
565 :param: cifs_server_id: The ID of CIFS server
566 :param: cifs_server_name: The name of CIFS server
567 :param: netbios_name: Name of the SMB server in windows network
568 :param: nas_server_id: The ID of NAS server
569 :return: Return id_or_name
570 """
571 if cifs_server_id:
572 id_or_name = cifs_server_id
573 elif cifs_server_name:
574 id_or_name = cifs_server_name
575 elif netbios_name:
576 id_or_name = netbios_name
577 else:
578 id_or_name = nas_server_id
579 return id_or_name
580
581
582 def process_response(cifs_server_details):
583 """Process CIFS server details.
584 :param: cifs_server_details: Dict containing CIFS server details
585 :return: Processed dict containing CIFS server details
586 """
587 if cifs_server_details.existed:
588 return cifs_server_details._get_properties()
589
590
591 def process_dict(cifs_server_details):
592 """Process CIFS server details.
593 :param: cifs_server_details: Dict containing CIFS server details
594 :return: Processed dict containing CIFS server details
595 """
596 param_list = ['description', 'domain', 'file_interfaces', 'health', 'id', 'is_standalone', 'name', 'nas_server'
597 'netbios_name', 'smb_multi_channel_supported', 'smb_protocol_versions', 'smbca_supported',
598 'workgroup', 'netbios_name']
599
600 for param in param_list:
601 if param in cifs_server_details:
602 cifs_server_details[param] = cifs_server_details[param][0]
603 return cifs_server_details
604
605
606 def get_cifs_server_parameters():
607 """This method provide parameters required for the ansible
608 CIFS server module on Unity"""
609 return dict(
610 cifs_server_id=dict(), cifs_server_name=dict(),
611 netbios_name=dict(), workgroup=dict(),
612 local_password=dict(no_log=True), domain=dict(),
613 domain_username=dict(), domain_password=dict(no_log=True),
614 nas_server_name=dict(), nas_server_id=dict(),
615 interfaces=dict(type='list', elements='str'),
616 unjoin_cifs_server_account=dict(type='bool'),
617 state=dict(required=True, type='str', choices=['present', 'absent']),
618 )
619
620
621 def main():
622 """Create Unity CIFS server object and perform action on it
623 based on user input from playbook"""
624 obj = CIFSServer()
625 obj.perform_module_operation()
626
627
628 if __name__ == '__main__':
629 main()