| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright (c) 2025, Peter Tselios (@icultus) <[email protected]> |
| 4 |
# Copyright (c) 2025, Florian Paul Azim Hoberg (@gyptazy) <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: proxmox_node |
| 11 |
version_added: 1.2.0 |
| 12 |
short_description: Manage Proxmox VE nodes |
| 13 |
description: |
| 14 |
- Manage the Proxmox VE nodes itself. |
| 15 |
author: |
| 16 |
- Florian Paul Azim Hoberg (@gyptazy) |
| 17 |
- Peter Tselios (@itcultus) |
| 18 |
attributes: |
| 19 |
check_mode: |
| 20 |
support: full |
| 21 |
diff_mode: |
| 22 |
support: none |
| 23 |
options: |
| 24 |
node_name: |
| 25 |
description: |
| 26 |
- The targeted node to perform actions on. |
| 27 |
type: str |
| 28 |
aliases: ["node"] |
| 29 |
required: true |
| 30 |
power_state: |
| 31 |
description: |
| 32 |
- Manages the power state of the node. |
| 33 |
type: str |
| 34 |
required: false |
| 35 |
choices: ["online", "offline"] |
| 36 |
certificates: |
| 37 |
description: |
| 38 |
- Manages the x509 certificates of the node. |
| 39 |
type: dict |
| 40 |
suboptions: |
| 41 |
cert: |
| 42 |
description: |
| 43 |
- The public certificate file path (including chain) in PEM format on the Ansible Controller. |
| 44 |
- Mutually exclusive with O(certificates.certificate). |
| 45 |
type: str |
| 46 |
aliases: ["certificate_file_path"] |
| 47 |
certificate: |
| 48 |
description: |
| 49 |
- The public certificate as a raw PEM encoded string (including chain). |
| 50 |
- Mutually exclusive with O(certificates.cert). |
| 51 |
type: str |
| 52 |
aliases: ["certificate_raw"] |
| 53 |
key: |
| 54 |
description: |
| 55 |
- The private key file path in PEM format. |
| 56 |
- Mutually exclusive with O(certificates.private_key). |
| 57 |
type: str |
| 58 |
aliases: ["private_key_file_path"] |
| 59 |
private_key: |
| 60 |
description: |
| 61 |
- The private key as a raw PEM encoded string. |
| 62 |
- Mutually exclusive with O(certificates.key). |
| 63 |
type: str |
| 64 |
aliases: ["private_key_raw"] |
| 65 |
state: |
| 66 |
description: |
| 67 |
- Defines the actions for the certificate. |
| 68 |
choices: ["present", "absent"] |
| 69 |
type: str |
| 70 |
restart: |
| 71 |
description: |
| 72 |
- Restart pveproxy to rehash the new certificates. |
| 73 |
type: bool |
| 74 |
default: false |
| 75 |
force: |
| 76 |
description: |
| 77 |
- Overwrite existing custom certificate files. |
| 78 |
type: bool |
| 79 |
default: false |
| 80 |
dns: |
| 81 |
description: |
| 82 |
- Manages the resolving DNS options of the node. |
| 83 |
type: dict |
| 84 |
suboptions: |
| 85 |
dns1: |
| 86 |
description: |
| 87 |
- The IP address of the first DNS resolver. |
| 88 |
type: str |
| 89 |
dns2: |
| 90 |
description: |
| 91 |
- The IP address of the second DNS resolver. |
| 92 |
type: str |
| 93 |
dns3: |
| 94 |
description: |
| 95 |
- The IP address of the third DNS resolver. |
| 96 |
type: str |
| 97 |
search: |
| 98 |
description: |
| 99 |
- The default search domain. |
| 100 |
type: str |
| 101 |
required: true |
| 102 |
subscription: |
| 103 |
description: |
| 104 |
- Manages the license subscription of the node. |
| 105 |
type: dict |
| 106 |
suboptions: |
| 107 |
state: |
| 108 |
description: |
| 109 |
- Defines the actions for the subscription file. |
| 110 |
choices: ["present", "absent"] |
| 111 |
type: str |
| 112 |
key: |
| 113 |
description: |
| 114 |
- The subscription license key. |
| 115 |
type: str |
| 116 |
extends_documentation_fragment: |
| 117 |
- community.proxmox.proxmox.actiongroup_proxmox |
| 118 |
- community.proxmox.proxmox.documentation |
| 119 |
- community.proxmox.attributes |
| 120 |
""" |
| 121 |
|
| 122 |
EXAMPLES = r""" |
| 123 |
- name: Start a Proxmox VE Node |
| 124 |
community.proxmox.node: |
| 125 |
node_name: de-cgn01-virt01 |
| 126 |
power_state: online |
| 127 |
|
| 128 |
- name: Update SSL certificates on a Proxmox VE Node (from files) |
| 129 |
community.proxmox.node: |
| 130 |
node_name: de-cgn01-virt01 |
| 131 |
certificates: |
| 132 |
private_key_file_path: /opt/ansible/key.pem |
| 133 |
certificate_file_path: /opt/ansible/cert.pem |
| 134 |
state: present |
| 135 |
force: false |
| 136 |
|
| 137 |
- name: Update SSL certificates on a Proxmox VE Node (raw PEM) |
| 138 |
community.proxmox.node: |
| 139 |
node_name: de-cgn01-virt01 |
| 140 |
certificates: |
| 141 |
certificate: "{{ pve_node_certificate_content }}" |
| 142 |
private_key: "{{ pve_node_private_key_content }}" |
| 143 |
state: present |
| 144 |
|
| 145 |
- name: Place a subscription license on a Proxmox VE Node |
| 146 |
community.proxmox.node: |
| 147 |
node_name: de-cgn01-virt01 |
| 148 |
subscription: |
| 149 |
state: present |
| 150 |
key: ABCD-EFGH-IJKL-MNOP-QRST-UVWX-YZ0123456789 |
| 151 |
""" |
| 152 |
|
| 153 |
RETURN = r""" |
| 154 |
certificates: |
| 155 |
description: Status message about the certificate on the node. |
| 156 |
returned: success |
| 157 |
type: str |
| 158 |
sample: "Certificate for node 'dev-virt01' is already present." |
| 159 |
changed: |
| 160 |
description: Indicates whether any changes were made. |
| 161 |
returned: success |
| 162 |
type: bool |
| 163 |
sample: true |
| 164 |
dns: |
| 165 |
description: Status message about the DNS configuration on the node. |
| 166 |
returned: success |
| 167 |
type: str |
| 168 |
sample: "DNS configuration for node 'dev-virt01' has been updated." |
| 169 |
power_state: |
| 170 |
description: Status message about the power state of the node. |
| 171 |
returned: success |
| 172 |
type: str |
| 173 |
sample: "Node 'dev-virt01' is already online." |
| 174 |
""" |
| 175 |
|
| 176 |
|
| 177 |
import hashlib |
| 178 |
import re |
| 179 |
import ssl |
| 180 |
|
| 181 |
from ansible_collections.community.proxmox.plugins.module_utils.proxmox import ( |
| 182 |
ProxmoxAnsible, |
| 183 |
ansible_to_proxmox_bool, |
| 184 |
create_proxmox_module, |
| 185 |
) |
| 186 |
|
| 187 |
|
| 188 |
def module_args(): |
| 189 |
return dict( |
| 190 |
node_name=dict(type="str", aliases=["node"], required=True), |
| 191 |
power_state=dict(choices=["online", "offline"]), |
| 192 |
certificates=dict( |
| 193 |
type="dict", |
| 194 |
options=dict( |
| 195 |
cert=dict(type="str", required=False, aliases=["certificate_file_path"], no_log=True), |
| 196 |
certificate=dict(type="str", required=False, aliases=["certificate_raw"], no_log=True), |
| 197 |
key=dict(type="str", required=False, aliases=["private_key_file_path"], no_log=True), |
| 198 |
private_key=dict(type="str", required=False, aliases=["private_key_raw"], no_log=True), |
| 199 |
state=dict(type="str", required=False, choices=["present", "absent"]), |
| 200 |
restart=dict(type="bool", default=False, required=False), |
| 201 |
force=dict(type="bool", default=False, required=False), |
| 202 |
), |
| 203 |
), |
| 204 |
dns=dict( |
| 205 |
type="dict", |
| 206 |
options=dict( |
| 207 |
dns1=dict(type="str", default=None, required=False), |
| 208 |
dns2=dict(type="str", default=None, required=False), |
| 209 |
dns3=dict(type="str", default=None, required=False), |
| 210 |
search=dict(type="str", required=True), |
| 211 |
), |
| 212 |
), |
| 213 |
subscription=dict( |
| 214 |
type="dict", |
| 215 |
options=dict( |
| 216 |
state=dict(type="str", required=False, choices=["present", "absent"]), |
| 217 |
key=dict(type="str", default=None, required=False, no_log=True), |
| 218 |
), |
| 219 |
), |
| 220 |
) |
| 221 |
|
| 222 |
|
| 223 |
def module_options(): |
| 224 |
return {} |
| 225 |
|
| 226 |
|
| 227 |
class ProxmoxNodeAnsible(ProxmoxAnsible): |
| 228 |
def __init__(self, module): |
| 229 |
super().__init__(module) |
| 230 |
self.params = module.params |
| 231 |
|
| 232 |
def get_nodes(self): |
| 233 |
nodes = {"nodes": {}} |
| 234 |
for node in self.proxmox_api.nodes.get(): |
| 235 |
nodes["nodes"][node["node"]] = {} |
| 236 |
nodes["nodes"][node["node"]]["name"] = node["node"] |
| 237 |
nodes["nodes"][node["node"]]["status"] = node["status"] |
| 238 |
return nodes |
| 239 |
|
| 240 |
def validate_node_name(self, nodes): |
| 241 |
node = self.params.get("node_name") |
| 242 |
if node not in nodes["nodes"]: |
| 243 |
self.module.fail_json(msg=f"Node '{node}' not found in the Proxmox cluster.") |
| 244 |
|
| 245 |
def read_file(self, file_path): |
| 246 |
try: |
| 247 |
with open(file_path, "r") as file_handler: |
| 248 |
file_content = file_handler.read() |
| 249 |
return file_content |
| 250 |
except Exception as e: |
| 251 |
self.module.fail_json(msg=f"Failed to read certificate or key file '{file_path}': {e}") |
| 252 |
|
| 253 |
def get_leaf_certificate_fingerprint(self, pem_data, hash_alg="sha256"): |
| 254 |
""" |
| 255 |
Extract the fingerprint of the leaf (first) certificate from a PEM bundle. |
| 256 |
""" |
| 257 |
certs = re.findall( |
| 258 |
r"-----BEGIN CERTIFICATE-----.*?-----END CERTIFICATE-----", |
| 259 |
pem_data, |
| 260 |
re.DOTALL, |
| 261 |
) |
| 262 |
|
| 263 |
if not certs: |
| 264 |
return None |
| 265 |
|
| 266 |
leaf_cert = certs[0] |
| 267 |
|
| 268 |
try: |
| 269 |
der = ssl.PEM_cert_to_DER_cert(leaf_cert.strip()) |
| 270 |
digest = getattr(hashlib, hash_alg)(der).hexdigest() |
| 271 |
|
| 272 |
return ":".join(digest[i : i + 2].upper() for i in range(0, len(digest), 2)) |
| 273 |
except Exception: |
| 274 |
return None |
| 275 |
|
| 276 |
def get_certificate_fingerprints_api(self, certificates): |
| 277 |
fingerprints = [] |
| 278 |
for cert in certificates: |
| 279 |
fingerprints.append(cert.get("fingerprint")) |
| 280 |
return fingerprints |
| 281 |
|
| 282 |
def dicts_differ(self, d1, d2): |
| 283 |
keys = set(d1) | set(d2) |
| 284 |
return any(d1.get(k) != d2.get(k) for k in keys) |
| 285 |
|
| 286 |
def power_state(self, nodes): |
| 287 |
state = self.params.get("power_state") |
| 288 |
node = self.params.get("node_name") |
| 289 |
changed = False |
| 290 |
result = "Unchanged" |
| 291 |
|
| 292 |
if state == "online": |
| 293 |
if nodes["nodes"][node]["status"] == "online": |
| 294 |
changed = False |
| 295 |
result = f"Node '{node}' is already online." |
| 296 |
else: |
| 297 |
if not self.module.check_mode: |
| 298 |
self.proxmox_api.nodes(node).wakeonlan.post(node_name=node) |
| 299 |
changed = True |
| 300 |
result = f"Node '{node}' has been powered on." |
| 301 |
|
| 302 |
if state == "offline": |
| 303 |
if nodes["nodes"][node]["status"] != "online": |
| 304 |
changed = False |
| 305 |
result = f"Node '{node}' is already offline." |
| 306 |
else: |
| 307 |
if not self.module.check_mode: |
| 308 |
self.proxmox_api.nodes(node).status.post(command="shutdown") |
| 309 |
changed = True |
| 310 |
result = f"Node '{node}' has been powered off." |
| 311 |
|
| 312 |
return changed, result |
| 313 |
|
| 314 |
def _get_custom_certificates(self, node): |
| 315 |
try: |
| 316 |
certs = self.proxmox_api.nodes(node).certificates.info.get() |
| 317 |
# Filter out default Proxmox certificates |
| 318 |
custom_certs = [cert for cert in certs if cert.get("filename") not in ["pve-root-ca.pem", "pve-ssl.pem"]] |
| 319 |
return custom_certs |
| 320 |
except Exception as e: |
| 321 |
self.module.fail_json(msg=f"Failed to get certificates information: {str(e)}") |
| 322 |
|
| 323 |
def _certificate_absent(self, node, restart): |
| 324 |
existing_certificates = self._get_custom_certificates(node) |
| 325 |
|
| 326 |
if existing_certificates: |
| 327 |
if self.module.check_mode: |
| 328 |
return True, f"Certificate on node '{node}' would be deleted." |
| 329 |
try: |
| 330 |
self.proxmox_api.nodes(node).certificates.custom.delete(restart=ansible_to_proxmox_bool(restart)) |
| 331 |
return True, f"Certificate on node '{node}' deleted." |
| 332 |
except Exception as e: |
| 333 |
self.module.fail_json(changed=False, msg=f"Failed to delete certificate on node '{node}': {str(e)}") |
| 334 |
else: |
| 335 |
return False, f"Certificate on node '{node}' already absent." |
| 336 |
|
| 337 |
def _certificate_present(self, node, restart, force): # noqa:PLR0912 |
| 338 |
# Certificate: cert (file path) OR certificate (raw string) - at least one required for present |
| 339 |
# Private key: key (file path) OR private_key (raw string) - both optional |
| 340 |
|
| 341 |
certificate_params = self.params["certificates"] |
| 342 |
certificate_file_path = certificate_params.get("cert") |
| 343 |
certificate_raw = certificate_params.get("certificate") |
| 344 |
private_key_file_path = certificate_params.get("key") |
| 345 |
private_key_raw = certificate_params.get("private_key") |
| 346 |
|
| 347 |
if certificate_file_path and certificate_raw: |
| 348 |
self.module.fail_json(msg="Cannot specify both cert (file path) and certificate (raw string).") |
| 349 |
if private_key_file_path and private_key_raw: |
| 350 |
self.module.fail_json(msg="Cannot specify both key (file path) and private_key (raw string).") |
| 351 |
|
| 352 |
if not certificate_file_path and not certificate_raw: |
| 353 |
self.module.fail_json( |
| 354 |
msg="Either cert (file path) or certificate (raw string) is required for state=present." |
| 355 |
) |
| 356 |
|
| 357 |
if certificate_file_path: |
| 358 |
cert_content = self.read_file(certificate_file_path) |
| 359 |
else: |
| 360 |
cert_content = certificate_raw |
| 361 |
|
| 362 |
key_content = None |
| 363 |
if private_key_file_path: |
| 364 |
key_content = self.read_file(private_key_file_path) |
| 365 |
elif private_key_raw: |
| 366 |
key_content = private_key_raw |
| 367 |
|
| 368 |
leaf_certificate_fingerprint = self.get_leaf_certificate_fingerprint(cert_content) |
| 369 |
if not leaf_certificate_fingerprint: |
| 370 |
self.module.fail_json(msg="Failed to parse certificate: no valid PEM certificate found.") |
| 371 |
|
| 372 |
existing_certificates = self._get_custom_certificates(node) |
| 373 |
existing_fingerprints = self.get_certificate_fingerprints_api(existing_certificates) |
| 374 |
|
| 375 |
certificate_already_present = leaf_certificate_fingerprint in existing_fingerprints |
| 376 |
|
| 377 |
if certificate_already_present and not force: |
| 378 |
return False, f"Certificate for node '{node}' is already present." |
| 379 |
|
| 380 |
if self.module.check_mode: |
| 381 |
if certificate_already_present and force: |
| 382 |
return True, f"Certificate for node '{node}' would be overwritten." |
| 383 |
return True, f"Certificate for node '{node}' would be updated." |
| 384 |
|
| 385 |
post_params = { |
| 386 |
"certificates": cert_content, |
| 387 |
"force": ansible_to_proxmox_bool(force), |
| 388 |
"restart": ansible_to_proxmox_bool(restart), |
| 389 |
} |
| 390 |
if key_content: |
| 391 |
post_params["key"] = key_content |
| 392 |
|
| 393 |
try: |
| 394 |
self.proxmox_api.nodes(node).certificates.custom.post(**post_params) |
| 395 |
except Exception as e: |
| 396 |
self.module.fail_json(msg=f"Failed to upload certificate on node '{node}': {str(e)}") |
| 397 |
|
| 398 |
if certificate_already_present and force: |
| 399 |
return True, f"Certificate for node '{node}' has been overwritten." |
| 400 |
return True, f"Certificate for node '{node}' has been updated." |
| 401 |
|
| 402 |
def _restart_pve_proxy_service(self, node): |
| 403 |
if self.module.check_mode: |
| 404 |
return True, "The service pveproxy would be restarted." |
| 405 |
|
| 406 |
try: |
| 407 |
self.proxmox_api.nodes(node).service("pveproxy").restart.post() |
| 408 |
return True, "The service pveproxy has been restarted." |
| 409 |
except Exception as e: |
| 410 |
self.module.warn(f"Failed to restart the service pveproxy: {str(e)}") |
| 411 |
|
| 412 |
def certificates(self): |
| 413 |
state = self.params["certificates"].get("state") |
| 414 |
node = self.params["node_name"] |
| 415 |
restart = self.params["certificates"]["restart"] |
| 416 |
force = self.params["certificates"].get("force") |
| 417 |
|
| 418 |
if force and state is None: |
| 419 |
self.module.fail_json(msg="Force is only supported when state is present or absent.") |
| 420 |
return False, "Unchanged" |
| 421 |
|
| 422 |
if state == "absent": |
| 423 |
changed, msg = self._certificate_absent(node, restart) |
| 424 |
elif state == "present": |
| 425 |
changed, msg = self._certificate_present(node, restart, force) |
| 426 |
elif state is None and restart: |
| 427 |
changed, msg = self._restart_pve_proxy_service(node) |
| 428 |
elif state is None and force: |
| 429 |
self.module.fail_json(msg="Force is only supported when state is present or absent.") |
| 430 |
return False, "Unchanged" |
| 431 |
else: |
| 432 |
return False, "Unchanged" |
| 433 |
|
| 434 |
if not changed: |
| 435 |
return False, msg |
| 436 |
|
| 437 |
return True, msg |
| 438 |
|
| 439 |
def dns(self): |
| 440 |
node_name = self.params.get("node_name") |
| 441 |
dns1 = self.params.get("dns", {}).get("dns1", None) |
| 442 |
dns2 = self.params.get("dns", {}).get("dns2", None) |
| 443 |
dns3 = self.params.get("dns", {}).get("dns3", None) |
| 444 |
search = self.params.get("dns", {}).get("search", None) |
| 445 |
dns_config_current = self.proxmox_api.nodes(node_name).dns.get() |
| 446 |
changed = False |
| 447 |
result_dns = "Unchanged" |
| 448 |
|
| 449 |
dns_config = {} |
| 450 |
if dns1: |
| 451 |
dns_config["dns1"] = dns1 |
| 452 |
if dns2: |
| 453 |
dns_config["dns2"] = dns2 |
| 454 |
if dns3: |
| 455 |
dns_config["dns3"] = dns3 |
| 456 |
if search: |
| 457 |
dns_config["search"] = search |
| 458 |
|
| 459 |
if self.dicts_differ(dns_config_current, dns_config): |
| 460 |
if not self.module.check_mode: |
| 461 |
self.proxmox_api.nodes(node_name).dns.put(**dns_config) |
| 462 |
changed = True |
| 463 |
result_dns = f"DNS configuration for node '{node_name}' has been updated." |
| 464 |
|
| 465 |
return changed, result_dns |
| 466 |
|
| 467 |
def subscription(self): |
| 468 |
subscription_state = self.params.get("subscription", {}).get("state") |
| 469 |
node_name = self.params.get("node_name") |
| 470 |
subscription_current = self.proxmox_api.nodes(node_name).subscription.get() |
| 471 |
changed = False |
| 472 |
result_subscription = "Unchanged" |
| 473 |
|
| 474 |
if subscription_state == "present": |
| 475 |
license_key = self.params.get("subscription", {}).get("key", None) |
| 476 |
if subscription_current.get("key", None) != license_key: |
| 477 |
if not self.module.check_mode: |
| 478 |
try: |
| 479 |
self.proxmox_api.nodes(node_name).subscription.put(key=license_key) |
| 480 |
except Exception as e: |
| 481 |
self.module.fail_json(msg=f"Failed to upload subscription key: {e}") |
| 482 |
changed = True |
| 483 |
result_subscription = f"License subscription for node '{node_name}' has been uploaded." |
| 484 |
|
| 485 |
if subscription_state == "absent" and subscription_current.get("status", None) != "notfound": |
| 486 |
changed = True |
| 487 |
result_subscription = f"License subscription for node '{node_name}' has been deleted." |
| 488 |
if not self.module.check_mode: |
| 489 |
try: |
| 490 |
self.proxmox_api.nodes(node_name).subscription.delete() |
| 491 |
except Exception as e: |
| 492 |
self.module.fail_json(msg=f"Failed to delete subscription key: {e}") |
| 493 |
|
| 494 |
return changed, result_subscription |
| 495 |
|
| 496 |
|
| 497 |
def main(): |
| 498 |
module = create_proxmox_module(module_args(), **module_options()) |
| 499 |
proxmox = ProxmoxNodeAnsible(module) |
| 500 |
|
| 501 |
# Initialize objects and avoid re-polling the current |
| 502 |
# nodes in the cluster in each function call. |
| 503 |
nodes = proxmox.get_nodes() |
| 504 |
proxmox.validate_node_name(nodes) |
| 505 |
result = {"changed": False} |
| 506 |
|
| 507 |
# Actions |
| 508 |
if module.params.get("power_state") is not None: |
| 509 |
changed, power_result = proxmox.power_state(nodes) |
| 510 |
result["changed"] = result["changed"] or changed |
| 511 |
result["power_state"] = power_result |
| 512 |
|
| 513 |
if module.params.get("certificates") is not None: |
| 514 |
changed, certificates_result = proxmox.certificates() |
| 515 |
result["changed"] = result["changed"] or changed |
| 516 |
result["certificates"] = certificates_result |
| 517 |
|
| 518 |
if module.params.get("dns") is not None: |
| 519 |
changed, dns_result = proxmox.dns() |
| 520 |
result["changed"] = result["changed"] or changed |
| 521 |
result["dns"] = dns_result |
| 522 |
|
| 523 |
if module.params.get("subscription") is not None: |
| 524 |
changed, subscription_result = proxmox.subscription() |
| 525 |
result["changed"] = result["changed"] or changed |
| 526 |
result["subscription"] = subscription_result |
| 527 |
|
| 528 |
module.exit_json(**result) |
| 529 |
|
| 530 |
|
| 531 |
if __name__ == "__main__": |
| 532 |
main() |