| 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_account |
| 10 |
short_description: Manages an ACME account |
| 11 |
version_added: "2.0.0" |
| 12 |
author: Clément Cruau (@PendaGTP) |
| 13 |
description: |
| 14 |
- Create, update or delete an ACME account on the Proxmox VE. |
| 15 |
- When an account already exists, only the contact email can be updated (Proxmox API limitation). |
| 16 |
- Requires C(root@pam) authentication. |
| 17 |
attributes: |
| 18 |
check_mode: |
| 19 |
support: full |
| 20 |
diff_mode: |
| 21 |
support: none |
| 22 |
options: |
| 23 |
state: |
| 24 |
description: |
| 25 |
- Desired state of the ACME account. |
| 26 |
type: str |
| 27 |
choices: |
| 28 |
- present |
| 29 |
- absent |
| 30 |
default: present |
| 31 |
name: |
| 32 |
description: |
| 33 |
- The ACME account name (filename). |
| 34 |
type: str |
| 35 |
default: default |
| 36 |
contact: |
| 37 |
description: |
| 38 |
- Contact email address for the ACME account. |
| 39 |
- Required when creating a new account (Proxmox API). |
| 40 |
type: str |
| 41 |
directory: |
| 42 |
description: |
| 43 |
- URL of the ACME CA directory endpoint. |
| 44 |
type: str |
| 45 |
eab_hmac_key: |
| 46 |
description: |
| 47 |
- HMAC key for External Account Binding (EAB). |
| 48 |
type: str |
| 49 |
eab_kid: |
| 50 |
description: |
| 51 |
- Key identifier for External Account Binding (EAB). |
| 52 |
type: str |
| 53 |
tos: |
| 54 |
description: |
| 55 |
- URL of the CA terms of service. |
| 56 |
type: str |
| 57 |
aliases: |
| 58 |
- tos_url |
| 59 |
|
| 60 |
seealso: |
| 61 |
- name: Certificate management (Proxmox documentation) |
| 62 |
description: ACME accounts and certificates in Proxmox VE |
| 63 |
link: https://pve.proxmox.com/pve-docs/pve-admin-guide.html#sysadmin_certificate_management |
| 64 |
- module: community.proxmox.proxmox_acme_accounts_info |
| 65 |
description: List ACME account names. |
| 66 |
- module: community.proxmox.proxmox_acme_account_info |
| 67 |
description: Retrieve information about a single ACME account. |
| 68 |
|
| 69 |
extends_documentation_fragment: |
| 70 |
- community.proxmox.proxmox.actiongroup_proxmox |
| 71 |
- community.proxmox.proxmox.documentation |
| 72 |
- community.proxmox.attributes |
| 73 |
""" |
| 74 |
|
| 75 |
EXAMPLES = r""" |
| 76 |
- name: Create ACME account |
| 77 |
community.proxmox.proxmox_acme_account: |
| 78 |
name: example |
| 79 |
contact: [email protected] |
| 80 |
directory: https://acme-staging-v02.api.letsencrypt.org/directory |
| 81 |
tos: https://letsencrypt.org/documents/LE-SA-v1.3-September-21-2022.pdf |
| 82 |
|
| 83 |
- name: Update ACME account contact |
| 84 |
community.proxmox.proxmox_acme_account: |
| 85 |
name: example |
| 86 |
contact: [email protected] |
| 87 |
|
| 88 |
- name: Ensure ACME account exists |
| 89 |
community.proxmox.proxmox_acme_account: |
| 90 |
name: example |
| 91 |
|
| 92 |
- name: Remove ACME account |
| 93 |
community.proxmox.proxmox_acme_account: |
| 94 |
name: example |
| 95 |
state: absent |
| 96 |
""" |
| 97 |
|
| 98 |
RETURN = r""" |
| 99 |
name: |
| 100 |
description: The ACME account configuration name. |
| 101 |
returned: on success |
| 102 |
type: str |
| 103 |
account: |
| 104 |
description: ACME account data returned by the API. |
| 105 |
returned: when O(state=present) and the account exists |
| 106 |
type: dict |
| 107 |
contains: |
| 108 |
contact: |
| 109 |
description: Contact email addresses. |
| 110 |
type: list |
| 111 |
elements: str |
| 112 |
created_at: |
| 113 |
description: Account creation timestamp from the ACME API. |
| 114 |
type: str |
| 115 |
status: |
| 116 |
description: Account status (for example V(valid), V(deactivated), V(revoked)). |
| 117 |
type: str |
| 118 |
directory: |
| 119 |
description: Directory URL of the ACME account. |
| 120 |
returned: when O(state=present) and the account exists |
| 121 |
type: str |
| 122 |
location: |
| 123 |
description: Account resource URL from the ACME CA. |
| 124 |
returned: when O(state=present) and the account exists |
| 125 |
type: str |
| 126 |
tos: |
| 127 |
description: Terms of service URL for the account. |
| 128 |
returned: when O(state=present) and the account exists |
| 129 |
type: str |
| 130 |
msg: |
| 131 |
description: Short description of the action taken. |
| 132 |
returned: always |
| 133 |
type: str |
| 134 |
""" |
| 135 |
|
| 136 |
import re |
| 137 |
|
| 138 |
from ansible.module_utils.common.text.converters import to_native |
| 139 |
|
| 140 |
from ansible_collections.community.proxmox.plugins.module_utils.proxmox import ( |
| 141 |
ProxmoxAnsible, |
| 142 |
create_proxmox_module, |
| 143 |
is_not_found_error, |
| 144 |
) |
| 145 |
from ansible_collections.community.proxmox.plugins.module_utils.proxmox_acme_account import ( |
| 146 |
acme_account_to_ansible_result, |
| 147 |
normalize_contact_list, |
| 148 |
) |
| 149 |
|
| 150 |
DIRECTORY_URL_PATTERN = re.compile(r"^https?://.*$") |
| 151 |
ACME_TASK_TIMEOUT = 30 |
| 152 |
|
| 153 |
|
| 154 |
def module_args(): |
| 155 |
return dict( |
| 156 |
state=dict(choices=["present", "absent"], default="present"), |
| 157 |
name=dict(type="str", default="default"), |
| 158 |
contact=dict(type="str"), |
| 159 |
directory=dict(type="str"), |
| 160 |
eab_hmac_key=dict(type="str", no_log=True), |
| 161 |
eab_kid=dict(type="str", no_log=True), |
| 162 |
tos=dict(type="str", aliases=["tos_url"]), |
| 163 |
) |
| 164 |
|
| 165 |
|
| 166 |
def module_options(): |
| 167 |
return {} |
| 168 |
|
| 169 |
|
| 170 |
class ProxmoxClusterAcmeAccountAnsible(ProxmoxAnsible): |
| 171 |
def __init__(self, module): |
| 172 |
super().__init__(module) |
| 173 |
self.params = module.params |
| 174 |
|
| 175 |
def run(self): |
| 176 |
state = self.params["state"] |
| 177 |
name = self.params["name"] |
| 178 |
|
| 179 |
if state == "present": |
| 180 |
self._ensure_present(name) |
| 181 |
else: |
| 182 |
self._ensure_absent(name) |
| 183 |
|
| 184 |
def validate_params(self): |
| 185 |
directory = self.params.get("directory") |
| 186 |
if directory and not DIRECTORY_URL_PATTERN.match(directory): |
| 187 |
self.module.fail_json( |
| 188 |
msg="directory must be a valid URL (http:// or https://)", |
| 189 |
directory=directory, |
| 190 |
) |
| 191 |
|
| 192 |
def _ensure_present(self, name): |
| 193 |
existing = self._fetch_account(name) |
| 194 |
|
| 195 |
if existing is None: |
| 196 |
if self.module.check_mode: |
| 197 |
self.module.exit_json( |
| 198 |
changed=True, |
| 199 |
name=name, |
| 200 |
msg=f"ACME account {name} would be created", |
| 201 |
) |
| 202 |
|
| 203 |
result = self._create_account(name) |
| 204 |
self.module.exit_json(**result) |
| 205 |
|
| 206 |
result = self._reconcile_existing(name, existing) |
| 207 |
self.module.exit_json(**result) |
| 208 |
|
| 209 |
def _ensure_absent(self, name): |
| 210 |
existing = self._fetch_account(name) |
| 211 |
|
| 212 |
if existing is None: |
| 213 |
self.module.exit_json( |
| 214 |
changed=False, |
| 215 |
name=name, |
| 216 |
msg=f"ACME account {name} does not exist", |
| 217 |
) |
| 218 |
|
| 219 |
if self.module.check_mode: |
| 220 |
self.module.exit_json( |
| 221 |
changed=True, |
| 222 |
name=name, |
| 223 |
msg=f"ACME account {name} would be deleted", |
| 224 |
) |
| 225 |
|
| 226 |
self._delete_account(name) |
| 227 |
|
| 228 |
self.module.exit_json( |
| 229 |
changed=True, |
| 230 |
name=name, |
| 231 |
msg=f"ACME account {name} successfully deleted", |
| 232 |
) |
| 233 |
|
| 234 |
def _reconcile_existing(self, name, existing): |
| 235 |
# No contact provided, nothing to do |
| 236 |
if not self._has_contact(): |
| 237 |
result = acme_account_to_ansible_result(existing) |
| 238 |
return { |
| 239 |
"changed": False, |
| 240 |
"name": name, |
| 241 |
"msg": f"ACME account {name} exists; contact not specified, no update attempted", |
| 242 |
**result, |
| 243 |
} |
| 244 |
|
| 245 |
# API does not return contact, cannot detect drift |
| 246 |
if not self._has_api_contact(existing): |
| 247 |
self.module.warn( |
| 248 |
"Proxmox API did not return contact addresses; cannot verify drift; applying contact update.", |
| 249 |
) |
| 250 |
|
| 251 |
if self.module.check_mode: |
| 252 |
result = acme_account_to_ansible_result(existing) |
| 253 |
return { |
| 254 |
"changed": True, |
| 255 |
"name": name, |
| 256 |
"msg": f"ACME account {name} contact would be updated", |
| 257 |
**result, |
| 258 |
} |
| 259 |
|
| 260 |
return self._update_contact(name) |
| 261 |
|
| 262 |
# Already match |
| 263 |
if self._is_contact_up_to_date(existing): |
| 264 |
result = acme_account_to_ansible_result(existing) |
| 265 |
return { |
| 266 |
"changed": False, |
| 267 |
"name": name, |
| 268 |
"msg": f"ACME account {name} already has desired contact", |
| 269 |
**result, |
| 270 |
} |
| 271 |
|
| 272 |
# Needs update |
| 273 |
if self.module.check_mode: |
| 274 |
result = acme_account_to_ansible_result(existing) |
| 275 |
return { |
| 276 |
"changed": True, |
| 277 |
"name": name, |
| 278 |
"msg": f"ACME account {name} contact would be updated", |
| 279 |
**result, |
| 280 |
} |
| 281 |
|
| 282 |
return self._update_contact(name) |
| 283 |
|
| 284 |
def _has_contact(self): |
| 285 |
contact = self.params.get("contact") |
| 286 |
return bool(contact and to_native(contact).strip()) |
| 287 |
|
| 288 |
def _has_api_contact(self, data): |
| 289 |
account = data.get("account") or {} |
| 290 |
contacts = account.get("contact") |
| 291 |
return bool(contacts and normalize_contact_list(contacts)) |
| 292 |
|
| 293 |
def _is_contact_up_to_date(self, data): |
| 294 |
desired = to_native(self.params["contact"]).strip() |
| 295 |
account = data.get("account") or {} |
| 296 |
current = normalize_contact_list(account.get("contact")) |
| 297 |
|
| 298 |
return bool(current and current[0] == desired) |
| 299 |
|
| 300 |
def _create_account(self, name): |
| 301 |
if not self._has_contact(): |
| 302 |
self.module.fail_json( |
| 303 |
msg="contact is required to create a new ACME account", |
| 304 |
name=name, |
| 305 |
) |
| 306 |
|
| 307 |
try: |
| 308 |
taskid = self._account_endpoint().post(**self._build_create_params()) |
| 309 |
if taskid: |
| 310 |
self._wait_acme_task(taskid) |
| 311 |
except Exception as e: |
| 312 |
self.module.fail_json(msg=f"Failed to create ACME account {name}: {to_native(e)}") |
| 313 |
|
| 314 |
data = self._fetch_account(name) |
| 315 |
if data is None: |
| 316 |
self.module.fail_json( |
| 317 |
msg=f"ACME account {name} not found after create", |
| 318 |
name=name, |
| 319 |
) |
| 320 |
|
| 321 |
result = acme_account_to_ansible_result(data) |
| 322 |
return { |
| 323 |
"changed": True, |
| 324 |
"name": name, |
| 325 |
"msg": f"ACME account {name} successfully created", |
| 326 |
**result, |
| 327 |
} |
| 328 |
|
| 329 |
def _update_contact(self, name): |
| 330 |
try: |
| 331 |
taskid = self._account_endpoint(name).put(contact=self.params["contact"]) |
| 332 |
if taskid: |
| 333 |
self._wait_acme_task(taskid) |
| 334 |
except Exception as e: |
| 335 |
self.module.fail_json(msg=f"Failed to update ACME account {name}: {to_native(e)}") |
| 336 |
|
| 337 |
updated = self._fetch_account(name) |
| 338 |
if updated is None: |
| 339 |
self.module.fail_json( |
| 340 |
msg=f"ACME account {name} not found after update", |
| 341 |
name=name, |
| 342 |
) |
| 343 |
|
| 344 |
result = acme_account_to_ansible_result(updated) |
| 345 |
return { |
| 346 |
"changed": True, |
| 347 |
"name": name, |
| 348 |
"msg": f"ACME account {name} successfully updated", |
| 349 |
**result, |
| 350 |
} |
| 351 |
|
| 352 |
def _delete_account(self, name): |
| 353 |
try: |
| 354 |
taskid = self._account_endpoint(name).delete() |
| 355 |
if taskid: |
| 356 |
self._wait_acme_task(taskid) |
| 357 |
except Exception as e: |
| 358 |
self.module.fail_json(msg=f"Failed to delete ACME account {name}: {to_native(e)}") |
| 359 |
|
| 360 |
def _fetch_account(self, name): |
| 361 |
try: |
| 362 |
return self._account_endpoint(name).get() |
| 363 |
except Exception as e: |
| 364 |
if is_not_found_error(e): |
| 365 |
return None |
| 366 |
self.module.fail_json(msg=f"Failed to read ACME account {name}: {to_native(e)}") |
| 367 |
|
| 368 |
def _account_endpoint(self, name=None): |
| 369 |
base = self.proxmox_api.cluster().acme().account() |
| 370 |
return base if not name else base(name) |
| 371 |
|
| 372 |
def _wait_acme_task(self, taskid): |
| 373 |
node = self.upid_to_node(taskid) |
| 374 |
ok, err = self.api_task_complete(node, taskid, ACME_TASK_TIMEOUT) |
| 375 |
|
| 376 |
if not ok: |
| 377 |
self.module.fail_json( |
| 378 |
msg=f"ACME background task failed: {err}", |
| 379 |
task=taskid, |
| 380 |
) |
| 381 |
|
| 382 |
def _build_create_params(self): |
| 383 |
p = self.params |
| 384 |
|
| 385 |
payload = { |
| 386 |
"name": p["name"], |
| 387 |
"contact": to_native(p["contact"]).strip(), |
| 388 |
} |
| 389 |
|
| 390 |
if p.get("directory"): |
| 391 |
payload["directory"] = p["directory"] |
| 392 |
if p.get("eab_hmac_key"): |
| 393 |
payload["eab-hmac-key"] = p["eab_hmac_key"] |
| 394 |
if p.get("eab_kid"): |
| 395 |
payload["eab-kid"] = p["eab_kid"] |
| 396 |
if p.get("tos"): |
| 397 |
payload["tos_url"] = p["tos"] |
| 398 |
|
| 399 |
return payload |
| 400 |
|
| 401 |
|
| 402 |
def main(): |
| 403 |
module = create_proxmox_module(module_args(), **module_options()) |
| 404 |
proxmox = ProxmoxClusterAcmeAccountAnsible(module) |
| 405 |
proxmox.validate_params() |
| 406 |
|
| 407 |
try: |
| 408 |
proxmox.run() |
| 409 |
except Exception as e: |
| 410 |
module.fail_json(msg=f"An error occurred: {to_native(e)}") |
| 411 |
|
| 412 |
|
| 413 |
if __name__ == "__main__": |
| 414 |
main() |