| 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_hostgroup |
| 10 |
author: Thomas Krahn (@Nosmoht) |
| 11 |
short_description: Manage FreeIPA host-group |
| 12 |
description: |
| 13 |
- Add, modify and delete an IPA host-group using IPA API. |
| 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(host) to the O(hostgroup). |
| 23 |
- If V(false), only the listed O(host) is set in O(hostgroup), removing any other hosts. |
| 24 |
default: false |
| 25 |
type: bool |
| 26 |
version_added: 6.6.0 |
| 27 |
cn: |
| 28 |
description: |
| 29 |
- Name of host-group. |
| 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. |
| 37 |
type: str |
| 38 |
host: |
| 39 |
description: |
| 40 |
- List of hosts that belong to the host-group. |
| 41 |
- If an empty list is passed all hosts are removed from the group. |
| 42 |
- If option is omitted hosts are not checked nor changed. |
| 43 |
- If option is passed all assigned hosts that are not passed are unassigned from the group. |
| 44 |
type: list |
| 45 |
elements: str |
| 46 |
hostgroup: |
| 47 |
description: |
| 48 |
- List of host-groups than belong to that host-group. |
| 49 |
- If an empty list is passed all host-groups are removed from the group. |
| 50 |
- If option is omitted host-groups are not checked nor changed. |
| 51 |
- If option is passed all assigned hostgroups that are not passed are unassigned from the group. |
| 52 |
type: list |
| 53 |
elements: str |
| 54 |
state: |
| 55 |
description: |
| 56 |
- State to ensure. |
| 57 |
- V("absent") and V("disabled") give the same results. |
| 58 |
- V("present") and V("enabled") give the same results. |
| 59 |
default: "present" |
| 60 |
choices: ["absent", "disabled", "enabled", "present"] |
| 61 |
type: str |
| 62 |
extends_documentation_fragment: |
| 63 |
- community.general._ipa.documentation |
| 64 |
- community.general._ipa.connection_notes |
| 65 |
- community.general._attributes |
| 66 |
""" |
| 67 |
|
| 68 |
EXAMPLES = r""" |
| 69 |
- name: Ensure host-group databases is present |
| 70 |
community.general.ipa_hostgroup: |
| 71 |
name: databases |
| 72 |
state: present |
| 73 |
host: |
| 74 |
- db.example.com |
| 75 |
hostgroup: |
| 76 |
- mysql-server |
| 77 |
- oracle-server |
| 78 |
ipa_host: ipa.example.com |
| 79 |
ipa_user: admin |
| 80 |
ipa_pass: topsecret |
| 81 |
|
| 82 |
- name: Ensure host-group databases is absent |
| 83 |
community.general.ipa_hostgroup: |
| 84 |
name: databases |
| 85 |
state: absent |
| 86 |
ipa_host: ipa.example.com |
| 87 |
ipa_user: admin |
| 88 |
ipa_pass: topsecret |
| 89 |
""" |
| 90 |
|
| 91 |
RETURN = r""" |
| 92 |
hostgroup: |
| 93 |
description: Hostgroup as returned by IPA API. |
| 94 |
returned: always |
| 95 |
type: dict |
| 96 |
""" |
| 97 |
|
| 98 |
import traceback |
| 99 |
|
| 100 |
from ansible.module_utils.basic import AnsibleModule |
| 101 |
|
| 102 |
from ansible_collections.community.general.plugins.module_utils._ipa import IPAClient, ipa_argument_spec |
| 103 |
|
| 104 |
|
| 105 |
class HostGroupIPAClient(IPAClient): |
| 106 |
def __init__(self, module, host, port, protocol): |
| 107 |
super().__init__(module, host, port, protocol) |
| 108 |
|
| 109 |
def hostgroup_find(self, name): |
| 110 |
return self._post_json(method="hostgroup_find", name=None, item={"all": True, "cn": name}) |
| 111 |
|
| 112 |
def hostgroup_add(self, name, item): |
| 113 |
return self._post_json(method="hostgroup_add", name=name, item=item) |
| 114 |
|
| 115 |
def hostgroup_mod(self, name, item): |
| 116 |
return self._post_json(method="hostgroup_mod", name=name, item=item) |
| 117 |
|
| 118 |
def hostgroup_del(self, name): |
| 119 |
return self._post_json(method="hostgroup_del", name=name) |
| 120 |
|
| 121 |
def hostgroup_add_member(self, name, item): |
| 122 |
return self._post_json(method="hostgroup_add_member", name=name, item=item) |
| 123 |
|
| 124 |
def hostgroup_add_host(self, name, item): |
| 125 |
return self.hostgroup_add_member(name=name, item={"host": item}) |
| 126 |
|
| 127 |
def hostgroup_add_hostgroup(self, name, item): |
| 128 |
return self.hostgroup_add_member(name=name, item={"hostgroup": item}) |
| 129 |
|
| 130 |
def hostgroup_remove_member(self, name, item): |
| 131 |
return self._post_json(method="hostgroup_remove_member", name=name, item=item) |
| 132 |
|
| 133 |
def hostgroup_remove_host(self, name, item): |
| 134 |
return self.hostgroup_remove_member(name=name, item={"host": item}) |
| 135 |
|
| 136 |
def hostgroup_remove_hostgroup(self, name, item): |
| 137 |
return self.hostgroup_remove_member(name=name, item={"hostgroup": item}) |
| 138 |
|
| 139 |
|
| 140 |
def get_hostgroup_dict(description=None): |
| 141 |
data = {} |
| 142 |
if description is not None: |
| 143 |
data["description"] = description |
| 144 |
return data |
| 145 |
|
| 146 |
|
| 147 |
def get_hostgroup_diff(client, ipa_hostgroup, module_hostgroup): |
| 148 |
return client.get_diff(ipa_data=ipa_hostgroup, module_data=module_hostgroup) |
| 149 |
|
| 150 |
|
| 151 |
def ensure(module, client): |
| 152 |
name = module.params["cn"] |
| 153 |
state = module.params["state"] |
| 154 |
host = module.params["host"] |
| 155 |
hostgroup = module.params["hostgroup"] |
| 156 |
append = module.params["append"] |
| 157 |
|
| 158 |
ipa_hostgroup = client.hostgroup_find(name=name) |
| 159 |
module_hostgroup = get_hostgroup_dict(description=module.params["description"]) |
| 160 |
|
| 161 |
changed = False |
| 162 |
if state in ["present", "enabled"]: |
| 163 |
if not ipa_hostgroup: |
| 164 |
changed = True |
| 165 |
if not module.check_mode: |
| 166 |
ipa_hostgroup = client.hostgroup_add(name=name, item=module_hostgroup) |
| 167 |
else: |
| 168 |
diff = get_hostgroup_diff(client, ipa_hostgroup, module_hostgroup) |
| 169 |
if len(diff) > 0: |
| 170 |
changed = True |
| 171 |
if not module.check_mode: |
| 172 |
data = {} |
| 173 |
for key in diff: |
| 174 |
data[key] = module_hostgroup.get(key) |
| 175 |
client.hostgroup_mod(name=name, item=data) |
| 176 |
|
| 177 |
if host is not None: |
| 178 |
changed = ( |
| 179 |
client.modify_if_diff( |
| 180 |
name, |
| 181 |
ipa_hostgroup.get("member_host", []), |
| 182 |
[item.lower() for item in host], |
| 183 |
client.hostgroup_add_host, |
| 184 |
client.hostgroup_remove_host, |
| 185 |
append=append, |
| 186 |
) |
| 187 |
or changed |
| 188 |
) |
| 189 |
|
| 190 |
if hostgroup is not None: |
| 191 |
changed = ( |
| 192 |
client.modify_if_diff( |
| 193 |
name, |
| 194 |
ipa_hostgroup.get("member_hostgroup", []), |
| 195 |
[item.lower() for item in hostgroup], |
| 196 |
client.hostgroup_add_hostgroup, |
| 197 |
client.hostgroup_remove_hostgroup, |
| 198 |
append=append, |
| 199 |
) |
| 200 |
or changed |
| 201 |
) |
| 202 |
|
| 203 |
else: |
| 204 |
if ipa_hostgroup: |
| 205 |
changed = True |
| 206 |
if not module.check_mode: |
| 207 |
client.hostgroup_del(name=name) |
| 208 |
|
| 209 |
return changed, client.hostgroup_find(name=name) |
| 210 |
|
| 211 |
|
| 212 |
def main(): |
| 213 |
argument_spec = ipa_argument_spec() |
| 214 |
argument_spec.update( |
| 215 |
cn=dict(type="str", required=True, aliases=["name"]), |
| 216 |
description=dict(type="str"), |
| 217 |
host=dict(type="list", elements="str"), |
| 218 |
hostgroup=dict(type="list", elements="str"), |
| 219 |
state=dict(type="str", default="present", choices=["present", "absent", "enabled", "disabled"]), |
| 220 |
append=dict(type="bool", default=False), |
| 221 |
) |
| 222 |
|
| 223 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 224 |
|
| 225 |
client = HostGroupIPAClient( |
| 226 |
module=module, |
| 227 |
host=module.params["ipa_host"], |
| 228 |
port=module.params["ipa_port"], |
| 229 |
protocol=module.params["ipa_prot"], |
| 230 |
) |
| 231 |
|
| 232 |
try: |
| 233 |
client.login(username=module.params["ipa_user"], password=module.params["ipa_pass"]) |
| 234 |
changed, hostgroup = ensure(module, client) |
| 235 |
module.exit_json(changed=changed, hostgroup=hostgroup) |
| 236 |
except Exception as e: |
| 237 |
module.fail_json(msg=f"{e}", exception=traceback.format_exc()) |
| 238 |
|
| 239 |
|
| 240 |
if __name__ == "__main__": |
| 241 |
main() |