| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2026, Clément Cruau (@PendaGTP) <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
module: proxmox_acme_certificate |
| 10 |
short_description: Manages ACME SSL certificates for Proxmox VE nodes |
| 11 |
version_added: 2.0.0 |
| 12 |
author: Clément Cruau (@PendaGTP) |
| 13 |
description: |
| 14 |
- Order, renew or remove ACME certificates from a Certificate Authority |
| 15 |
for a specific Proxmox VE node. |
| 16 |
- Before using this module, ensure that an ACME account is configured |
| 17 |
(using M(community.proxmox.proxmox_acme_account)) and DNS plugins are |
| 18 |
configured if using DNS-01 challenge |
| 19 |
(using M(community.proxmox.proxmox_acme_plugin_dns)). |
| 20 |
- Requires C(root@pam) authentication. |
| 21 |
attributes: |
| 22 |
check_mode: |
| 23 |
support: full |
| 24 |
diff_mode: |
| 25 |
support: none |
| 26 |
options: |
| 27 |
state: |
| 28 |
description: |
| 29 |
- Desired state of the ACME certificate on the node. |
| 30 |
type: str |
| 31 |
choices: |
| 32 |
- present |
| 33 |
- absent |
| 34 |
default: present |
| 35 |
node_name: |
| 36 |
description: |
| 37 |
- The name of the Proxmox VE node for which to order/manage the ACME certificate. |
| 38 |
type: str |
| 39 |
required: true |
| 40 |
account: |
| 41 |
description: |
| 42 |
- The ACME account name to use for ordering the certificate. |
| 43 |
type: str |
| 44 |
domains: |
| 45 |
description: |
| 46 |
- The list of domains to include in the certificate. |
| 47 |
- At least one domain is required when O(state=present). |
| 48 |
type: list |
| 49 |
elements: dict |
| 50 |
suboptions: |
| 51 |
domain: |
| 52 |
description: |
| 53 |
- The domain name to include in the certificate. |
| 54 |
type: str |
| 55 |
required: true |
| 56 |
plugin: |
| 57 |
description: |
| 58 |
- The DNS plugin to use for DNS-01 challenge validation. |
| 59 |
- If not specified, the standalone HTTP-01 challenge will be used. |
| 60 |
type: str |
| 61 |
alias: |
| 62 |
description: |
| 63 |
- An optional alias domain for DNS validation. |
| 64 |
type: str |
| 65 |
force: |
| 66 |
description: |
| 67 |
- Force certificate renewal even if the certificate is not due for renewal yet. |
| 68 |
- Setting this to V(true) will trigger a new certificate order. |
| 69 |
type: bool |
| 70 |
default: false |
| 71 |
|
| 72 |
seealso: |
| 73 |
- name: Certificate management (Proxmox documentation) |
| 74 |
description: ACME accounts and certificates in Proxmox VE |
| 75 |
link: https://pve.proxmox.com/pve-docs/pve-admin-guide.html#sysadmin_certificate_management |
| 76 |
- module: community.proxmox.proxmox_acme_certificates_info |
| 77 |
- module: community.proxmox.proxmox_acme_account |
| 78 |
- module: community.proxmox.proxmox_acme_plugin_dns |
| 79 |
|
| 80 |
extends_documentation_fragment: |
| 81 |
- community.proxmox.proxmox.actiongroup_proxmox |
| 82 |
- community.proxmox.proxmox.documentation |
| 83 |
- community.proxmox.attributes |
| 84 |
""" |
| 85 |
|
| 86 |
EXAMPLES = r""" |
| 87 |
- name: Order ACME certificate with HTTP-01 challenge (standalone) |
| 88 |
community.proxmox.proxmox_acme_certificate: |
| 89 |
node_name: pve-node-01 |
| 90 |
account: production |
| 91 |
domains: |
| 92 |
- domain: pve.example.com |
| 93 |
|
| 94 |
- name: Order ACME certificate with DNS-01 challenge using Cloudflare |
| 95 |
community.proxmox.proxmox_acme_certificate: |
| 96 |
node_name: pve-node-01 |
| 97 |
account: production |
| 98 |
domains: |
| 99 |
- domain: pve.example.com |
| 100 |
plugin: cloudflare |
| 101 |
|
| 102 |
- name: Multiple domains with mixed challenge types |
| 103 |
community.proxmox.proxmox_acme_certificate: |
| 104 |
node_name: pve-node-01 |
| 105 |
account: production |
| 106 |
domains: |
| 107 |
- domain: pve.example.com |
| 108 |
plugin: cloudflare |
| 109 |
- domain: pve2.example.com |
| 110 |
|
| 111 |
- name: Force certificate renewal |
| 112 |
community.proxmox.proxmox_acme_certificate: |
| 113 |
node_name: pve-node-01 |
| 114 |
account: production |
| 115 |
force: true |
| 116 |
domains: |
| 117 |
- domain: pve.example.com |
| 118 |
plugin: cloudflare |
| 119 |
|
| 120 |
- name: Remove ACME certificate and configuration |
| 121 |
community.proxmox.proxmox_acme_certificate: |
| 122 |
node_name: pve-node-01 |
| 123 |
state: absent |
| 124 |
""" |
| 125 |
|
| 126 |
RETURN = r""" |
| 127 |
node_name: |
| 128 |
description: The Proxmox VE node name. |
| 129 |
returned: on success |
| 130 |
type: str |
| 131 |
account: |
| 132 |
description: The ACME account name used for the certificate. |
| 133 |
returned: when O(state=present) |
| 134 |
type: str |
| 135 |
domains: |
| 136 |
description: The list of domains included in the certificate. |
| 137 |
returned: when O(state=present) |
| 138 |
type: list |
| 139 |
elements: dict |
| 140 |
contains: |
| 141 |
domain: |
| 142 |
description: The domain name. |
| 143 |
type: str |
| 144 |
plugin: |
| 145 |
description: The DNS plugin used for validation. |
| 146 |
type: str |
| 147 |
alias: |
| 148 |
description: The alias domain used for DNS validation. |
| 149 |
type: str |
| 150 |
certificate: |
| 151 |
description: The PEM-encoded certificate data. |
| 152 |
returned: when O(state=present) and a certificate exists |
| 153 |
type: str |
| 154 |
fingerprint: |
| 155 |
description: The certificate fingerprint. |
| 156 |
returned: when O(state=present) and a certificate exists |
| 157 |
type: str |
| 158 |
issuer: |
| 159 |
description: The certificate issuer. |
| 160 |
returned: when O(state=present) and a certificate exists |
| 161 |
type: str |
| 162 |
subject: |
| 163 |
description: The certificate subject. |
| 164 |
returned: when O(state=present) and a certificate exists |
| 165 |
type: str |
| 166 |
not_after: |
| 167 |
description: The certificate expiration timestamp. |
| 168 |
returned: when O(state=present) and a certificate exists |
| 169 |
type: str |
| 170 |
not_before: |
| 171 |
description: The certificate start timestamp. |
| 172 |
returned: when O(state=present) and a certificate exists |
| 173 |
type: str |
| 174 |
subject_alternative_names: |
| 175 |
description: The certificate subject alternative names (SANs). |
| 176 |
returned: when O(state=present) and a certificate exists |
| 177 |
type: list |
| 178 |
elements: str |
| 179 |
msg: |
| 180 |
description: Short description of the action taken. |
| 181 |
returned: always |
| 182 |
type: str |
| 183 |
""" |
| 184 |
|
| 185 |
from ansible.module_utils.common.text.converters import to_native |
| 186 |
|
| 187 |
from ansible_collections.community.proxmox.plugins.module_utils.proxmox import ( |
| 188 |
ProxmoxAnsible, |
| 189 |
ansible_to_proxmox_bool, |
| 190 |
create_proxmox_module, |
| 191 |
) |
| 192 |
from ansible_collections.community.proxmox.plugins.module_utils.proxmox_acme_certificate import ( |
| 193 |
cert_info_to_ansible_result, |
| 194 |
parse_acme_config, |
| 195 |
) |
| 196 |
|
| 197 |
CERTIFICATE_TASK_TIMEOUT = 120 |
| 198 |
ACME_DOMAIN_SLOTS = 6 # acmedomain0 .. acmedomain5 |
| 199 |
|
| 200 |
|
| 201 |
def build_acmedomain_property_string(domain, plugin=None, alias=None): |
| 202 |
"""Build an ``acmedomainN`` node config property string. |
| 203 |
|
| 204 |
Example output: ``"domain=pve.example.com,plugin=cloudflare,alias=alt.example.com"`` |
| 205 |
""" |
| 206 |
parts = [f"domain={domain}"] |
| 207 |
if plugin: |
| 208 |
parts.append(f"plugin={plugin}") |
| 209 |
if alias: |
| 210 |
parts.append(f"alias={alias}") |
| 211 |
return ",".join(parts) |
| 212 |
|
| 213 |
|
| 214 |
def find_acme_certificate(certificates): |
| 215 |
"""Find the ACME/custom certificate from the node's certificate list. |
| 216 |
|
| 217 |
The Proxmox API returns a ``filename`` field for each certificate. |
| 218 |
ACME and custom certificates are stored as ``pveproxy-ssl.pem``, |
| 219 |
while the self-signed cert is ``pve-ssl.pem``. |
| 220 |
|
| 221 |
Returns the matching certificate dict, or None. |
| 222 |
""" |
| 223 |
if not certificates: |
| 224 |
return None |
| 225 |
|
| 226 |
for cert in certificates: |
| 227 |
filename = cert.get("filename", "") |
| 228 |
if filename == "pveproxy-ssl.pem": |
| 229 |
return cert |
| 230 |
|
| 231 |
return None |
| 232 |
|
| 233 |
|
| 234 |
def normalize_domain_list(domains): |
| 235 |
"""Normalize a list of domain dicts for comparison. |
| 236 |
|
| 237 |
Sorts by domain name and normalises None/empty plugin/alias to None. |
| 238 |
""" |
| 239 |
normalized = [] |
| 240 |
for d in domains: |
| 241 |
normalized.append( |
| 242 |
{ |
| 243 |
"domain": d["domain"].strip().lower(), |
| 244 |
"plugin": d["plugin"].strip() if d.get("plugin") else None, |
| 245 |
"alias": d["alias"].strip() if d.get("alias") else None, |
| 246 |
} |
| 247 |
) |
| 248 |
return sorted(normalized, key=lambda x: x["domain"]) |
| 249 |
|
| 250 |
|
| 251 |
def module_args(): |
| 252 |
return dict( |
| 253 |
state=dict(choices=["present", "absent"], default="present"), |
| 254 |
node_name=dict(type="str", required=True), |
| 255 |
account=dict(type="str"), |
| 256 |
domains=dict( |
| 257 |
type="list", |
| 258 |
elements="dict", |
| 259 |
options=dict( |
| 260 |
domain=dict(type="str", required=True), |
| 261 |
plugin=dict(type="str"), |
| 262 |
alias=dict(type="str"), |
| 263 |
), |
| 264 |
), |
| 265 |
force=dict(type="bool", default=False), |
| 266 |
) |
| 267 |
|
| 268 |
|
| 269 |
def module_options(): |
| 270 |
return dict( |
| 271 |
required_if=[("state", "present", ["account", "domains"])], |
| 272 |
) |
| 273 |
|
| 274 |
|
| 275 |
class ProxmoxAcmeCertificateAnsible(ProxmoxAnsible): |
| 276 |
def __init__(self, module): |
| 277 |
super().__init__(module) |
| 278 |
self.params = module.params |
| 279 |
|
| 280 |
def run(self): |
| 281 |
state = self.params["state"] |
| 282 |
node_name = self.params["node_name"] |
| 283 |
|
| 284 |
if state == "present": |
| 285 |
self._ensure_present(node_name) |
| 286 |
else: |
| 287 |
self._ensure_absent(node_name) |
| 288 |
|
| 289 |
def validate_params(self): |
| 290 |
domains = self.params.get("domains") |
| 291 |
if domains and len(domains) > ACME_DOMAIN_SLOTS: |
| 292 |
self.module.fail_json( |
| 293 |
msg=f"Proxmox supports a maximum of {ACME_DOMAIN_SLOTS} ACME domains, got {len(domains)}", |
| 294 |
) |
| 295 |
|
| 296 |
def _ensure_present(self, node_name): |
| 297 |
current_config = self._read_node_acme_config(node_name) |
| 298 |
desired_config = self._build_desired_config() |
| 299 |
force = self.params["force"] |
| 300 |
|
| 301 |
cert = self._read_certificate_info(node_name) |
| 302 |
|
| 303 |
config_changed = self._is_config_changed(current_config, desired_config) |
| 304 |
needs_order = config_changed or force or current_config["account"] is None or cert is None |
| 305 |
|
| 306 |
if not needs_order: |
| 307 |
result = cert_info_to_ansible_result(cert) |
| 308 |
self.module.exit_json( |
| 309 |
changed=False, |
| 310 |
node_name=node_name, |
| 311 |
account=self.params["account"], |
| 312 |
domains=self.params["domains"], |
| 313 |
msg=f"ACME certificate for node {node_name} is already up to date", |
| 314 |
**result, |
| 315 |
) |
| 316 |
|
| 317 |
if self.module.check_mode: |
| 318 |
self.module.exit_json( |
| 319 |
changed=True, |
| 320 |
node_name=node_name, |
| 321 |
account=self.params["account"], |
| 322 |
domains=self.params["domains"], |
| 323 |
msg=f"ACME certificate for node {node_name} would be ordered", |
| 324 |
) |
| 325 |
|
| 326 |
self._configure_node_acme(node_name, desired_config) |
| 327 |
self._order_certificate(node_name) |
| 328 |
|
| 329 |
cert = self._read_certificate_info(node_name) |
| 330 |
result = cert_info_to_ansible_result(cert) if cert else {} |
| 331 |
|
| 332 |
self.module.exit_json( |
| 333 |
changed=True, |
| 334 |
node_name=node_name, |
| 335 |
account=self.params["account"], |
| 336 |
domains=self.params["domains"], |
| 337 |
msg=f"ACME certificate for node {node_name} successfully ordered", |
| 338 |
**result, |
| 339 |
) |
| 340 |
|
| 341 |
def _ensure_absent(self, node_name): |
| 342 |
current_config = self._read_node_acme_config(node_name) |
| 343 |
|
| 344 |
if current_config["account"] is None and not current_config["domains"]: |
| 345 |
self.module.exit_json(changed=False, node_name=node_name, msg=f"No ACME configuration on node {node_name}") |
| 346 |
|
| 347 |
if self.module.check_mode: |
| 348 |
self.module.exit_json( |
| 349 |
changed=True, |
| 350 |
node_name=node_name, |
| 351 |
msg=f"ACME certificate and configuration for node {node_name} would be removed", |
| 352 |
) |
| 353 |
|
| 354 |
self._delete_certificate(node_name) |
| 355 |
self._cleanup_node_acme_config(node_name) |
| 356 |
|
| 357 |
self.module.exit_json( |
| 358 |
changed=True, |
| 359 |
node_name=node_name, |
| 360 |
msg=f"ACME certificate and configuration for node {node_name} successfully removed", |
| 361 |
) |
| 362 |
|
| 363 |
def _read_node_acme_config(self, node_name): |
| 364 |
try: |
| 365 |
config = self.proxmox_api.nodes(node_name).config.get() |
| 366 |
except Exception as e: |
| 367 |
self.module.fail_json(msg=f"Failed to read node config for {node_name}: {to_native(e)}") |
| 368 |
return parse_acme_config(config) |
| 369 |
|
| 370 |
def _configure_node_acme(self, node_name, desired_config): |
| 371 |
account = desired_config["account"] |
| 372 |
domains = desired_config["domains"] |
| 373 |
|
| 374 |
params = {"acme": f"account={account}"} |
| 375 |
|
| 376 |
for i, domain in enumerate(domains): |
| 377 |
key = f"acmedomain{i}" |
| 378 |
params[key] = build_acmedomain_property_string( |
| 379 |
domain["domain"], |
| 380 |
plugin=domain.get("plugin"), |
| 381 |
alias=domain.get("alias"), |
| 382 |
) |
| 383 |
|
| 384 |
unused_slots = [f"acmedomain{i}" for i in range(len(domains), ACME_DOMAIN_SLOTS)] |
| 385 |
if unused_slots: |
| 386 |
params["delete"] = ",".join(unused_slots) |
| 387 |
|
| 388 |
try: |
| 389 |
self.proxmox_api.nodes(node_name).config.put(**params) |
| 390 |
except Exception as e: |
| 391 |
self.module.fail_json(msg=f"Failed to configure ACME settings on node {node_name}: {to_native(e)}") |
| 392 |
|
| 393 |
def _cleanup_node_acme_config(self, node_name): |
| 394 |
slots = [f"acmedomain{i}" for i in range(ACME_DOMAIN_SLOTS)] |
| 395 |
delete_keys = ",".join(["acme"] + slots) |
| 396 |
|
| 397 |
try: |
| 398 |
self.proxmox_api.nodes(node_name).config.put(delete=delete_keys) |
| 399 |
except Exception as e: |
| 400 |
self.module.warn(f"Failed to clean up ACME configuration on node {node_name}: {to_native(e)}.") |
| 401 |
|
| 402 |
def _order_certificate(self, node_name): |
| 403 |
force = self.params["force"] |
| 404 |
|
| 405 |
try: |
| 406 |
taskid = self.proxmox_api.nodes(node_name).certificates.acme.certificate.post( |
| 407 |
force=ansible_to_proxmox_bool(force), |
| 408 |
) |
| 409 |
except Exception as e: |
| 410 |
err = to_native(e) |
| 411 |
if "'force' is not set" in err or "Custom certificate exists" in err: |
| 412 |
self.module.fail_json( |
| 413 |
msg=( |
| 414 |
f"A custom certificate already exists on node {node_name}. " |
| 415 |
"Set force=true to replace it with an ACME certificate." |
| 416 |
) |
| 417 |
) |
| 418 |
self.module.fail_json(msg=f"Failed to order ACME certificate for node {node_name}: {err}") |
| 419 |
|
| 420 |
if taskid: |
| 421 |
self._wait_certificate_task(node_name, taskid) |
| 422 |
|
| 423 |
def _delete_certificate(self, node_name): |
| 424 |
try: |
| 425 |
taskid = self.proxmox_api.nodes(node_name).certificates.acme.certificate.delete() |
| 426 |
except Exception as e: |
| 427 |
self.module.warn(f"Failed to delete ACME certificate on node {node_name}: {to_native(e)}") |
| 428 |
return |
| 429 |
|
| 430 |
if taskid and "acmerevoke" in to_native(taskid): |
| 431 |
self._wait_revocation_task(node_name, taskid) |
| 432 |
|
| 433 |
def _read_certificate_info(self, node_name): |
| 434 |
try: |
| 435 |
certs = self.proxmox_api.nodes(node_name).certificates.info.get() |
| 436 |
except Exception as e: |
| 437 |
self.module.fail_json(msg=f"Failed to read certificates info for node {node_name}: {to_native(e)}") |
| 438 |
return find_acme_certificate(certs) |
| 439 |
|
| 440 |
def _wait_certificate_task(self, node_name, taskid): |
| 441 |
node = self.upid_to_node(taskid) |
| 442 |
ok, err = self.api_task_complete(node, taskid, CERTIFICATE_TASK_TIMEOUT) |
| 443 |
|
| 444 |
if not ok: |
| 445 |
self.module.fail_json( |
| 446 |
msg=f"ACME certificate task failed: {err}", |
| 447 |
task=taskid, |
| 448 |
) |
| 449 |
|
| 450 |
def _wait_revocation_task(self, node_name, taskid): |
| 451 |
node = self.upid_to_node(taskid) |
| 452 |
ok, err = self.api_task_complete(node, taskid, CERTIFICATE_TASK_TIMEOUT) |
| 453 |
|
| 454 |
if not ok: |
| 455 |
self.module.fail_json( |
| 456 |
msg=f"ACME certificate revocation failed on node {node_name}: {err}", |
| 457 |
task=taskid, |
| 458 |
) |
| 459 |
|
| 460 |
def _build_desired_config(self): |
| 461 |
return { |
| 462 |
"account": self.params["account"], |
| 463 |
"domains": self.params["domains"] or [], |
| 464 |
} |
| 465 |
|
| 466 |
def _is_config_changed(self, current, desired): |
| 467 |
if (current["account"] or "") != (desired["account"] or ""): |
| 468 |
return True |
| 469 |
|
| 470 |
current_domains = normalize_domain_list(current["domains"]) |
| 471 |
desired_domains = normalize_domain_list(desired["domains"]) |
| 472 |
|
| 473 |
return current_domains != desired_domains |
| 474 |
|
| 475 |
|
| 476 |
def main(): |
| 477 |
module = create_proxmox_module(module_args(), **module_options()) |
| 478 |
proxmox = ProxmoxAcmeCertificateAnsible(module) |
| 479 |
proxmox.validate_params() |
| 480 |
|
| 481 |
try: |
| 482 |
proxmox.run() |
| 483 |
except Exception as e: |
| 484 |
module.fail_json(msg=f"An error occurred: {to_native(e)}") |
| 485 |
|
| 486 |
|
| 487 |
if __name__ == "__main__": |
| 488 |
main() |