| 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_role |
| 10 |
author: Thomas Krahn (@Nosmoht) |
| 11 |
short_description: Manage FreeIPA role |
| 12 |
description: |
| 13 |
- Add, modify and delete a role within FreeIPA server using FreeIPA API. |
| 14 |
attributes: |
| 15 |
check_mode: |
| 16 |
support: full |
| 17 |
diff_mode: |
| 18 |
support: none |
| 19 |
options: |
| 20 |
cn: |
| 21 |
description: |
| 22 |
- Role name. |
| 23 |
- Can not be changed as it is the unique identifier. |
| 24 |
required: true |
| 25 |
aliases: ['name'] |
| 26 |
type: str |
| 27 |
description: |
| 28 |
description: |
| 29 |
- A description of this role-group. |
| 30 |
type: str |
| 31 |
group: |
| 32 |
description: |
| 33 |
- List of group names assign to this role. |
| 34 |
- If an empty list is passed all assigned groups are unassigned from the role. |
| 35 |
- If option is omitted groups are not checked nor changed. |
| 36 |
- If option is passed all assigned groups that are not passed are unassigned from the role. |
| 37 |
type: list |
| 38 |
elements: str |
| 39 |
host: |
| 40 |
description: |
| 41 |
- List of host names to assign. |
| 42 |
- If an empty list is passed all assigned hosts are unassigned from the role. |
| 43 |
- If option is omitted hosts are not checked nor changed. |
| 44 |
- If option is passed all assigned hosts that are not passed are unassigned from the role. |
| 45 |
type: list |
| 46 |
elements: str |
| 47 |
hostgroup: |
| 48 |
description: |
| 49 |
- List of host group names to assign. |
| 50 |
- If an empty list is passed all assigned host groups are removed from the role. |
| 51 |
- If option is omitted host groups are not checked nor changed. |
| 52 |
- If option is passed all assigned hostgroups that are not passed are unassigned from the role. |
| 53 |
type: list |
| 54 |
elements: str |
| 55 |
privilege: |
| 56 |
description: |
| 57 |
- List of privileges granted to the role. |
| 58 |
- If an empty list is passed all assigned privileges are removed. |
| 59 |
- If option is omitted privileges are not checked nor changed. |
| 60 |
- If option is passed all assigned privileges that are not passed are removed. |
| 61 |
type: list |
| 62 |
elements: str |
| 63 |
service: |
| 64 |
description: |
| 65 |
- List of service names to assign. |
| 66 |
- If an empty list is passed all assigned services are removed from the role. |
| 67 |
- If option is omitted services are not checked nor changed. |
| 68 |
- If option is passed all assigned services that are not passed are removed from the role. |
| 69 |
type: list |
| 70 |
elements: str |
| 71 |
state: |
| 72 |
description: State to ensure. |
| 73 |
default: "present" |
| 74 |
choices: ["absent", "present"] |
| 75 |
type: str |
| 76 |
user: |
| 77 |
description: |
| 78 |
- List of user names to assign. |
| 79 |
- If an empty list is passed all assigned users are removed from the role. |
| 80 |
- If option is omitted users are not checked nor changed. |
| 81 |
type: list |
| 82 |
elements: str |
| 83 |
extends_documentation_fragment: |
| 84 |
- community.general._ipa.documentation |
| 85 |
- community.general._ipa.connection_notes |
| 86 |
- community.general._attributes |
| 87 |
""" |
| 88 |
|
| 89 |
EXAMPLES = r""" |
| 90 |
- name: Ensure role is present |
| 91 |
community.general.ipa_role: |
| 92 |
name: dba |
| 93 |
description: Database Administrators |
| 94 |
state: present |
| 95 |
user: |
| 96 |
- pinky |
| 97 |
- brain |
| 98 |
ipa_host: ipa.example.com |
| 99 |
ipa_user: admin |
| 100 |
ipa_pass: topsecret |
| 101 |
|
| 102 |
- name: Ensure role with certain details |
| 103 |
community.general.ipa_role: |
| 104 |
name: another-role |
| 105 |
description: Just another role |
| 106 |
group: |
| 107 |
- editors |
| 108 |
host: |
| 109 |
- host01.example.com |
| 110 |
hostgroup: |
| 111 |
- hostgroup01 |
| 112 |
privilege: |
| 113 |
- Group Administrators |
| 114 |
- User Administrators |
| 115 |
service: |
| 116 |
- service01 |
| 117 |
|
| 118 |
- name: Ensure role is absent |
| 119 |
community.general.ipa_role: |
| 120 |
name: dba |
| 121 |
state: absent |
| 122 |
ipa_host: ipa.example.com |
| 123 |
ipa_user: admin |
| 124 |
ipa_pass: topsecret |
| 125 |
""" |
| 126 |
|
| 127 |
RETURN = r""" |
| 128 |
role: |
| 129 |
description: Role as returned by IPA API. |
| 130 |
returned: always |
| 131 |
type: dict |
| 132 |
""" |
| 133 |
|
| 134 |
import traceback |
| 135 |
|
| 136 |
from ansible.module_utils.basic import AnsibleModule |
| 137 |
|
| 138 |
from ansible_collections.community.general.plugins.module_utils._ipa import IPAClient, ipa_argument_spec |
| 139 |
|
| 140 |
|
| 141 |
class RoleIPAClient(IPAClient): |
| 142 |
def __init__(self, module, host, port, protocol): |
| 143 |
super().__init__(module, host, port, protocol) |
| 144 |
|
| 145 |
def role_find(self, name): |
| 146 |
return self._post_json(method="role_find", name=None, item={"all": True, "cn": name}) |
| 147 |
|
| 148 |
def role_add(self, name, item): |
| 149 |
return self._post_json(method="role_add", name=name, item=item) |
| 150 |
|
| 151 |
def role_mod(self, name, item): |
| 152 |
return self._post_json(method="role_mod", name=name, item=item) |
| 153 |
|
| 154 |
def role_del(self, name): |
| 155 |
return self._post_json(method="role_del", name=name) |
| 156 |
|
| 157 |
def role_add_member(self, name, item): |
| 158 |
return self._post_json(method="role_add_member", name=name, item=item) |
| 159 |
|
| 160 |
def role_add_group(self, name, item): |
| 161 |
return self.role_add_member(name=name, item={"group": item}) |
| 162 |
|
| 163 |
def role_add_host(self, name, item): |
| 164 |
return self.role_add_member(name=name, item={"host": item}) |
| 165 |
|
| 166 |
def role_add_hostgroup(self, name, item): |
| 167 |
return self.role_add_member(name=name, item={"hostgroup": item}) |
| 168 |
|
| 169 |
def role_add_service(self, name, item): |
| 170 |
return self.role_add_member(name=name, item={"service": item}) |
| 171 |
|
| 172 |
def role_add_user(self, name, item): |
| 173 |
return self.role_add_member(name=name, item={"user": item}) |
| 174 |
|
| 175 |
def role_remove_member(self, name, item): |
| 176 |
return self._post_json(method="role_remove_member", name=name, item=item) |
| 177 |
|
| 178 |
def role_remove_group(self, name, item): |
| 179 |
return self.role_remove_member(name=name, item={"group": item}) |
| 180 |
|
| 181 |
def role_remove_host(self, name, item): |
| 182 |
return self.role_remove_member(name=name, item={"host": item}) |
| 183 |
|
| 184 |
def role_remove_hostgroup(self, name, item): |
| 185 |
return self.role_remove_member(name=name, item={"hostgroup": item}) |
| 186 |
|
| 187 |
def role_remove_service(self, name, item): |
| 188 |
return self.role_remove_member(name=name, item={"service": item}) |
| 189 |
|
| 190 |
def role_remove_user(self, name, item): |
| 191 |
return self.role_remove_member(name=name, item={"user": item}) |
| 192 |
|
| 193 |
def role_add_privilege(self, name, item): |
| 194 |
return self._post_json(method="role_add_privilege", name=name, item={"privilege": item}) |
| 195 |
|
| 196 |
def role_remove_privilege(self, name, item): |
| 197 |
return self._post_json(method="role_remove_privilege", name=name, item={"privilege": item}) |
| 198 |
|
| 199 |
|
| 200 |
def get_role_dict(description=None): |
| 201 |
data = {} |
| 202 |
if description is not None: |
| 203 |
data["description"] = description |
| 204 |
return data |
| 205 |
|
| 206 |
|
| 207 |
def get_role_diff(client, ipa_role, module_role): |
| 208 |
return client.get_diff(ipa_data=ipa_role, module_data=module_role) |
| 209 |
|
| 210 |
|
| 211 |
def ensure(module, client): |
| 212 |
state = module.params["state"] |
| 213 |
name = module.params["cn"] |
| 214 |
group = module.params["group"] |
| 215 |
host = module.params["host"] |
| 216 |
hostgroup = module.params["hostgroup"] |
| 217 |
privilege = module.params["privilege"] |
| 218 |
service = module.params["service"] |
| 219 |
user = module.params["user"] |
| 220 |
|
| 221 |
module_role = get_role_dict(description=module.params["description"]) |
| 222 |
ipa_role = client.role_find(name=name) |
| 223 |
|
| 224 |
changed = False |
| 225 |
if state == "present": |
| 226 |
if not ipa_role: |
| 227 |
changed = True |
| 228 |
if not module.check_mode: |
| 229 |
ipa_role = client.role_add(name=name, item=module_role) |
| 230 |
else: |
| 231 |
diff = get_role_diff(client, ipa_role, module_role) |
| 232 |
if len(diff) > 0: |
| 233 |
changed = True |
| 234 |
if not module.check_mode: |
| 235 |
data = {} |
| 236 |
for key in diff: |
| 237 |
data[key] = module_role.get(key) |
| 238 |
client.role_mod(name=name, item=data) |
| 239 |
|
| 240 |
if group is not None: |
| 241 |
changed = ( |
| 242 |
client.modify_if_diff( |
| 243 |
name, ipa_role.get("member_group", []), group, client.role_add_group, client.role_remove_group |
| 244 |
) |
| 245 |
or changed |
| 246 |
) |
| 247 |
if host is not None: |
| 248 |
changed = ( |
| 249 |
client.modify_if_diff( |
| 250 |
name, ipa_role.get("member_host", []), host, client.role_add_host, client.role_remove_host |
| 251 |
) |
| 252 |
or changed |
| 253 |
) |
| 254 |
|
| 255 |
if hostgroup is not None: |
| 256 |
changed = ( |
| 257 |
client.modify_if_diff( |
| 258 |
name, |
| 259 |
ipa_role.get("member_hostgroup", []), |
| 260 |
hostgroup, |
| 261 |
client.role_add_hostgroup, |
| 262 |
client.role_remove_hostgroup, |
| 263 |
) |
| 264 |
or changed |
| 265 |
) |
| 266 |
|
| 267 |
if privilege is not None: |
| 268 |
changed = ( |
| 269 |
client.modify_if_diff( |
| 270 |
name, |
| 271 |
ipa_role.get("memberof_privilege", []), |
| 272 |
privilege, |
| 273 |
client.role_add_privilege, |
| 274 |
client.role_remove_privilege, |
| 275 |
) |
| 276 |
or changed |
| 277 |
) |
| 278 |
if service is not None: |
| 279 |
changed = ( |
| 280 |
client.modify_if_diff( |
| 281 |
name, |
| 282 |
ipa_role.get("member_service", []), |
| 283 |
service, |
| 284 |
client.role_add_service, |
| 285 |
client.role_remove_service, |
| 286 |
) |
| 287 |
or changed |
| 288 |
) |
| 289 |
if user is not None: |
| 290 |
changed = ( |
| 291 |
client.modify_if_diff( |
| 292 |
name, ipa_role.get("member_user", []), user, client.role_add_user, client.role_remove_user |
| 293 |
) |
| 294 |
or changed |
| 295 |
) |
| 296 |
|
| 297 |
else: |
| 298 |
if ipa_role: |
| 299 |
changed = True |
| 300 |
if not module.check_mode: |
| 301 |
client.role_del(name) |
| 302 |
|
| 303 |
return changed, client.role_find(name=name) |
| 304 |
|
| 305 |
|
| 306 |
def main(): |
| 307 |
argument_spec = ipa_argument_spec() |
| 308 |
argument_spec.update( |
| 309 |
cn=dict(type="str", required=True, aliases=["name"]), |
| 310 |
description=dict(type="str"), |
| 311 |
group=dict(type="list", elements="str"), |
| 312 |
host=dict(type="list", elements="str"), |
| 313 |
hostgroup=dict(type="list", elements="str"), |
| 314 |
privilege=dict(type="list", elements="str"), |
| 315 |
service=dict(type="list", elements="str"), |
| 316 |
state=dict(type="str", default="present", choices=["present", "absent"]), |
| 317 |
user=dict(type="list", elements="str"), |
| 318 |
) |
| 319 |
|
| 320 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 321 |
|
| 322 |
client = RoleIPAClient( |
| 323 |
module=module, |
| 324 |
host=module.params["ipa_host"], |
| 325 |
port=module.params["ipa_port"], |
| 326 |
protocol=module.params["ipa_prot"], |
| 327 |
) |
| 328 |
|
| 329 |
try: |
| 330 |
client.login(username=module.params["ipa_user"], password=module.params["ipa_pass"]) |
| 331 |
changed, role = ensure(module, client) |
| 332 |
module.exit_json(changed=changed, role=role) |
| 333 |
except Exception as e: |
| 334 |
module.fail_json(msg=f"{e}", exception=traceback.format_exc()) |
| 335 |
|
| 336 |
|
| 337 |
if __name__ == "__main__": |
| 338 |
main() |