Debian Codegraph @

ansible_collections/community/general/plugins/modules/udm_share.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2016, Adfinis SyGroup AG
4 # @keachi
5 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
6 # SPDX-License-Identifier: GPL-3.0-or-later
7
8 from __future__ import annotations
9
10 DOCUMENTATION = r"""
11 module: udm_share
12 author:
13 - keachi (@keachi)
14 short_description: Manage samba shares on a univention corporate server
15 description:
16 - This module allows to manage samba shares on a univention corporate server (UCS). It uses the Python API of the UCS to
17 create a new object or edit it.
18 extends_documentation_fragment:
19 - community.general._attributes
20 attributes:
21 check_mode:
22 support: full
23 diff_mode:
24 support: partial
25 options:
26 state:
27 default: "present"
28 choices: [present, absent]
29 description:
30 - Whether the share is present or not.
31 type: str
32 name:
33 required: true
34 description:
35 - Name.
36 type: str
37 host:
38 description:
39 - Host FQDN (server which provides the share), for example V({{ ansible_fqdn }}). Required if O(state=present).
40 type: str
41 path:
42 description:
43 - Directory on the providing server, for example V(/home). Required if O(state=present).
44 type: path
45 sambaName:
46 description:
47 - Windows name. Required if O(state=present).
48 type: str
49 aliases: [samba_name]
50 ou:
51 required: true
52 description:
53 - Organisational unit, inside the LDAP Base DN.
54 type: str
55 owner:
56 default: '0'
57 description:
58 - Directory owner of the share's root directory.
59 type: str
60 group:
61 default: '0'
62 description:
63 - Directory owner group of the share's root directory.
64 type: str
65 directorymode:
66 default: '00755'
67 description:
68 - Permissions for the share's root directory.
69 type: str
70 root_squash:
71 default: true
72 description:
73 - Modify user ID for root user (root squashing).
74 type: bool
75 subtree_checking:
76 default: true
77 description:
78 - Subtree checking.
79 type: bool
80 sync:
81 default: 'sync'
82 description:
83 - NFS synchronisation.
84 type: str
85 writeable:
86 default: true
87 description:
88 - NFS write access.
89 type: bool
90 sambaBlockSize:
91 description:
92 - Blocking size.
93 type: str
94 aliases: [samba_block_size]
95 sambaBlockingLocks:
96 default: true
97 description:
98 - Blocking locks.
99 type: bool
100 aliases: [samba_blocking_locks]
101 sambaBrowseable:
102 description:
103 - Show in Windows network environment.
104 type: bool
105 default: true
106 aliases: [samba_browsable]
107 sambaCreateMode:
108 default: '0744'
109 description:
110 - File mode.
111 type: str
112 aliases: [samba_create_mode]
113 sambaCscPolicy:
114 default: 'manual'
115 description:
116 - Client-side caching policy.
117 type: str
118 aliases: [samba_csc_policy]
119 sambaCustomSettings:
120 default: []
121 description:
122 - Option name in C(smb.conf) and its value.
123 type: list
124 elements: dict
125 aliases: [samba_custom_settings]
126 sambaDirectoryMode:
127 default: '0755'
128 description:
129 - Directory mode.
130 type: str
131 aliases: [samba_directory_mode]
132 sambaDirectorySecurityMode:
133 default: '0777'
134 description:
135 - Directory security mode.
136 type: str
137 aliases: [samba_directory_security_mode]
138 sambaDosFilemode:
139 default: false
140 description:
141 - Users with write access may modify permissions.
142 type: bool
143 aliases: [samba_dos_filemode]
144 sambaFakeOplocks:
145 default: false
146 description:
147 - Fake oplocks.
148 type: bool
149 aliases: [samba_fake_oplocks]
150 sambaForceCreateMode:
151 default: false
152 description:
153 - Force file mode.
154 type: bool
155 aliases: [samba_force_create_mode]
156 sambaForceDirectoryMode:
157 default: false
158 description:
159 - Force directory mode.
160 type: bool
161 aliases: [samba_force_directory_mode]
162 sambaForceDirectorySecurityMode:
163 default: false
164 description:
165 - Force directory security mode.
166 type: bool
167 aliases: [samba_force_directory_security_mode]
168 sambaForceGroup:
169 description:
170 - Force group.
171 type: str
172 aliases: [samba_force_group]
173 sambaForceSecurityMode:
174 default: false
175 description:
176 - Force security mode.
177 type: bool
178 aliases: [samba_force_security_mode]
179 sambaForceUser:
180 description:
181 - Force user.
182 type: str
183 aliases: [samba_force_user]
184 sambaHideFiles:
185 description:
186 - Hide files.
187 type: str
188 aliases: [samba_hide_files]
189 sambaHideUnreadable:
190 default: false
191 description:
192 - Hide unreadable files/directories.
193 type: bool
194 aliases: [samba_hide_unreadable]
195 sambaHostsAllow:
196 default: []
197 description:
198 - Allowed host/network.
199 type: list
200 elements: str
201 aliases: [samba_hosts_allow]
202 sambaHostsDeny:
203 default: []
204 description:
205 - Denied host/network.
206 type: list
207 elements: str
208 aliases: [samba_hosts_deny]
209 sambaInheritAcls:
210 default: true
211 description:
212 - Inherit ACLs.
213 type: bool
214 aliases: [samba_inherit_acls]
215 sambaInheritOwner:
216 default: false
217 description:
218 - Create files/directories with the owner of the parent directory.
219 type: bool
220 aliases: [samba_inherit_owner]
221 sambaInheritPermissions:
222 default: false
223 description:
224 - Create files/directories with permissions of the parent directory.
225 type: bool
226 aliases: [samba_inherit_permissions]
227 sambaInvalidUsers:
228 description:
229 - Invalid users or groups.
230 type: str
231 aliases: [samba_invalid_users]
232 sambaLevel2Oplocks:
233 default: true
234 description:
235 - Level 2 oplocks.
236 type: bool
237 aliases: [samba_level_2_oplocks]
238 sambaLocking:
239 default: true
240 description:
241 - Locking.
242 type: bool
243 aliases: [samba_locking]
244 sambaMSDFSRoot:
245 default: false
246 description:
247 - MSDFS root.
248 type: bool
249 aliases: [samba_msdfs_root]
250 sambaNtAclSupport:
251 default: true
252 description:
253 - NT ACL support.
254 type: bool
255 aliases: [samba_nt_acl_support]
256 sambaOplocks:
257 default: true
258 description:
259 - Oplocks.
260 type: bool
261 aliases: [samba_oplocks]
262 sambaPostexec:
263 description:
264 - Postexec script.
265 type: str
266 aliases: [samba_postexec]
267 sambaPreexec:
268 description:
269 - Preexec script.
270 type: str
271 aliases: [samba_preexec]
272 sambaPublic:
273 default: false
274 description:
275 - Allow anonymous read-only access with a guest user.
276 type: bool
277 aliases: [samba_public]
278 sambaSecurityMode:
279 default: '0777'
280 description:
281 - Security mode.
282 type: str
283 aliases: [samba_security_mode]
284 sambaStrictLocking:
285 default: 'Auto'
286 description:
287 - Strict locking.
288 type: str
289 aliases: [samba_strict_locking]
290 sambaVFSObjects:
291 description:
292 - VFS objects.
293 type: str
294 aliases: [samba_vfs_objects]
295 sambaValidUsers:
296 description:
297 - Valid users or groups.
298 type: str
299 aliases: [samba_valid_users]
300 sambaWriteList:
301 description:
302 - Restrict write access to these users/groups.
303 type: str
304 aliases: [samba_write_list]
305 sambaWriteable:
306 default: true
307 description:
308 - Samba write access.
309 type: bool
310 aliases: [samba_writeable]
311 nfs_hosts:
312 default: []
313 description:
314 - Only allow access for this host, IP address or network.
315 type: list
316 elements: str
317 nfsCustomSettings:
318 default: []
319 description:
320 - Option name in exports file.
321 type: list
322 elements: str
323 aliases: [nfs_custom_settings]
324 """
325
326
327 EXAMPLES = r"""
328 - name: Create a share named home on the server ucs.example.com with the path /home
329 community.general.udm_share:
330 name: home
331 path: /home
332 host: ucs.example.com
333 sambaName: Home
334 """
335
336
337 RETURN = """#"""
338
339 from ansible.module_utils.basic import AnsibleModule
340
341 from ansible_collections.community.general.plugins.module_utils._univention_umc import (
342 base_dn,
343 ldap_search,
344 umc_module_for_add,
345 umc_module_for_edit,
346 )
347
348
349 def main():
350 module = AnsibleModule(
351 argument_spec=dict(
352 name=dict(required=True, type="str"),
353 ou=dict(required=True, type="str"),
354 owner=dict(type="str", default="0"),
355 group=dict(type="str", default="0"),
356 path=dict(type="path"),
357 directorymode=dict(type="str", default="00755"),
358 host=dict(type="str"),
359 root_squash=dict(type="bool", default=True),
360 subtree_checking=dict(type="bool", default=True),
361 sync=dict(type="str", default="sync"),
362 writeable=dict(type="bool", default=True),
363 sambaBlockSize=dict(type="str", aliases=["samba_block_size"]),
364 sambaBlockingLocks=dict(type="bool", aliases=["samba_blocking_locks"], default=True),
365 sambaBrowseable=dict(type="bool", aliases=["samba_browsable"], default=True),
366 sambaCreateMode=dict(type="str", aliases=["samba_create_mode"], default="0744"),
367 sambaCscPolicy=dict(type="str", aliases=["samba_csc_policy"], default="manual"),
368 sambaCustomSettings=dict(type="list", elements="dict", aliases=["samba_custom_settings"], default=[]),
369 sambaDirectoryMode=dict(type="str", aliases=["samba_directory_mode"], default="0755"),
370 sambaDirectorySecurityMode=dict(type="str", aliases=["samba_directory_security_mode"], default="0777"),
371 sambaDosFilemode=dict(type="bool", aliases=["samba_dos_filemode"], default=False),
372 sambaFakeOplocks=dict(type="bool", aliases=["samba_fake_oplocks"], default=False),
373 sambaForceCreateMode=dict(type="bool", aliases=["samba_force_create_mode"], default=False),
374 sambaForceDirectoryMode=dict(type="bool", aliases=["samba_force_directory_mode"], default=False),
375 sambaForceDirectorySecurityMode=dict(
376 type="bool", aliases=["samba_force_directory_security_mode"], default=False
377 ),
378 sambaForceGroup=dict(type="str", aliases=["samba_force_group"]),
379 sambaForceSecurityMode=dict(type="bool", aliases=["samba_force_security_mode"], default=False),
380 sambaForceUser=dict(type="str", aliases=["samba_force_user"]),
381 sambaHideFiles=dict(type="str", aliases=["samba_hide_files"]),
382 sambaHideUnreadable=dict(type="bool", aliases=["samba_hide_unreadable"], default=False),
383 sambaHostsAllow=dict(type="list", elements="str", aliases=["samba_hosts_allow"], default=[]),
384 sambaHostsDeny=dict(type="list", elements="str", aliases=["samba_hosts_deny"], default=[]),
385 sambaInheritAcls=dict(type="bool", aliases=["samba_inherit_acls"], default=True),
386 sambaInheritOwner=dict(type="bool", aliases=["samba_inherit_owner"], default=False),
387 sambaInheritPermissions=dict(type="bool", aliases=["samba_inherit_permissions"], default=False),
388 sambaInvalidUsers=dict(type="str", aliases=["samba_invalid_users"]),
389 sambaLevel2Oplocks=dict(type="bool", aliases=["samba_level_2_oplocks"], default=True),
390 sambaLocking=dict(type="bool", aliases=["samba_locking"], default=True),
391 sambaMSDFSRoot=dict(type="bool", aliases=["samba_msdfs_root"], default=False),
392 sambaName=dict(type="str", aliases=["samba_name"]),
393 sambaNtAclSupport=dict(type="bool", aliases=["samba_nt_acl_support"], default=True),
394 sambaOplocks=dict(type="bool", aliases=["samba_oplocks"], default=True),
395 sambaPostexec=dict(type="str", aliases=["samba_postexec"]),
396 sambaPreexec=dict(type="str", aliases=["samba_preexec"]),
397 sambaPublic=dict(type="bool", aliases=["samba_public"], default=False),
398 sambaSecurityMode=dict(type="str", aliases=["samba_security_mode"], default="0777"),
399 sambaStrictLocking=dict(type="str", aliases=["samba_strict_locking"], default="Auto"),
400 sambaVFSObjects=dict(type="str", aliases=["samba_vfs_objects"]),
401 sambaValidUsers=dict(type="str", aliases=["samba_valid_users"]),
402 sambaWriteList=dict(type="str", aliases=["samba_write_list"]),
403 sambaWriteable=dict(type="bool", aliases=["samba_writeable"], default=True),
404 nfs_hosts=dict(type="list", elements="str", default=[]),
405 nfsCustomSettings=dict(type="list", elements="str", aliases=["nfs_custom_settings"], default=[]),
406 state=dict(default="present", choices=["present", "absent"], type="str"),
407 ),
408 supports_check_mode=True,
409 required_if=([("state", "present", ["path", "host", "sambaName"])]),
410 )
411 name = module.params["name"]
412 state = module.params["state"]
413 changed = False
414 diff = None
415
416 obj = list(ldap_search(f"(&(objectClass=univentionShare)(cn={name}))", attr=["cn"]))
417
418 exists = bool(len(obj))
419 container = f"cn=shares,ou={module.params['ou']},{base_dn()}"
420 dn = f"cn={name},{container}"
421
422 if state == "present":
423 try:
424 if not exists:
425 obj = umc_module_for_add("shares/share", container)
426 else:
427 obj = umc_module_for_edit("shares/share", dn)
428
429 module.params["printablename"] = f"{name} ({module.params['host']})"
430 for k in obj.keys():
431 if module.params[k] is True:
432 module.params[k] = "1"
433 elif module.params[k] is False:
434 module.params[k] = "0"
435 obj[k] = module.params[k]
436
437 diff = obj.diff()
438 if exists:
439 for k in obj.keys():
440 if obj.hasChanged(k):
441 changed = True
442 else:
443 changed = True
444 if not module.check_mode:
445 if not exists:
446 obj.create()
447 elif changed:
448 obj.modify()
449 except Exception as err:
450 module.fail_json(msg=f"Creating/editing share {name} in {container} failed: {err}")
451
452 if state == "absent" and exists:
453 try:
454 obj = umc_module_for_edit("shares/share", dn)
455 if not module.check_mode:
456 obj.remove()
457 changed = True
458 except Exception as err:
459 module.fail_json(msg=f"Removing share {name} in {container} failed: {err}")
460
461 module.exit_json(changed=changed, name=name, diff=diff, container=container)
462
463
464 if __name__ == "__main__":
465 main()