| 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_sudocmd |
| 10 |
author: Thomas Krahn (@Nosmoht) |
| 11 |
short_description: Manage FreeIPA sudo command |
| 12 |
description: |
| 13 |
- Add, modify or delete sudo command within FreeIPA server using FreeIPA API. |
| 14 |
attributes: |
| 15 |
check_mode: |
| 16 |
support: full |
| 17 |
diff_mode: |
| 18 |
support: none |
| 19 |
options: |
| 20 |
sudocmd: |
| 21 |
description: |
| 22 |
- Sudo command. |
| 23 |
aliases: ['name'] |
| 24 |
required: true |
| 25 |
type: str |
| 26 |
description: |
| 27 |
description: |
| 28 |
- A description of this command. |
| 29 |
type: str |
| 30 |
state: |
| 31 |
description: State to ensure. |
| 32 |
default: present |
| 33 |
choices: ['absent', 'disabled', 'enabled', 'present'] |
| 34 |
type: str |
| 35 |
extends_documentation_fragment: |
| 36 |
- community.general._ipa.documentation |
| 37 |
- community.general._ipa.connection_notes |
| 38 |
- community.general._attributes |
| 39 |
""" |
| 40 |
|
| 41 |
EXAMPLES = r""" |
| 42 |
- name: Ensure sudo command exists |
| 43 |
community.general.ipa_sudocmd: |
| 44 |
name: su |
| 45 |
description: Allow to run su via sudo |
| 46 |
ipa_host: ipa.example.com |
| 47 |
ipa_user: admin |
| 48 |
ipa_pass: topsecret |
| 49 |
|
| 50 |
- name: Ensure sudo command does not exist |
| 51 |
community.general.ipa_sudocmd: |
| 52 |
name: su |
| 53 |
state: absent |
| 54 |
ipa_host: ipa.example.com |
| 55 |
ipa_user: admin |
| 56 |
ipa_pass: topsecret |
| 57 |
""" |
| 58 |
|
| 59 |
RETURN = r""" |
| 60 |
sudocmd: |
| 61 |
description: Sudo command as return from IPA API. |
| 62 |
returned: always |
| 63 |
type: dict |
| 64 |
""" |
| 65 |
|
| 66 |
import traceback |
| 67 |
|
| 68 |
from ansible.module_utils.basic import AnsibleModule |
| 69 |
|
| 70 |
from ansible_collections.community.general.plugins.module_utils._ipa import IPAClient, ipa_argument_spec |
| 71 |
|
| 72 |
|
| 73 |
class SudoCmdIPAClient(IPAClient): |
| 74 |
def __init__(self, module, host, port, protocol): |
| 75 |
super().__init__(module, host, port, protocol) |
| 76 |
|
| 77 |
def sudocmd_find(self, name): |
| 78 |
return self._post_json(method="sudocmd_find", name=None, item={"all": True, "sudocmd": name}) |
| 79 |
|
| 80 |
def sudocmd_add(self, name, item): |
| 81 |
return self._post_json(method="sudocmd_add", name=name, item=item) |
| 82 |
|
| 83 |
def sudocmd_mod(self, name, item): |
| 84 |
return self._post_json(method="sudocmd_mod", name=name, item=item) |
| 85 |
|
| 86 |
def sudocmd_del(self, name): |
| 87 |
return self._post_json(method="sudocmd_del", name=name) |
| 88 |
|
| 89 |
|
| 90 |
def get_sudocmd_dict(description=None): |
| 91 |
data = {} |
| 92 |
if description is not None: |
| 93 |
data["description"] = description |
| 94 |
return data |
| 95 |
|
| 96 |
|
| 97 |
def get_sudocmd_diff(client, ipa_sudocmd, module_sudocmd): |
| 98 |
return client.get_diff(ipa_data=ipa_sudocmd, module_data=module_sudocmd) |
| 99 |
|
| 100 |
|
| 101 |
def ensure(module, client): |
| 102 |
name = module.params["sudocmd"] |
| 103 |
state = module.params["state"] |
| 104 |
|
| 105 |
module_sudocmd = get_sudocmd_dict(description=module.params["description"]) |
| 106 |
ipa_sudocmd = client.sudocmd_find(name=name) |
| 107 |
|
| 108 |
changed = False |
| 109 |
if state == "present": |
| 110 |
if not ipa_sudocmd: |
| 111 |
changed = True |
| 112 |
if not module.check_mode: |
| 113 |
client.sudocmd_add(name=name, item=module_sudocmd) |
| 114 |
else: |
| 115 |
diff = get_sudocmd_diff(client, ipa_sudocmd, module_sudocmd) |
| 116 |
if len(diff) > 0: |
| 117 |
changed = True |
| 118 |
if not module.check_mode: |
| 119 |
data = {} |
| 120 |
for key in diff: |
| 121 |
data[key] = module_sudocmd.get(key) |
| 122 |
client.sudocmd_mod(name=name, item=data) |
| 123 |
else: |
| 124 |
if ipa_sudocmd: |
| 125 |
changed = True |
| 126 |
if not module.check_mode: |
| 127 |
client.sudocmd_del(name=name) |
| 128 |
|
| 129 |
return changed, client.sudocmd_find(name=name) |
| 130 |
|
| 131 |
|
| 132 |
def main(): |
| 133 |
argument_spec = ipa_argument_spec() |
| 134 |
argument_spec.update( |
| 135 |
description=dict(type="str"), |
| 136 |
state=dict(type="str", default="present", choices=["present", "absent", "enabled", "disabled"]), |
| 137 |
sudocmd=dict(type="str", required=True, aliases=["name"]), |
| 138 |
) |
| 139 |
|
| 140 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 141 |
|
| 142 |
client = SudoCmdIPAClient( |
| 143 |
module=module, |
| 144 |
host=module.params["ipa_host"], |
| 145 |
port=module.params["ipa_port"], |
| 146 |
protocol=module.params["ipa_prot"], |
| 147 |
) |
| 148 |
try: |
| 149 |
client.login(username=module.params["ipa_user"], password=module.params["ipa_pass"]) |
| 150 |
changed, sudocmd = ensure(module, client) |
| 151 |
module.exit_json(changed=changed, sudocmd=sudocmd) |
| 152 |
except Exception as e: |
| 153 |
module.fail_json(msg=f"{e}", exception=traceback.format_exc()) |
| 154 |
|
| 155 |
|
| 156 |
if __name__ == "__main__": |
| 157 |
main() |