| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2018, 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_service |
| 10 |
author: Cédric Parent (@cprh) |
| 11 |
short_description: Manage FreeIPA service |
| 12 |
description: |
| 13 |
- Add and delete an IPA service using IPA API. |
| 14 |
attributes: |
| 15 |
check_mode: |
| 16 |
support: full |
| 17 |
diff_mode: |
| 18 |
support: none |
| 19 |
options: |
| 20 |
krbcanonicalname: |
| 21 |
description: |
| 22 |
- Principal of the service. |
| 23 |
- Can not be changed as it is the unique identifier. |
| 24 |
required: true |
| 25 |
aliases: ["name"] |
| 26 |
type: str |
| 27 |
hosts: |
| 28 |
description: |
| 29 |
- Defines the list of C(ManagedBy) hosts. |
| 30 |
type: list |
| 31 |
elements: str |
| 32 |
force: |
| 33 |
description: |
| 34 |
- Force principal name even if host is not in DNS. |
| 35 |
type: bool |
| 36 |
skip_host_check: |
| 37 |
description: |
| 38 |
- Force service to be created even when host object does not exist to manage it. |
| 39 |
- This is only used on creation, not for updating existing services. |
| 40 |
type: bool |
| 41 |
default: false |
| 42 |
version_added: 4.7.0 |
| 43 |
state: |
| 44 |
description: State to ensure. |
| 45 |
default: present |
| 46 |
choices: ["absent", "present"] |
| 47 |
type: str |
| 48 |
extends_documentation_fragment: |
| 49 |
- community.general._ipa.documentation |
| 50 |
- community.general._ipa.connection_notes |
| 51 |
- community.general._attributes |
| 52 |
""" |
| 53 |
|
| 54 |
EXAMPLES = r""" |
| 55 |
- name: Ensure service is present |
| 56 |
community.general.ipa_service: |
| 57 |
name: http/host01.example.com |
| 58 |
state: present |
| 59 |
ipa_host: ipa.example.com |
| 60 |
ipa_user: admin |
| 61 |
ipa_pass: topsecret |
| 62 |
|
| 63 |
- name: Ensure service is absent |
| 64 |
community.general.ipa_service: |
| 65 |
name: http/host01.example.com |
| 66 |
state: absent |
| 67 |
ipa_host: ipa.example.com |
| 68 |
ipa_user: admin |
| 69 |
ipa_pass: topsecret |
| 70 |
|
| 71 |
- name: Changing Managing hosts list |
| 72 |
community.general.ipa_service: |
| 73 |
name: http/host01.example.com |
| 74 |
hosts: |
| 75 |
- host01.example.com |
| 76 |
- host02.example.com |
| 77 |
ipa_host: ipa.example.com |
| 78 |
ipa_user: admin |
| 79 |
ipa_pass: topsecret |
| 80 |
""" |
| 81 |
|
| 82 |
RETURN = r""" |
| 83 |
service: |
| 84 |
description: Service as returned by IPA API. |
| 85 |
returned: always |
| 86 |
type: dict |
| 87 |
""" |
| 88 |
|
| 89 |
import traceback |
| 90 |
|
| 91 |
from ansible.module_utils.basic import AnsibleModule |
| 92 |
|
| 93 |
from ansible_collections.community.general.plugins.module_utils._ipa import IPAClient, ipa_argument_spec |
| 94 |
|
| 95 |
|
| 96 |
class ServiceIPAClient(IPAClient): |
| 97 |
def __init__(self, module, host, port, protocol): |
| 98 |
super().__init__(module, host, port, protocol) |
| 99 |
|
| 100 |
def service_find(self, name): |
| 101 |
return self._post_json(method="service_find", name=None, item={"all": True, "krbcanonicalname": name}) |
| 102 |
|
| 103 |
def service_add(self, name, service): |
| 104 |
return self._post_json(method="service_add", name=name, item=service) |
| 105 |
|
| 106 |
def service_mod(self, name, service): |
| 107 |
return self._post_json(method="service_mod", name=name, item=service) |
| 108 |
|
| 109 |
def service_del(self, name): |
| 110 |
return self._post_json(method="service_del", name=name) |
| 111 |
|
| 112 |
def service_disable(self, name): |
| 113 |
return self._post_json(method="service_disable", name=name) |
| 114 |
|
| 115 |
def service_add_host(self, name, item): |
| 116 |
return self._post_json(method="service_add_host", name=name, item={"host": item}) |
| 117 |
|
| 118 |
def service_remove_host(self, name, item): |
| 119 |
return self._post_json(method="service_remove_host", name=name, item={"host": item}) |
| 120 |
|
| 121 |
|
| 122 |
def get_service_dict(force=None, krbcanonicalname=None, skip_host_check=None): |
| 123 |
data = {} |
| 124 |
if force is not None: |
| 125 |
data["force"] = force |
| 126 |
if krbcanonicalname is not None: |
| 127 |
data["krbcanonicalname"] = krbcanonicalname |
| 128 |
if skip_host_check is not None: |
| 129 |
data["skip_host_check"] = skip_host_check |
| 130 |
return data |
| 131 |
|
| 132 |
|
| 133 |
def get_service_diff(client, ipa_host, module_service): |
| 134 |
non_updateable_keys = ["force", "krbcanonicalname", "skip_host_check"] |
| 135 |
for key in non_updateable_keys: |
| 136 |
if key in module_service: |
| 137 |
del module_service[key] |
| 138 |
|
| 139 |
return client.get_diff(ipa_data=ipa_host, module_data=module_service) |
| 140 |
|
| 141 |
|
| 142 |
def ensure(module, client): |
| 143 |
name = module.params["krbcanonicalname"] |
| 144 |
state = module.params["state"] |
| 145 |
hosts = module.params["hosts"] |
| 146 |
|
| 147 |
ipa_service = client.service_find(name=name) |
| 148 |
module_service = get_service_dict(force=module.params["force"], skip_host_check=module.params["skip_host_check"]) |
| 149 |
changed = False |
| 150 |
if state in ["present", "enabled", "disabled"]: |
| 151 |
if not ipa_service: |
| 152 |
changed = True |
| 153 |
if not module.check_mode: |
| 154 |
client.service_add(name=name, service=module_service) |
| 155 |
else: |
| 156 |
diff = get_service_diff(client, ipa_service, module_service) |
| 157 |
if len(diff) > 0: |
| 158 |
changed = True |
| 159 |
if not module.check_mode: |
| 160 |
data = {} |
| 161 |
for key in diff: |
| 162 |
data[key] = module_service.get(key) |
| 163 |
client.service_mod(name=name, service=data) |
| 164 |
if hosts is not None: |
| 165 |
if "managedby_host" in ipa_service: |
| 166 |
for host in ipa_service["managedby_host"]: |
| 167 |
if host not in hosts: |
| 168 |
if not module.check_mode: |
| 169 |
client.service_remove_host(name=name, item=host) |
| 170 |
changed = True |
| 171 |
for host in hosts: |
| 172 |
if host not in ipa_service["managedby_host"]: |
| 173 |
if not module.check_mode: |
| 174 |
client.service_add_host(name=name, item=host) |
| 175 |
changed = True |
| 176 |
else: |
| 177 |
for host in hosts: |
| 178 |
if not module.check_mode: |
| 179 |
client.service_add_host(name=name, item=host) |
| 180 |
changed = True |
| 181 |
|
| 182 |
else: |
| 183 |
if ipa_service: |
| 184 |
changed = True |
| 185 |
if not module.check_mode: |
| 186 |
client.service_del(name=name) |
| 187 |
|
| 188 |
return changed, client.service_find(name=name) |
| 189 |
|
| 190 |
|
| 191 |
def main(): |
| 192 |
argument_spec = ipa_argument_spec() |
| 193 |
argument_spec.update( |
| 194 |
krbcanonicalname=dict(type="str", required=True, aliases=["name"]), |
| 195 |
force=dict(type="bool"), |
| 196 |
skip_host_check=dict(type="bool", default=False), |
| 197 |
hosts=dict(type="list", elements="str"), |
| 198 |
state=dict(type="str", default="present", choices=["present", "absent"]), |
| 199 |
) |
| 200 |
|
| 201 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 202 |
|
| 203 |
client = ServiceIPAClient( |
| 204 |
module=module, |
| 205 |
host=module.params["ipa_host"], |
| 206 |
port=module.params["ipa_port"], |
| 207 |
protocol=module.params["ipa_prot"], |
| 208 |
) |
| 209 |
|
| 210 |
try: |
| 211 |
client.login(username=module.params["ipa_user"], password=module.params["ipa_pass"]) |
| 212 |
changed, host = ensure(module, client) |
| 213 |
module.exit_json(changed=changed, host=host) |
| 214 |
except Exception as e: |
| 215 |
module.fail_json(msg=f"{e}", exception=traceback.format_exc()) |
| 216 |
|
| 217 |
|
| 218 |
if __name__ == "__main__": |
| 219 |
main() |