| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2017, Ansible Project |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
module: ipa_group |
| 10 |
author: Thomas Krahn (@Nosmoht) |
| 11 |
short_description: Manage FreeIPA group |
| 12 |
description: |
| 13 |
- Add, modify and delete group within IPA server. |
| 14 |
attributes: |
| 15 |
check_mode: |
| 16 |
support: full |
| 17 |
diff_mode: |
| 18 |
support: none |
| 19 |
options: |
| 20 |
append: |
| 21 |
description: |
| 22 |
- If V(true), add the listed O(user) and O(group) to the group members. |
| 23 |
- If V(false), only the listed O(user) and O(group) are set as group members, removing any other members. |
| 24 |
default: false |
| 25 |
type: bool |
| 26 |
version_added: 4.0.0 |
| 27 |
cn: |
| 28 |
description: |
| 29 |
- Canonical name. |
| 30 |
- Can not be changed as it is the unique identifier. |
| 31 |
required: true |
| 32 |
aliases: ['name'] |
| 33 |
type: str |
| 34 |
description: |
| 35 |
description: |
| 36 |
- Description of the group. |
| 37 |
type: str |
| 38 |
external: |
| 39 |
description: |
| 40 |
- Allow adding external non-IPA members from trusted domains. |
| 41 |
type: bool |
| 42 |
gidnumber: |
| 43 |
description: |
| 44 |
- GID (use this option to set it manually). |
| 45 |
aliases: ['gid'] |
| 46 |
type: str |
| 47 |
group: |
| 48 |
description: |
| 49 |
- List of group names assigned to this group. |
| 50 |
- If O(append=false) and an empty list is passed all groups are removed from this group. |
| 51 |
- Groups that are already assigned but not passed are removed. |
| 52 |
- If O(append=true) the listed groups are assigned without removing other groups. |
| 53 |
- If option is omitted assigned groups are not checked or changed. |
| 54 |
type: list |
| 55 |
elements: str |
| 56 |
nonposix: |
| 57 |
description: |
| 58 |
- Create as a non-POSIX group. |
| 59 |
type: bool |
| 60 |
user: |
| 61 |
description: |
| 62 |
- List of user names assigned to this group. |
| 63 |
- If O(append=false) and an empty list is passed all users are removed from this group. |
| 64 |
- Users that are already assigned but not passed are removed. |
| 65 |
- If O(append=true) the listed users are assigned without removing other users. |
| 66 |
- If option is omitted assigned users are not checked or changed. |
| 67 |
type: list |
| 68 |
elements: str |
| 69 |
external_user: |
| 70 |
description: |
| 71 |
- List of external users assigned to this group. |
| 72 |
- Behaves identically to O(user) with respect to O(append) attribute. |
| 73 |
- List entries can be in V(DOMAIN\\\\username) or SID format. |
| 74 |
- Unless SIDs are provided, the module always attempts to make changes even if the group already has all the users. |
| 75 |
This is because only SIDs are returned by IPA query. |
| 76 |
- O(external=true) is needed for this option to work. |
| 77 |
type: list |
| 78 |
elements: str |
| 79 |
version_added: 6.3.0 |
| 80 |
state: |
| 81 |
description: |
| 82 |
- State to ensure. |
| 83 |
default: "present" |
| 84 |
choices: ["absent", "present"] |
| 85 |
type: str |
| 86 |
extends_documentation_fragment: |
| 87 |
- community.general._ipa.documentation |
| 88 |
- community.general._ipa.connection_notes |
| 89 |
- community.general._attributes |
| 90 |
""" |
| 91 |
|
| 92 |
EXAMPLES = r""" |
| 93 |
- name: Ensure group is present |
| 94 |
community.general.ipa_group: |
| 95 |
name: oinstall |
| 96 |
gidnumber: '54321' |
| 97 |
state: present |
| 98 |
ipa_host: ipa.example.com |
| 99 |
ipa_user: admin |
| 100 |
ipa_pass: topsecret |
| 101 |
|
| 102 |
- name: Ensure that groups sysops and appops are assigned to ops but no other group |
| 103 |
community.general.ipa_group: |
| 104 |
name: ops |
| 105 |
group: |
| 106 |
- sysops |
| 107 |
- appops |
| 108 |
ipa_host: ipa.example.com |
| 109 |
ipa_user: admin |
| 110 |
ipa_pass: topsecret |
| 111 |
|
| 112 |
- name: Ensure that users linus and larry are assign to the group, but no other user |
| 113 |
community.general.ipa_group: |
| 114 |
name: sysops |
| 115 |
user: |
| 116 |
- linus |
| 117 |
- larry |
| 118 |
ipa_host: ipa.example.com |
| 119 |
ipa_user: admin |
| 120 |
ipa_pass: topsecret |
| 121 |
|
| 122 |
- name: Ensure that new starter named john is member of the group, without removing other members |
| 123 |
community.general.ipa_group: |
| 124 |
name: developers |
| 125 |
user: |
| 126 |
- john |
| 127 |
append: true |
| 128 |
state: present |
| 129 |
ipa_host: ipa.example.com |
| 130 |
ipa_user: admin |
| 131 |
ipa_pass: topsecret |
| 132 |
|
| 133 |
- name: Add external user to a group |
| 134 |
community.general.ipa_group: |
| 135 |
name: developers |
| 136 |
external: true |
| 137 |
append: true |
| 138 |
external_user: |
| 139 |
- S-1-5-21-123-1234-12345-63421 |
| 140 |
ipa_host: ipa.example.com |
| 141 |
ipa_user: admin |
| 142 |
ipa_pass: topsecret |
| 143 |
|
| 144 |
- name: Add a user from MYDOMAIN |
| 145 |
community.general.ipa_group: |
| 146 |
name: developers |
| 147 |
external: true |
| 148 |
append: true |
| 149 |
external_user: |
| 150 |
- MYDOMAIN\\john |
| 151 |
ipa_host: ipa.example.com |
| 152 |
ipa_user: admin |
| 153 |
ipa_pass: topsecret |
| 154 |
|
| 155 |
- name: Ensure group is absent |
| 156 |
community.general.ipa_group: |
| 157 |
name: sysops |
| 158 |
state: absent |
| 159 |
ipa_host: ipa.example.com |
| 160 |
ipa_user: admin |
| 161 |
ipa_pass: topsecret |
| 162 |
""" |
| 163 |
|
| 164 |
RETURN = r""" |
| 165 |
group: |
| 166 |
description: Group as returned by IPA API. |
| 167 |
returned: always |
| 168 |
type: dict |
| 169 |
""" |
| 170 |
|
| 171 |
import traceback |
| 172 |
|
| 173 |
from ansible.module_utils.basic import AnsibleModule |
| 174 |
|
| 175 |
from ansible_collections.community.general.plugins.module_utils._ipa import IPAClient, ipa_argument_spec |
| 176 |
|
| 177 |
|
| 178 |
class GroupIPAClient(IPAClient): |
| 179 |
def __init__(self, module, host, port, protocol): |
| 180 |
super().__init__(module, host, port, protocol) |
| 181 |
|
| 182 |
def group_find(self, name): |
| 183 |
return self._post_json(method="group_find", name=None, item={"all": True, "cn": name}) |
| 184 |
|
| 185 |
def group_add(self, name, item): |
| 186 |
return self._post_json(method="group_add", name=name, item=item) |
| 187 |
|
| 188 |
def group_mod(self, name, item): |
| 189 |
return self._post_json(method="group_mod", name=name, item=item) |
| 190 |
|
| 191 |
def group_del(self, name): |
| 192 |
return self._post_json(method="group_del", name=name) |
| 193 |
|
| 194 |
def group_add_member(self, name, item): |
| 195 |
return self._post_json(method="group_add_member", name=name, item=item) |
| 196 |
|
| 197 |
def group_add_member_group(self, name, item): |
| 198 |
return self.group_add_member(name=name, item={"group": item}) |
| 199 |
|
| 200 |
def group_add_member_user(self, name, item): |
| 201 |
return self.group_add_member(name=name, item={"user": item}) |
| 202 |
|
| 203 |
def group_add_member_externaluser(self, name, item): |
| 204 |
return self.group_add_member(name=name, item={"ipaexternalmember": item}) |
| 205 |
|
| 206 |
def group_remove_member(self, name, item): |
| 207 |
return self._post_json(method="group_remove_member", name=name, item=item) |
| 208 |
|
| 209 |
def group_remove_member_group(self, name, item): |
| 210 |
return self.group_remove_member(name=name, item={"group": item}) |
| 211 |
|
| 212 |
def group_remove_member_user(self, name, item): |
| 213 |
return self.group_remove_member(name=name, item={"user": item}) |
| 214 |
|
| 215 |
def group_remove_member_externaluser(self, name, item): |
| 216 |
return self.group_remove_member(name=name, item={"ipaexternalmember": item}) |
| 217 |
|
| 218 |
|
| 219 |
def get_group_dict(description=None, external=None, gid=None, nonposix=None): |
| 220 |
group = {} |
| 221 |
if description is not None: |
| 222 |
group["description"] = description |
| 223 |
if external is not None: |
| 224 |
group["external"] = external |
| 225 |
if gid is not None: |
| 226 |
group["gidnumber"] = gid |
| 227 |
if nonposix is not None: |
| 228 |
group["nonposix"] = nonposix |
| 229 |
return group |
| 230 |
|
| 231 |
|
| 232 |
def get_group_diff(client, ipa_group, module_group): |
| 233 |
# With group_add attribute nonposix is passed, whereas with group_mod only posix can be passed. |
| 234 |
if "nonposix" in module_group: |
| 235 |
# Only non-posix groups can be changed to posix |
| 236 |
if not module_group["nonposix"] and ipa_group.get("nonposix"): |
| 237 |
module_group["posix"] = True |
| 238 |
del module_group["nonposix"] |
| 239 |
|
| 240 |
if "external" in module_group: |
| 241 |
is_external_in_ipa = "ipaexternalgroup" in ipa_group.get("objectclass", []) |
| 242 |
if module_group["external"] == is_external_in_ipa: |
| 243 |
del module_group["external"] |
| 244 |
|
| 245 |
return client.get_diff(ipa_data=ipa_group, module_data=module_group) |
| 246 |
|
| 247 |
|
| 248 |
def ensure(module, client): |
| 249 |
state = module.params["state"] |
| 250 |
name = module.params["cn"] |
| 251 |
group = module.params["group"] |
| 252 |
user = module.params["user"] |
| 253 |
external = module.params["external"] |
| 254 |
external_user = module.params["external_user"] |
| 255 |
append = module.params["append"] |
| 256 |
|
| 257 |
module_group = get_group_dict( |
| 258 |
description=module.params["description"], |
| 259 |
external=external, |
| 260 |
gid=module.params["gidnumber"], |
| 261 |
nonposix=module.params["nonposix"], |
| 262 |
) |
| 263 |
ipa_group = client.group_find(name=name) |
| 264 |
|
| 265 |
if not (external or external_user is None): |
| 266 |
module.fail_json("external_user can only be set if external = True") |
| 267 |
|
| 268 |
changed = False |
| 269 |
if state == "present": |
| 270 |
if not ipa_group: |
| 271 |
changed = True |
| 272 |
if not module.check_mode: |
| 273 |
ipa_group = client.group_add(name, item=module_group) |
| 274 |
else: |
| 275 |
diff = get_group_diff(client, ipa_group, module_group) |
| 276 |
if len(diff) > 0: |
| 277 |
changed = True |
| 278 |
if not module.check_mode: |
| 279 |
data = {} |
| 280 |
for key in diff: |
| 281 |
data[key] = module_group.get(key) |
| 282 |
client.group_mod(name=name, item=data) |
| 283 |
|
| 284 |
if group is not None: |
| 285 |
changed = ( |
| 286 |
client.modify_if_diff( |
| 287 |
name, |
| 288 |
ipa_group.get("member_group", []), |
| 289 |
group, |
| 290 |
client.group_add_member_group, |
| 291 |
client.group_remove_member_group, |
| 292 |
append=append, |
| 293 |
) |
| 294 |
or changed |
| 295 |
) |
| 296 |
|
| 297 |
if user is not None: |
| 298 |
changed = ( |
| 299 |
client.modify_if_diff( |
| 300 |
name, |
| 301 |
ipa_group.get("member_user", []), |
| 302 |
user, |
| 303 |
client.group_add_member_user, |
| 304 |
client.group_remove_member_user, |
| 305 |
append=append, |
| 306 |
) |
| 307 |
or changed |
| 308 |
) |
| 309 |
|
| 310 |
if external_user is not None: |
| 311 |
changed = ( |
| 312 |
client.modify_if_diff( |
| 313 |
name, |
| 314 |
ipa_group.get("ipaexternalmember", []), |
| 315 |
external_user, |
| 316 |
client.group_add_member_externaluser, |
| 317 |
client.group_remove_member_externaluser, |
| 318 |
append=append, |
| 319 |
) |
| 320 |
or changed |
| 321 |
) |
| 322 |
else: |
| 323 |
if ipa_group: |
| 324 |
changed = True |
| 325 |
if not module.check_mode: |
| 326 |
client.group_del(name) |
| 327 |
|
| 328 |
return changed, client.group_find(name=name) |
| 329 |
|
| 330 |
|
| 331 |
def main(): |
| 332 |
argument_spec = ipa_argument_spec() |
| 333 |
argument_spec.update( |
| 334 |
cn=dict(type="str", required=True, aliases=["name"]), |
| 335 |
description=dict(type="str"), |
| 336 |
external=dict(type="bool"), |
| 337 |
external_user=dict(type="list", elements="str"), |
| 338 |
gidnumber=dict(type="str", aliases=["gid"]), |
| 339 |
group=dict(type="list", elements="str"), |
| 340 |
nonposix=dict(type="bool"), |
| 341 |
state=dict(type="str", default="present", choices=["present", "absent"]), |
| 342 |
user=dict(type="list", elements="str"), |
| 343 |
append=dict(type="bool", default=False), |
| 344 |
) |
| 345 |
|
| 346 |
module = AnsibleModule( |
| 347 |
argument_spec=argument_spec, |
| 348 |
supports_check_mode=True, |
| 349 |
) |
| 350 |
|
| 351 |
client = GroupIPAClient( |
| 352 |
module=module, |
| 353 |
host=module.params["ipa_host"], |
| 354 |
port=module.params["ipa_port"], |
| 355 |
protocol=module.params["ipa_prot"], |
| 356 |
) |
| 357 |
try: |
| 358 |
client.login(username=module.params["ipa_user"], password=module.params["ipa_pass"]) |
| 359 |
changed, group = ensure(module, client) |
| 360 |
module.exit_json(changed=changed, group=group) |
| 361 |
except Exception as e: |
| 362 |
module.fail_json(msg=f"{e}", exception=traceback.format_exc()) |
| 363 |
|
| 364 |
|
| 365 |
if __name__ == "__main__": |
| 366 |
main() |