| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2025 Aleksandr Gabidullin <[email protected]> |
| 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: sssd_info |
| 10 |
version_added: 12.2.0 |
| 11 |
short_description: Check SSSD domain status using D-Bus |
| 12 |
description: |
| 13 |
- Check the online status of SSSD domains, list domains, and retrieve active servers using D-Bus. |
| 14 |
author: "Aleksandr Gabidullin (@a-gabidullin)" |
| 15 |
requirements: |
| 16 |
- dbus |
| 17 |
- SSSD needs to be running |
| 18 |
options: |
| 19 |
action: |
| 20 |
description: |
| 21 |
- The action to perform. |
| 22 |
type: str |
| 23 |
required: true |
| 24 |
choices: |
| 25 |
domain_status: Check if domain is online. |
| 26 |
domain_list: List all configured domains. |
| 27 |
active_servers: Get active servers for domain. |
| 28 |
list_servers: List all servers for domain. |
| 29 |
domain: |
| 30 |
description: |
| 31 |
- Domain name to check. |
| 32 |
- Required unless O(action=domain_list). |
| 33 |
- When O(action=domain_list), this parameter is ignored and the module returns a list of all configured domains. |
| 34 |
type: str |
| 35 |
server_type: |
| 36 |
description: |
| 37 |
- Required parameter when O(action=active_servers) and O(action=list_servers). |
| 38 |
- Optional and ignored for all other actions. |
| 39 |
- At this point, the module supports ONLY the types C(IPA) for FreeIPA servers and C(AD). |
| 40 |
type: str |
| 41 |
choices: ['IPA', 'AD'] |
| 42 |
extends_documentation_fragment: |
| 43 |
- community.general._attributes |
| 44 |
- community.general._attributes.info_module |
| 45 |
""" |
| 46 |
|
| 47 |
EXAMPLES = r""" |
| 48 |
- name: Check SSSD domain status |
| 49 |
community.general.sssd_info: |
| 50 |
action: domain_status |
| 51 |
domain: example.com |
| 52 |
register: sssd_status_result |
| 53 |
|
| 54 |
- name: Get domain list |
| 55 |
community.general.sssd_info: |
| 56 |
action: domain_list |
| 57 |
register: domain_list_result |
| 58 |
|
| 59 |
- name: Get active IPA servers for a domain |
| 60 |
community.general.sssd_info: |
| 61 |
action: active_servers |
| 62 |
domain: example.com |
| 63 |
server_type: IPA |
| 64 |
register: active_servers_result |
| 65 |
|
| 66 |
- name: List servers for a domain |
| 67 |
community.general.sssd_info: |
| 68 |
action: list_servers |
| 69 |
domain: example.com |
| 70 |
server_type: AD |
| 71 |
register: list_servers_result |
| 72 |
""" |
| 73 |
|
| 74 |
RETURN = r""" |
| 75 |
online: |
| 76 |
description: The online status of the SSSD domain. |
| 77 |
type: str |
| 78 |
returned: when O(action=domain_status) |
| 79 |
sample: online |
| 80 |
domain_list: |
| 81 |
description: List of SSSD domains. |
| 82 |
type: list |
| 83 |
elements: str |
| 84 |
returned: when O(action=domain_list) |
| 85 |
sample: ["ipa.domain", "winad.test"] |
| 86 |
servers: |
| 87 |
description: Active servers for the specified domain and type. |
| 88 |
type: dict |
| 89 |
returned: when O(action=active_servers) |
| 90 |
sample: {"Global Catalog": "server1.winad.test", "Domain Server": "server2.winad.test"} |
| 91 |
list_servers: |
| 92 |
description: List of servers for the specified domain. |
| 93 |
type: list |
| 94 |
elements: str |
| 95 |
returned: when O(action=list_servers) |
| 96 |
sample: ["server1.winad.test", "server2.winad.test"] |
| 97 |
""" |
| 98 |
|
| 99 |
|
| 100 |
import typing as t |
| 101 |
|
| 102 |
from ansible.module_utils.basic import AnsibleModule |
| 103 |
|
| 104 |
from ansible_collections.community.general.plugins.module_utils import _deps as deps |
| 105 |
|
| 106 |
with deps.declare("dbus"): |
| 107 |
import dbus |
| 108 |
|
| 109 |
|
| 110 |
class SSSDHandler: |
| 111 |
"""SSSD D-Bus handler""" |
| 112 |
|
| 113 |
BUS_NAME = "org.freedesktop.sssd.infopipe" |
| 114 |
DOMAIN_INTERFACE = "org.freedesktop.sssd.infopipe.Domains.Domain" |
| 115 |
INFOPIPE_INTERFACE = "org.freedesktop.sssd.infopipe" |
| 116 |
|
| 117 |
def __init__(self) -> None: |
| 118 |
"""Initialize SSSD D-Bus connection.""" |
| 119 |
self.bus = dbus.SystemBus() |
| 120 |
self.sssd_obj = self.bus.get_object(self.BUS_NAME, "/org/freedesktop/sssd/infopipe") |
| 121 |
self.infopipe_iface = dbus.Interface(self.sssd_obj, dbus_interface=self.INFOPIPE_INTERFACE) |
| 122 |
|
| 123 |
def domain_path(self, domain: str) -> str: |
| 124 |
return f"/org/freedesktop/sssd/infopipe/Domains/{domain.replace('.', '_2e')}" |
| 125 |
|
| 126 |
def domain_object(self, domain: str) -> dbus.proxies.ProxyObject: |
| 127 |
domain_path = self.domain_path(domain) |
| 128 |
try: |
| 129 |
return self.bus.get_object(self.BUS_NAME, domain_path) |
| 130 |
except dbus.exceptions.DBusException as e: |
| 131 |
raise Exception(f"Domain not found: {domain}. Error: {e}") from e |
| 132 |
|
| 133 |
def check_domain_status(self, domain: str) -> str: |
| 134 |
domain_obj = self.domain_object(domain) |
| 135 |
iface = dbus.Interface(domain_obj, dbus_interface=self.DOMAIN_INTERFACE) |
| 136 |
return "online" if iface.IsOnline() else "offline" |
| 137 |
|
| 138 |
def active_servers(self, domain: str, server_type: str) -> dict[str, str]: |
| 139 |
"""Get active servers for domain. |
| 140 |
|
| 141 |
Args: |
| 142 |
domain: Domain name to get servers for. |
| 143 |
server_type: Type of servers ('IPA' or 'AD'). |
| 144 |
|
| 145 |
Returns: |
| 146 |
Dictionary with server types as keys and server names as values. |
| 147 |
""" |
| 148 |
domain_obj = self.domain_object(domain) |
| 149 |
iface = dbus.Interface(domain_obj, dbus_interface=self.DOMAIN_INTERFACE) |
| 150 |
|
| 151 |
if server_type == "IPA": |
| 152 |
server_name = f"{server_type} Server" |
| 153 |
return {server_name: iface.ActiveServer(server_type)} |
| 154 |
else: |
| 155 |
return { |
| 156 |
"AD Global Catalog": iface.ActiveServer(f"sd_gc_{domain}"), |
| 157 |
"AD Domain Controller": iface.ActiveServer(f"sd_{domain}"), |
| 158 |
} |
| 159 |
|
| 160 |
def list_servers(self, domain: str, server_type: str) -> list[str]: |
| 161 |
"""List all servers for domain. |
| 162 |
|
| 163 |
Args: |
| 164 |
domain: Domain name to list servers for. |
| 165 |
server_type: Type of servers ('IPA' or 'AD'). |
| 166 |
|
| 167 |
Returns: |
| 168 |
List of server names. |
| 169 |
""" |
| 170 |
domain_obj = self.domain_object(domain) |
| 171 |
iface = dbus.Interface(domain_obj, dbus_interface=self.DOMAIN_INTERFACE) |
| 172 |
if server_type == "IPA": |
| 173 |
return iface.ListServers(server_type) |
| 174 |
else: |
| 175 |
return iface.ListServers(f"sd_{domain}") |
| 176 |
|
| 177 |
def domain_list(self) -> list[str]: |
| 178 |
"""Get list of all domains. |
| 179 |
|
| 180 |
Returns: |
| 181 |
List of domain names. |
| 182 |
""" |
| 183 |
response = self.infopipe_iface.ListDomains() |
| 184 |
return [domain.rsplit("/", maxsplit=1)[-1].replace("_2e", ".") for domain in response] |
| 185 |
|
| 186 |
|
| 187 |
def main() -> None: |
| 188 |
"""Main function for the Ansible module.""" |
| 189 |
module = AnsibleModule( |
| 190 |
argument_spec=dict( |
| 191 |
action=dict( |
| 192 |
type="str", |
| 193 |
required=True, |
| 194 |
choices=["domain_status", "domain_list", "active_servers", "list_servers"], |
| 195 |
), |
| 196 |
domain=dict(type="str"), |
| 197 |
server_type=dict(type="str", choices=["IPA", "AD"]), |
| 198 |
), |
| 199 |
supports_check_mode=True, |
| 200 |
required_if=[ |
| 201 |
("action", "domain_status", ["domain"]), |
| 202 |
("action", "list_servers", ["domain", "server_type"]), |
| 203 |
("action", "active_servers", ["domain", "server_type"]), |
| 204 |
], |
| 205 |
) |
| 206 |
|
| 207 |
deps.validate(module) |
| 208 |
|
| 209 |
action = module.params["action"] |
| 210 |
domain = module.params.get("domain") |
| 211 |
server_type = module.params.get("server_type") |
| 212 |
|
| 213 |
sssd = SSSDHandler() |
| 214 |
result: dict[str, t.Any] = {} |
| 215 |
|
| 216 |
try: |
| 217 |
if action == "domain_status": |
| 218 |
result["online"] = sssd.check_domain_status(domain) |
| 219 |
elif action == "domain_list": |
| 220 |
result["domain_list"] = sssd.domain_list() |
| 221 |
elif action == "active_servers": |
| 222 |
result["servers"] = sssd.active_servers(domain, server_type) |
| 223 |
elif action == "list_servers": |
| 224 |
result["list_servers"] = sssd.list_servers(domain, server_type) |
| 225 |
|
| 226 |
except Exception as e: |
| 227 |
module.fail_json(msg=f"Error: {e}") |
| 228 |
|
| 229 |
module.exit_json(**result) |
| 230 |
|
| 231 |
|
| 232 |
if __name__ == "__main__": |
| 233 |
main() |