| 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_group |
| 12 |
author: |
| 13 |
- keachi (@keachi) |
| 14 |
short_description: Manage of the posix group |
| 15 |
description: |
| 16 |
- This module allows to manage user groups 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 group is present or not. |
| 31 |
type: str |
| 32 |
name: |
| 33 |
required: true |
| 34 |
description: |
| 35 |
- Name of the POSIX group. |
| 36 |
type: str |
| 37 |
description: |
| 38 |
description: |
| 39 |
- Group description. |
| 40 |
type: str |
| 41 |
position: |
| 42 |
description: |
| 43 |
- Define the whole LDAP position of the group, for example V(cn=g123m-1A,cn=classes,cn=schueler,cn=groups,ou=schule,dc=example,dc=com). |
| 44 |
type: str |
| 45 |
default: '' |
| 46 |
ou: |
| 47 |
description: |
| 48 |
- LDAP OU, for example V(school) for LDAP OU V(ou=school,dc=example,dc=com). |
| 49 |
type: str |
| 50 |
default: '' |
| 51 |
subpath: |
| 52 |
description: |
| 53 |
- Subpath inside the OU, for example V(cn=classes,cn=students,cn=groups). |
| 54 |
type: str |
| 55 |
default: "cn=groups" |
| 56 |
""" |
| 57 |
|
| 58 |
|
| 59 |
EXAMPLES = r""" |
| 60 |
- name: Create a POSIX group |
| 61 |
community.general.udm_group: |
| 62 |
name: g123m-1A |
| 63 |
|
| 64 |
# Create a POSIX group with the exact DN |
| 65 |
# C(cn=g123m-1A,cn=classes,cn=students,cn=groups,ou=school,dc=school,dc=example,dc=com) |
| 66 |
- name: Create a POSIX group with a DN |
| 67 |
community.general.udm_group: |
| 68 |
name: g123m-1A |
| 69 |
subpath: 'cn=classes,cn=students,cn=groups' |
| 70 |
ou: school |
| 71 |
|
| 72 |
# or |
| 73 |
- name: Create a POSIX group with a DN |
| 74 |
community.general.udm_group: |
| 75 |
name: g123m-1A |
| 76 |
position: 'cn=classes,cn=students,cn=groups,ou=school,dc=school,dc=example,dc=com' |
| 77 |
""" |
| 78 |
|
| 79 |
|
| 80 |
RETURN = """#""" |
| 81 |
|
| 82 |
from ansible.module_utils.basic import AnsibleModule |
| 83 |
|
| 84 |
from ansible_collections.community.general.plugins.module_utils._univention_umc import ( |
| 85 |
base_dn, |
| 86 |
ldap_search, |
| 87 |
umc_module_for_add, |
| 88 |
umc_module_for_edit, |
| 89 |
) |
| 90 |
|
| 91 |
|
| 92 |
def main(): |
| 93 |
module = AnsibleModule( |
| 94 |
argument_spec=dict( |
| 95 |
name=dict(required=True, type="str"), |
| 96 |
description=dict(type="str"), |
| 97 |
position=dict(default="", type="str"), |
| 98 |
ou=dict(default="", type="str"), |
| 99 |
subpath=dict(default="cn=groups", type="str"), |
| 100 |
state=dict(default="present", choices=["present", "absent"], type="str"), |
| 101 |
), |
| 102 |
supports_check_mode=True, |
| 103 |
) |
| 104 |
name = module.params["name"] |
| 105 |
description = module.params["description"] |
| 106 |
position = module.params["position"] |
| 107 |
ou = module.params["ou"] |
| 108 |
subpath = module.params["subpath"] |
| 109 |
state = module.params["state"] |
| 110 |
changed = False |
| 111 |
diff = None |
| 112 |
|
| 113 |
groups = list(ldap_search(f"(&(objectClass=posixGroup)(cn={name}))", attr=["cn"])) |
| 114 |
if position != "": |
| 115 |
container = position |
| 116 |
else: |
| 117 |
if ou != "": |
| 118 |
ou = f"ou={ou}," |
| 119 |
if subpath != "": |
| 120 |
subpath = f"{subpath}," |
| 121 |
container = f"{subpath}{ou}{base_dn()}" |
| 122 |
group_dn = f"cn={name},{container}" |
| 123 |
|
| 124 |
exists = bool(len(groups)) |
| 125 |
|
| 126 |
if state == "present": |
| 127 |
try: |
| 128 |
if not exists: |
| 129 |
grp = umc_module_for_add("groups/group", container) |
| 130 |
else: |
| 131 |
grp = umc_module_for_edit("groups/group", group_dn) |
| 132 |
grp["name"] = name |
| 133 |
grp["description"] = description |
| 134 |
diff = grp.diff() |
| 135 |
changed = grp.diff() != [] |
| 136 |
if not module.check_mode: |
| 137 |
if not exists: |
| 138 |
grp.create() |
| 139 |
else: |
| 140 |
grp.modify() |
| 141 |
except Exception: |
| 142 |
module.fail_json(msg=f"Creating/editing group {name} in {container} failed") |
| 143 |
|
| 144 |
if state == "absent" and exists: |
| 145 |
try: |
| 146 |
grp = umc_module_for_edit("groups/group", group_dn) |
| 147 |
if not module.check_mode: |
| 148 |
grp.remove() |
| 149 |
changed = True |
| 150 |
except Exception: |
| 151 |
module.fail_json(msg=f"Removing group {name} failed") |
| 152 |
|
| 153 |
module.exit_json(changed=changed, name=name, diff=diff, container=container) |
| 154 |
|
| 155 |
|
| 156 |
if __name__ == "__main__": |
| 157 |
main() |