| 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_sudocmdgroup |
| 10 |
author: Thomas Krahn (@Nosmoht) |
| 11 |
short_description: Manage FreeIPA sudo command group |
| 12 |
description: |
| 13 |
- Add, modify or delete sudo command group within IPA server using IPA API. |
| 14 |
attributes: |
| 15 |
check_mode: |
| 16 |
support: full |
| 17 |
diff_mode: |
| 18 |
support: none |
| 19 |
options: |
| 20 |
cn: |
| 21 |
description: |
| 22 |
- Sudo Command Group. |
| 23 |
aliases: ['name'] |
| 24 |
required: true |
| 25 |
type: str |
| 26 |
description: |
| 27 |
description: |
| 28 |
- Group description. |
| 29 |
type: str |
| 30 |
state: |
| 31 |
description: State to ensure. |
| 32 |
default: present |
| 33 |
choices: ['absent', 'disabled', 'enabled', 'present'] |
| 34 |
type: str |
| 35 |
sudocmd: |
| 36 |
description: |
| 37 |
- List of sudo commands to assign to the group. |
| 38 |
- If an empty list is passed all assigned commands are removed from the group. |
| 39 |
- If option is omitted sudo commands are not checked nor changed. |
| 40 |
type: list |
| 41 |
elements: str |
| 42 |
extends_documentation_fragment: |
| 43 |
- community.general._ipa.documentation |
| 44 |
- community.general._ipa.connection_notes |
| 45 |
- community.general._attributes |
| 46 |
""" |
| 47 |
|
| 48 |
EXAMPLES = r""" |
| 49 |
- name: Ensure sudo command group exists |
| 50 |
community.general.ipa_sudocmdgroup: |
| 51 |
name: group01 |
| 52 |
description: Group of important commands |
| 53 |
sudocmd: |
| 54 |
- su |
| 55 |
ipa_host: ipa.example.com |
| 56 |
ipa_user: admin |
| 57 |
ipa_pass: topsecret |
| 58 |
|
| 59 |
- name: Ensure sudo command group does not exist |
| 60 |
community.general.ipa_sudocmdgroup: |
| 61 |
name: group01 |
| 62 |
state: absent |
| 63 |
ipa_host: ipa.example.com |
| 64 |
ipa_user: admin |
| 65 |
ipa_pass: topsecret |
| 66 |
""" |
| 67 |
|
| 68 |
RETURN = r""" |
| 69 |
sudocmdgroup: |
| 70 |
description: Sudo command group as returned by IPA API. |
| 71 |
returned: always |
| 72 |
type: dict |
| 73 |
""" |
| 74 |
|
| 75 |
import traceback |
| 76 |
|
| 77 |
from ansible.module_utils.basic import AnsibleModule |
| 78 |
|
| 79 |
from ansible_collections.community.general.plugins.module_utils._ipa import IPAClient, ipa_argument_spec |
| 80 |
|
| 81 |
|
| 82 |
class SudoCmdGroupIPAClient(IPAClient): |
| 83 |
def __init__(self, module, host, port, protocol): |
| 84 |
super().__init__(module, host, port, protocol) |
| 85 |
|
| 86 |
def sudocmdgroup_find(self, name): |
| 87 |
return self._post_json(method="sudocmdgroup_find", name=None, item={"all": True, "cn": name}) |
| 88 |
|
| 89 |
def sudocmdgroup_add(self, name, item): |
| 90 |
return self._post_json(method="sudocmdgroup_add", name=name, item=item) |
| 91 |
|
| 92 |
def sudocmdgroup_mod(self, name, item): |
| 93 |
return self._post_json(method="sudocmdgroup_mod", name=name, item=item) |
| 94 |
|
| 95 |
def sudocmdgroup_del(self, name): |
| 96 |
return self._post_json(method="sudocmdgroup_del", name=name) |
| 97 |
|
| 98 |
def sudocmdgroup_add_member(self, name, item): |
| 99 |
return self._post_json(method="sudocmdgroup_add_member", name=name, item=item) |
| 100 |
|
| 101 |
def sudocmdgroup_add_member_sudocmd(self, name, item): |
| 102 |
return self.sudocmdgroup_add_member(name=name, item={"sudocmd": item}) |
| 103 |
|
| 104 |
def sudocmdgroup_remove_member(self, name, item): |
| 105 |
return self._post_json(method="sudocmdgroup_remove_member", name=name, item=item) |
| 106 |
|
| 107 |
def sudocmdgroup_remove_member_sudocmd(self, name, item): |
| 108 |
return self.sudocmdgroup_remove_member(name=name, item={"sudocmd": item}) |
| 109 |
|
| 110 |
|
| 111 |
def get_sudocmdgroup_dict(description=None): |
| 112 |
data = {} |
| 113 |
if description is not None: |
| 114 |
data["description"] = description |
| 115 |
return data |
| 116 |
|
| 117 |
|
| 118 |
def get_sudocmdgroup_diff(client, ipa_sudocmdgroup, module_sudocmdgroup): |
| 119 |
return client.get_diff(ipa_data=ipa_sudocmdgroup, module_data=module_sudocmdgroup) |
| 120 |
|
| 121 |
|
| 122 |
def ensure(module, client): |
| 123 |
name = module.params["cn"] |
| 124 |
state = module.params["state"] |
| 125 |
sudocmd = module.params["sudocmd"] |
| 126 |
|
| 127 |
module_sudocmdgroup = get_sudocmdgroup_dict(description=module.params["description"]) |
| 128 |
ipa_sudocmdgroup = client.sudocmdgroup_find(name=name) |
| 129 |
|
| 130 |
changed = False |
| 131 |
if state == "present": |
| 132 |
if not ipa_sudocmdgroup: |
| 133 |
changed = True |
| 134 |
if not module.check_mode: |
| 135 |
ipa_sudocmdgroup = client.sudocmdgroup_add(name=name, item=module_sudocmdgroup) |
| 136 |
else: |
| 137 |
diff = get_sudocmdgroup_diff(client, ipa_sudocmdgroup, module_sudocmdgroup) |
| 138 |
if len(diff) > 0: |
| 139 |
changed = True |
| 140 |
if not module.check_mode: |
| 141 |
data = {} |
| 142 |
for key in diff: |
| 143 |
data[key] = module_sudocmdgroup.get(key) |
| 144 |
client.sudocmdgroup_mod(name=name, item=data) |
| 145 |
|
| 146 |
if sudocmd is not None: |
| 147 |
changed = client.modify_if_diff( |
| 148 |
name, |
| 149 |
ipa_sudocmdgroup.get("member_sudocmd", []), |
| 150 |
sudocmd, |
| 151 |
client.sudocmdgroup_add_member_sudocmd, |
| 152 |
client.sudocmdgroup_remove_member_sudocmd, |
| 153 |
) |
| 154 |
else: |
| 155 |
if ipa_sudocmdgroup: |
| 156 |
changed = True |
| 157 |
if not module.check_mode: |
| 158 |
client.sudocmdgroup_del(name=name) |
| 159 |
|
| 160 |
return changed, client.sudocmdgroup_find(name=name) |
| 161 |
|
| 162 |
|
| 163 |
def main(): |
| 164 |
argument_spec = ipa_argument_spec() |
| 165 |
argument_spec.update( |
| 166 |
cn=dict(type="str", required=True, aliases=["name"]), |
| 167 |
description=dict(type="str"), |
| 168 |
state=dict(type="str", default="present", choices=["present", "absent", "enabled", "disabled"]), |
| 169 |
sudocmd=dict(type="list", elements="str"), |
| 170 |
) |
| 171 |
|
| 172 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 173 |
|
| 174 |
client = SudoCmdGroupIPAClient( |
| 175 |
module=module, |
| 176 |
host=module.params["ipa_host"], |
| 177 |
port=module.params["ipa_port"], |
| 178 |
protocol=module.params["ipa_prot"], |
| 179 |
) |
| 180 |
try: |
| 181 |
client.login(username=module.params["ipa_user"], password=module.params["ipa_pass"]) |
| 182 |
changed, sudocmdgroup = ensure(module, client) |
| 183 |
module.exit_json(changed=changed, sudorule=sudocmdgroup) |
| 184 |
except Exception as e: |
| 185 |
module.fail_json(msg=f"{e}", exception=traceback.format_exc()) |
| 186 |
|
| 187 |
|
| 188 |
if __name__ == "__main__": |
| 189 |
main() |