Debian Codegraph @

ansible_collections/purestorage/flashblade/plugins/modules/purefb_ds.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2018, Simon Dodsley ([email protected])
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11 ANSIBLE_METADATA = {
12 "metadata_version": "1.1",
13 "status": ["preview"],
14 "supported_by": "community",
15 }
16
17 DOCUMENTATION = r"""
18 ---
19 module: purefb_ds
20 version_added: '1.0.0'
21 short_description: Configure FlashBlade Directory Service
22 description:
23 - Create, modify or erase directory services configurations. There is no
24 facility to SSL certificates at this time. Use the FlashBlade GUI for this
25 additional configuration work.
26 - If updating a directory service and i(bind_password) is provided this
27 will always cause a change, even if the password given isn't different from
28 the current. This makes this part of the module non-idempotent..
29 author:
30 - Pure Storage Ansible Team (@sdodsley) <[email protected]>
31 options:
32 state:
33 description:
34 - Create, delete or test directory service configuration
35 default: present
36 type: str
37 choices: [ absent, present, test ]
38 dstype:
39 description:
40 - The type of directory service to work on
41 choices: [ management, nfs, smb ]
42 type: str
43 required: true
44 enable:
45 description:
46 - Whether to enable or disable directory service support.
47 default: false
48 type: bool
49 uri:
50 description:
51 - A list of up to 30 URIs of the directory servers. Each URI must include
52 the scheme ldap:// or ldaps:// (for LDAP over SSL), a hostname, and a
53 domain name or IP address. For example, ldap://ad.company.com configures
54 the directory service with the hostname "ad" in the domain "company.com"
55 while specifying the unencrypted LDAP protocol.
56 type: list
57 elements: str
58 base_dn:
59 description:
60 - Sets the base of the Distinguished Name (DN) of the directory service
61 groups. The base should consist of only Domain Components (DCs). The
62 base_dn will populate with a default value when a URI is entered by
63 parsing domain components from the URI. The base DN should specify DC=
64 for each domain component and multiple DCs should be separated by commas.
65 type: str
66 bind_password:
67 description:
68 - Sets the password of the bind_user user name account.
69 type: str
70 force_bind_password:
71 type: bool
72 default: true
73 description:
74 - Will force the bind password to be reset even if the bind user password
75 is unchanged.
76 - If set to I(false) and I(bind_user) is unchanged the password will not
77 be reset.
78 version_added: 1.16.0
79 bind_user:
80 description:
81 - Sets the user name that can be used to bind to and query the directory.
82 - For Active Directory, enter the username - often referred to as
83 sAMAccountName or User Logon Name - of the account that is used to
84 perform directory lookups.
85 - For OpenLDAP, enter the full DN of the user.
86 type: str
87 nis_servers:
88 description:
89 - A list of up to 30 IP addresses or FQDNs for NIS servers.
90 - This cannot be used in conjunction with LDAP configurations.
91 type: list
92 elements: str
93 nis_domain:
94 description:
95 - The NIS domain to search
96 - This cannot be used in conjunction with LDAP configurations.
97 type: str
98 join_ou:
99 description:
100 - The optional organizational unit (OU) where the machine account
101 for the directory service will be created.
102 type: str
103 nfs_server:
104 description:
105 - Name of the internal server
106 - Only required if I(nfs) is the I(dstype)
107 type: str
108 version_added: 1.23.0
109 extends_documentation_fragment:
110 - purestorage.flashblade.purestorage.fb
111 """
112
113 EXAMPLES = r"""
114 - name: Delete existing management directory service
115 purestorage.flashblade.purefb_ds:
116 dstype: management
117 state: absent
118 fb_url: 10.10.10.2
119 api_token: T-55a68eb5-c785-4720-a2ca-8b03903bf641
120
121 - name: Create NFS directory service (disabled)
122 purestorage.flashblade.purefb_ds:
123 dstype: nfs
124 uri: "ldaps://lab.purestorage.com"
125 base_dn: "DC=lab,DC=purestorage,DC=com"
126 bind_user: Administrator
127 bind_password: password
128 fb_url: 10.10.10.2
129 api_token: T-55a68eb5-c785-4720-a2ca-8b03903bf641
130
131 - name: Enable existing SMB directory service
132 purestorage.flashblade.purefb_ds:
133 dstypr: smb
134 enable: true
135 fb_url: 10.10.10.2
136 api_token: T-55a68eb5-c785-4720-a2ca-8b03903bf641
137
138 - name: Disable existing management directory service
139 purestorage.flashblade.purefb_ds:
140 dstype: management
141 enable: false
142 fb_url: 10.10.10.2
143 api_token: T-55a68eb5-c785-4720-a2ca-8b03903bf641
144
145 - name: Create NFS directory service (LDAP-based) - default internal server
146 purestorage.flashblade.purefb_ds:
147 dstype: nfs
148 enable: true
149 uri: "ldaps://lab.purestorage.com"
150 base_dn: "DC=lab,DC=purestorage,DC=com"
151 bind_user: Administrator
152 bind_password: password
153 fb_url: 10.10.10.2
154 api_token: T-55a68eb5-c785-4720-a2ca-8b03903bf641
155
156 - name: Create NFS directory service (NIS based) - alternate internal server
157 purestorage.flashblade.purefb_ds:
158 dstype: nfs
159 nfs_server: acme1
160 enable: true
161 nis_domain: "acme.com"
162 nis_servers:
163 - "1.1.1.1"
164 - "2.2.2.2"
165 fb_url: 10.10.10.2
166 api_token: T-55a68eb5-c785-4720-a2ca-8b03903bf641
167
168 - name: Test array management directory service
169 purestorage.flashblade.purefb_ds:
170 dstype: management
171 state: test
172 fb_url: 10.10.10.2
173 api_token: T-55a68eb5-c785-4720-a2ca-8b03903bf641
174
175 - name: Test default nfs directory service
176 purestorage.flashblade.purefb_ds:
177 dstype: nfs
178 state: test
179 fb_url: 10.10.10.2
180 api_token: T-55a68eb5-c785-4720-a2ca-8b03903bf641
181
182 - name: Test NFS directory service for internal server fred
183 purestorage.flashblade.purefb_ds:
184 dstype: nfs
185 nfs_server: fred
186 state: test
187 fb_url: 10.10.10.2
188 api_token: T-55a68eb5-c785-4720-a2ca-8b03903bf641
189 """
190
191 RETURN = r"""
192 """
193
194
195 NO_SMB_VERSION = "2.16"
196
197 HAS_PURITY_FB = True
198 try:
199 from pypureclient.flashblade import DirectoryService
200 except ImportError:
201 HAS_PURITY_FB = False
202
203
204 from ansible.module_utils.basic import AnsibleModule
205 from ansible_collections.purestorage.flashblade.plugins.module_utils.purefb import (
206 get_system,
207 purefb_argument_spec,
208 )
209
210
211 def delete_ds(module, blade):
212 """Delete Directory Service"""
213 changed = True
214 ds_name = module.params["dstype"]
215 if not module.check_mode:
216 if module.params["nfs_server"] and module.params["dstype"] == "nfs":
217 if module.params["nfs_server"] != "_array_server":
218 ds_name = module.params["nfs_server"] + "_nfs"
219 res = blade.get_directory_services(names=[ds_name])
220 if res.status_code != 200:
221 module.fail_json(
222 msg="Fetch {0} Directory Service failed. Error: {1}".format(
223 module.params["dstype"], res.errors[0].message
224 )
225 )
226 dirserv = list(res.items)[0]
227 dir_service = DirectoryService() # Initialize for pylint
228 if module.params["dstype"] == "management":
229 if dirserv.uris:
230 dir_service = DirectoryService(
231 uris=[""],
232 base_dn="",
233 bind_user="",
234 bind_password="",
235 enabled=False,
236 )
237 else:
238 changed = False
239 elif module.params["dstype"] == "smb":
240 if dirserv.uris:
241 smb_attrs = {"join_ou": ""}
242 dir_service = DirectoryService(
243 uris=[""],
244 base_dn="",
245 bind_user="",
246 bind_password="",
247 smb=smb_attrs,
248 enabled=False,
249 )
250 else:
251 changed = False
252 elif module.params["dstype"] == "nfs":
253 if dirserv.uris:
254 dir_service = DirectoryService(
255 uris=[""],
256 base_dn="",
257 bind_user="",
258 bind_password="",
259 enabled=False,
260 )
261 elif dirserv.nfs.nis_domains:
262 nfs_attrs = {"nis_domains": [], "nis_servers": []}
263 dir_service = DirectoryService(nfs=nfs_attrs, enabled=False)
264 else:
265 changed = False
266 if changed:
267 res = blade.patch_directory_services(
268 names=[ds_name], directory_service=dir_service
269 )
270 if res.status_code != 200:
271 module.fail_json(
272 msg="Delete Directory Service {0} failed. Error: {1}".format(
273 ds_name, res.errors[0].message
274 )
275 )
276 module.exit_json(changed=changed)
277
278
279 def update_ds(module, blade):
280 """Update Directory Service"""
281 mod_ds = False
282 changed = False
283 password_required = False
284 attr = {}
285 ds_name = module.params["dstype"]
286 if module.params["nfs_server"] and module.params["dstype"] == "nfs":
287 if module.params["nfs_server"] != "_array_server":
288 ds_name = module.params["nfs_server"] + "_nfs"
289 res = blade.get_directory_services(names=[ds_name])
290 if res.status_code != 200:
291 module.fail_json(
292 msg="Fetch {0} Directory Service failed. Error: {1}".format(
293 module.params["dstype"], res.errors[0].message
294 )
295 )
296 ds_now = list(res.items)[0]
297 if module.params["dstype"] == "nfs" and module.params["nis_servers"]:
298 if module.params["enable"]:
299 if module.params["enable"] != ds_now.enabled:
300 attr["enabled"] = module.params["enable"]
301 mod_ds = True
302 if sorted(module.params["nis_servers"]) != sorted(
303 ds_now.nfs.nis_servers
304 ) or module.params["nis_domain"] != "".join(map(str, ds_now.nfs.nis_domains)):
305 attr["nfs"] = {
306 "nis_domains": [module.params["nis_domain"]],
307 "nis_servers": module.params["nis_servers"][0:30],
308 }
309 mod_ds = True
310 elif module.params["dstype"] == "smb":
311 if module.params["join_ou"] != ds_now.smb.join_ou:
312 attr["smb"] = {"join_ou": module.params["join_ou"]}
313 mod_ds = True
314 if module.params["enable"]:
315 if module.params["enable"] != ds_now.enabled:
316 attr["enabled"] = module.params["enable"]
317 mod_ds = True
318 else:
319 if module.params["uri"]:
320 if sorted(module.params["uri"][0:30]) != sorted(ds_now.uris):
321 attr["uris"] = module.params["uri"][0:30]
322 mod_ds = True
323 password_required = True
324 if module.params["base_dn"]:
325 if module.params["base_dn"] != ds_now.base_dn:
326 attr["base_dn"] = module.params["base_dn"]
327 mod_ds = True
328 if module.params["bind_user"]:
329 if module.params["bind_user"] != ds_now.bind_user:
330 password_required = True
331 attr["bind_user"] = module.params["bind_user"]
332 mod_ds = True
333 elif module.params["force_bind_password"]:
334 password_required = True
335 mod_ds = True
336 if module.params["enable"]:
337 if module.params["enable"] != ds_now.enabled:
338 attr["enabled"] = module.params["enable"]
339 mod_ds = True
340 if password_required:
341 if module.params["bind_password"]:
342 attr["bind_password"] = module.params["bind_password"]
343 mod_ds = True
344 else:
345 module.fail_json(msg="'bind_password' must be provided for this task")
346 if mod_ds:
347 changed = True
348 if not module.check_mode:
349 n_attr = DirectoryService(**attr)
350 res = blade.patch_directory_services(
351 names=[ds_name], directory_service=n_attr
352 )
353 if res.status_code != 200:
354 module.fail_json(
355 msg="Failed to change directory service {0}. Error: {1}".format(
356 ds_name, res.errors[0].message
357 )
358 )
359 module.exit_json(changed=changed)
360
361
362 def create_ds(module, blade):
363 """Create Directory Service"""
364 changed = True
365 ds_name = module.params["dstype"]
366 if not module.check_mode:
367 dir_service = DirectoryService() # Initialize for pylint
368 if module.params["dstype"] == "management":
369 if module.params["uri"]:
370 dir_service = DirectoryService(
371 uris=module.params["uri"][0:30],
372 base_dn=module.params["base_dn"],
373 bind_user=module.params["bind_user"],
374 bind_password=module.params["bind_password"],
375 enabled=module.params["enable"],
376 )
377 else:
378 module.fail_json(
379 msg="Incorrect parameters provided for dstype {0}".format(
380 module.params["dstype"]
381 )
382 )
383 elif module.params["dstype"] == "smb":
384 if module.params["uri"]:
385 smb_attrs = {"join_ou": module.params["join_ou"]}
386 dir_service = DirectoryService(
387 uris=module.params["uri"][0:30],
388 base_dn=module.params["base_dn"],
389 bind_user=module.params["bind_user"],
390 bind_password=module.params["bind_password"],
391 smb=smb_attrs,
392 enabled=module.params["enable"],
393 )
394 else:
395 module.fail_json(
396 msg="Incorrect parameters provided for dstype {0}".format(
397 module.params["dstype"]
398 )
399 )
400 elif module.params["dstype"] == "nfs":
401 if module.params["nis_domain"]:
402 nfs_attrs = {
403 "nis_domains": [module.params["nis_domain"]],
404 "nis_servers": module.params["nis_servers"][0:30],
405 }
406 dir_service = DirectoryService(
407 nfs=nfs_attrs, enabled=module.params["enable"]
408 )
409 else:
410 dir_service = DirectoryService(
411 uris=module.params["uri"][0:30],
412 base_dn=module.params["base_dn"],
413 bind_user=module.params["bind_user"],
414 bind_password=module.params["bind_password"],
415 enabled=module.params["enable"],
416 )
417 if module.params["nfs_server"] and module.params["dstype"] == "nfs":
418 if module.params["nfs_server"] != "_array_server":
419 ds_name = module.params["nfs_server"] + "_nfs"
420 res = blade.patch_directory_services(
421 names=[ds_name], directory_service=dir_service
422 )
423 if res.status_code != 200:
424 module.fail_json(
425 msg="Create Directory Service {0} failed. Error: {1}".format(
426 ds_name, res.errors[0].message
427 )
428 )
429 module.exit_json(changed=changed)
430
431
432 def test_ds(module, blade):
433 """Test directory services configuration"""
434 test_server = module.params["dstype"]
435 if module.params["dstype"] == "nfs" and module.params["nfs_server"]:
436 if module.params["test_nfs_server"] != "_array_server":
437 test_server = module.params["nfs_server"] + "_nfs"
438 test_response = []
439 response = list(blade.get_directory_services_test(names=[test_server]).items)
440 for component in range(len(response)):
441 if response[component].enabled:
442 enabled = "true"
443 else:
444 enabled = "false"
445 if response[component].success:
446 success = "true"
447 else:
448 success = "false"
449 test_response.append(
450 {
451 "component_address": response[component].component_address,
452 "component_name": response[component].component_name,
453 "description": response[component].description,
454 "destination": response[component].destination,
455 "enabled": enabled,
456 "result_details": getattr(response[component], "result_details", ""),
457 "success": success,
458 "test_type": response[component].test_type,
459 "resource_name": response[component].resource.name,
460 }
461 )
462 module.exit_json(changed=False, test_response=test_response)
463
464
465 def main():
466 argument_spec = purefb_argument_spec()
467 argument_spec.update(
468 dict(
469 uri=dict(type="list", elements="str"),
470 dstype=dict(
471 required=True, type="str", choices=["management", "nfs", "smb"]
472 ),
473 state=dict(
474 type="str", default="present", choices=["absent", "present", "test"]
475 ),
476 enable=dict(type="bool", default=False),
477 bind_password=dict(type="str", no_log=True),
478 force_bind_password=dict(type="bool", default=True, no_log=False),
479 bind_user=dict(type="str"),
480 base_dn=dict(type="str"),
481 join_ou=dict(type="str"),
482 nis_domain=dict(type="str"),
483 nis_servers=dict(type="list", elements="str"),
484 nfs_server=dict(type="str"),
485 )
486 )
487
488 required_together = [
489 ["uri", "bind_password", "bind_user", "base_dn"],
490 ["nis_servers", "nis_domain"],
491 ]
492 mutually_exclusive = [["uri", "nis_domain"]]
493
494 module = AnsibleModule(
495 argument_spec,
496 required_together=required_together,
497 mutually_exclusive=mutually_exclusive,
498 supports_check_mode=True,
499 )
500 if not HAS_PURITY_FB:
501 module.fail_json(msg="py-pure-client sdk is required for this module")
502
503 state = module.params["state"]
504 blade = get_system(module)
505 api_version = list(blade.get_versions().items)
506 if NO_SMB_VERSION in api_version and module.params["dstype"] == "smb":
507 module.warn("Directory Service for SMB no longer supported by FlashBlade")
508 module.exit_json(changed=False)
509 ds_name = module.params["dstype"]
510 if module.params["nfs_server"] and module.params["dstype"] == "nfs":
511 if module.params["nfs_server"] != "_array_server":
512 ds_name = module.params["nfs_server"] + "_nfs"
513 res = blade.get_directory_services(names=[ds_name])
514 ds_configured = False
515 if res.status_code == 200:
516 dirserv = list(res.items)[0]
517 ds_configured = True
518 if module.params["dstype"] == "nfs" and not dirserv.nfs.nis_servers:
519 ds_configured = False
520 else:
521 module.exit_json(msg="Directory Service {0} does not exist".format(ds_name))
522 ds_enabled = dirserv.enabled
523 ldap_uri = False
524 set_ldap = False
525 for uri in range(len(dirserv.uris)):
526 if "ldap" in dirserv.uris[uri].lower():
527 ldap_uri = True
528 if module.params["uri"]:
529 for uri in range(len(module.params["uri"])):
530 if "ldap" in module.params["uri"][uri].lower():
531 set_ldap = True
532 if not module.params["uri"] and ldap_uri or module.params["uri"] and set_ldap:
533 if module.params["nis_servers"] or module.params["nis_domain"]:
534 module.fail_json(
535 msg="NIS configuration not supported in an LDAP environment"
536 )
537 if state == "absent":
538 delete_ds(module, blade)
539 elif ds_configured and state == "present":
540 update_ds(module, blade)
541 elif not ds_configured and state == "present":
542 create_ds(module, blade)
543 elif state == "test":
544 test_ds(module, blade)
545 else:
546 module.exit_json(changed=False)
547
548
549 if __name__ == "__main__":
550 main()