| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright (c) 2024, IamLunchbox <[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_backup |
| 10 |
author: "Raphael Grieger (@IamLunchbox) <[email protected]>" |
| 11 |
short_description: Start a VM backup in Proxmox VE cluster |
| 12 |
description: |
| 13 |
- Allows you to create backups of KVM and LXC guests in Proxmox VE cluster. |
| 14 |
- Offers the GUI functionality of creating a single backup as well as using the run-now functionality from the cluster backup |
| 15 |
schedule. |
| 16 |
- The mininum required privileges to use this module are C(VM.Backup) and C(Datastore.AllocateSpace) for the respective |
| 17 |
VMs and storage. |
| 18 |
- Most options are optional and if unspecified will be chosen by the Cluster and its default values. |
| 19 |
- Note that this module B(is not idempotent). It always starts a new backup (when not in check mode). |
| 20 |
attributes: |
| 21 |
check_mode: |
| 22 |
support: full |
| 23 |
diff_mode: |
| 24 |
support: none |
| 25 |
options: |
| 26 |
backup_mode: |
| 27 |
description: |
| 28 |
- The mode how Proxmox performs backups. The default is, to create a runtime snapshot including memory. |
| 29 |
- Check U(https://pve.proxmox.com/pve-docs/chapter-vzdump.html#_backup_modes) for an explanation of the differences. |
| 30 |
type: str |
| 31 |
choices: ["snapshot", "suspend", "stop"] |
| 32 |
default: snapshot |
| 33 |
bandwidth: |
| 34 |
description: |
| 35 |
- Limit the I/O bandwidth (in KiB/s) to write backup. V(0) is unlimited. |
| 36 |
type: int |
| 37 |
change_detection_mode: |
| 38 |
description: |
| 39 |
- Set the change detection mode (available from Proxmox VE 8.3). |
| 40 |
- It is only used when backing up containers, Proxmox silently ignores this option when applied to kvm guests. |
| 41 |
type: str |
| 42 |
choices: ["legacy", "data", "metadata"] |
| 43 |
compress: |
| 44 |
description: |
| 45 |
- Enable additional compression of the backup archive. |
| 46 |
- V(0) will use the Proxmox recommended value, depending on your storage target. |
| 47 |
type: str |
| 48 |
choices: ["0", "1", "gzip", "lzo", "zstd"] |
| 49 |
compression_threads: |
| 50 |
description: |
| 51 |
- The number of threads zstd will use to compress the backup. |
| 52 |
- V(0) uses 50% of the available cores, anything larger than V(0) will use exactly as many threads. |
| 53 |
- Is ignored if you specify O(compress=gzip) or O(compress=lzo). |
| 54 |
type: int |
| 55 |
description: |
| 56 |
description: |
| 57 |
- Specify the description of the backup. |
| 58 |
- Needs to be a single line, newline and backslash need to be escaped as V(\\n) and V(\\\\) respectively. |
| 59 |
- If you need variable interpolation, you can set the content as usual through ansible jinja templating and/or let Proxmox |
| 60 |
substitute templates. |
| 61 |
- Proxmox currently supports V({{cluster}}), V({{guestname}}), V({{node}}), and V({{vmid}}) as templating variables. |
| 62 |
Since this is also a jinja delimiter, you need to set these values as raw jinja. |
| 63 |
default: "{{guestname}}" |
| 64 |
type: str |
| 65 |
fleecing: |
| 66 |
description: |
| 67 |
- Enable backup fleecing. Works only for virtual machines and their disks. |
| 68 |
- Must be entered as a string, containing key-value pairs in a list. |
| 69 |
type: str |
| 70 |
mode: |
| 71 |
description: |
| 72 |
- Specifices the mode to select backup targets. |
| 73 |
choices: ["include", "all", "pool"] |
| 74 |
required: true |
| 75 |
type: str |
| 76 |
node: |
| 77 |
description: |
| 78 |
- Only execute the backup job for the given node. |
| 79 |
- This option is usually used if O(mode=all). |
| 80 |
- If you specify a node ID and your vmids or pool do not reside there, they will not be backed up! |
| 81 |
type: str |
| 82 |
notification_mode: |
| 83 |
description: |
| 84 |
- Determine which notification system to use. |
| 85 |
type: str |
| 86 |
choices: ["auto", "legacy-sendmail", "notification-system"] |
| 87 |
default: auto |
| 88 |
performance_tweaks: |
| 89 |
description: |
| 90 |
- Enable other performance-related settings. |
| 91 |
- Must be entered as a string, containing comma separated key-value pairs. |
| 92 |
- 'For example: V(max-workers=2,pbs-entries-max=2).' |
| 93 |
type: str |
| 94 |
pool: |
| 95 |
description: |
| 96 |
- Specify a pool name to limit backups to guests to the given pool. |
| 97 |
- Required, when O(mode=pool). |
| 98 |
- Also required, when your user only has VM.Backup permission for this single pool. |
| 99 |
type: str |
| 100 |
protected: |
| 101 |
description: |
| 102 |
- Marks backups as protected. |
| 103 |
- '"Might fail, when the PBS backend has verify enabled due to this bug: U(https://bugzilla.proxmox.com/show_bug.cgi?id=4289)".' |
| 104 |
type: bool |
| 105 |
retention: |
| 106 |
description: |
| 107 |
- Use custom retention options instead of those from the default cluster configuration (which is usually V("keep-all=1")). |
| 108 |
- Always requires Datastore.Allocate permission at the storage endpoint. |
| 109 |
- Specifying a retention time other than V(keep-all=1) might trigger pruning on the datastore, if an existing backup |
| 110 |
should be deleted due to your specified timeframe. |
| 111 |
- Deleting requires C(Datastore.Modify) or C(Datastore.Prune) permissions on the backup storage. |
| 112 |
type: str |
| 113 |
storage: |
| 114 |
description: |
| 115 |
- Store the backup archive on this storage. |
| 116 |
type: str |
| 117 |
required: true |
| 118 |
vmids: |
| 119 |
description: |
| 120 |
- The instance IDs to be backed up. |
| 121 |
- Only valid, if O(mode=include). |
| 122 |
type: list |
| 123 |
elements: int |
| 124 |
wait: |
| 125 |
description: |
| 126 |
- Wait for the backup to be finished. |
| 127 |
- Fails, if job does not succeed successfully within the given timeout. |
| 128 |
type: bool |
| 129 |
default: false |
| 130 |
wait_timeout: |
| 131 |
description: |
| 132 |
- Seconds to wait for the backup to be finished. |
| 133 |
- Will only be evaluated, if O(wait=true). |
| 134 |
type: int |
| 135 |
default: 10 |
| 136 |
extends_documentation_fragment: |
| 137 |
- community.proxmox.proxmox.actiongroup_proxmox |
| 138 |
- community.proxmox.proxmox.documentation |
| 139 |
- community.proxmox.attributes |
| 140 |
""" |
| 141 |
|
| 142 |
EXAMPLES = r""" |
| 143 |
- name: Backup all vms in the Proxmox cluster to storage mypbs |
| 144 |
community.proxmox.proxmox_backup: |
| 145 |
storage: mypbs |
| 146 |
mode: all |
| 147 |
|
| 148 |
- name: Backup VMID 100 by stopping it and set an individual retention |
| 149 |
community.proxmox.proxmox_backup: |
| 150 |
backup-mode: stop |
| 151 |
mode: include |
| 152 |
retention: keep-daily=5, keep-last=14, keep-monthly=4, keep-weekly=4, keep-yearly=0 |
| 153 |
storage: mypbs |
| 154 |
vmid: [100] |
| 155 |
|
| 156 |
- name: Backup all vms on node node2 to storage mypbs and wait for the task to finish |
| 157 |
community.proxmox.proxmox_backup: |
| 158 |
storage: mypbs |
| 159 |
mode: all |
| 160 |
node: node2 |
| 161 |
wait: true |
| 162 |
wait_timeout: 30 |
| 163 |
|
| 164 |
- name: Use all the options |
| 165 |
community.proxmox.proxmox_backup: |
| 166 |
bandwidth: 1000 |
| 167 |
backup_mode: suspend |
| 168 |
compress: zstd |
| 169 |
compression_threads: 0 |
| 170 |
description: A single backup for {% raw %}{{ guestname }}{% endraw %} |
| 171 |
mode: include |
| 172 |
notification_mode: notification-system |
| 173 |
protected: true |
| 174 |
retention: keep-monthly=1, keep-weekly=1 |
| 175 |
storage: mypbs |
| 176 |
vmids: |
| 177 |
- 100 |
| 178 |
- 101 |
| 179 |
""" |
| 180 |
|
| 181 |
RETURN = r""" |
| 182 |
backups: |
| 183 |
description: List of nodes and their task IDs. |
| 184 |
returned: on success |
| 185 |
type: list |
| 186 |
elements: dict |
| 187 |
contains: |
| 188 |
node: |
| 189 |
description: Node ID. |
| 190 |
returned: on success |
| 191 |
type: str |
| 192 |
status: |
| 193 |
description: Last known task status. Will be unknown, if O(wait=false). |
| 194 |
returned: on success |
| 195 |
type: str |
| 196 |
choices: ["unknown", "success", "failed"] |
| 197 |
upid: |
| 198 |
description: >- |
| 199 |
Proxmox cluster UPID, which is needed to lookup task info. Returns OK, when a cluster node did not create a task after |
| 200 |
being called, for example due to no matching targets. |
| 201 |
returned: on success |
| 202 |
type: str |
| 203 |
""" |
| 204 |
|
| 205 |
import time |
| 206 |
|
| 207 |
from ansible.module_utils.common.text.converters import to_native |
| 208 |
|
| 209 |
from ansible_collections.community.proxmox.plugins.module_utils.proxmox import ( |
| 210 |
ProxmoxAnsible, |
| 211 |
create_proxmox_module, |
| 212 |
) |
| 213 |
|
| 214 |
|
| 215 |
def has_permission(permission_tree, permission, search_scopes, default=0, expected=1): |
| 216 |
return any(permission_tree.get(scope, {}).get(permission, default) == expected for scope in search_scopes) |
| 217 |
|
| 218 |
|
| 219 |
def module_args(): |
| 220 |
return dict( |
| 221 |
backup_mode=dict(type="str", default="snapshot", choices=["snapshot", "suspend", "stop"]), |
| 222 |
bandwidth=dict(type="int"), |
| 223 |
change_detection_mode=dict(type="str", choices=["legacy", "data", "metadata"]), |
| 224 |
compress=dict(type="str", choices=["0", "1", "gzip", "lzo", "zstd"]), |
| 225 |
compression_threads=dict(type="int"), |
| 226 |
description=dict(type="str", default="{{guestname}}"), |
| 227 |
fleecing=dict(type="str"), |
| 228 |
mode=dict(type="str", required=True, choices=["include", "all", "pool"]), |
| 229 |
node=dict(type="str"), |
| 230 |
notification_mode=dict( |
| 231 |
type="str", |
| 232 |
default="auto", |
| 233 |
choices=["auto", "legacy-sendmail", "notification-system"], |
| 234 |
), |
| 235 |
performance_tweaks=dict(type="str"), |
| 236 |
pool=dict(type="str"), |
| 237 |
protected=dict(type="bool"), |
| 238 |
retention=dict(type="str"), |
| 239 |
storage=dict(type="str", required=True), |
| 240 |
vmids=dict(type="list", elements="int"), |
| 241 |
wait=dict(type="bool", default=False), |
| 242 |
wait_timeout=dict(type="int", default=10), |
| 243 |
) |
| 244 |
|
| 245 |
|
| 246 |
def module_options(): |
| 247 |
return dict(required_if=[("mode", "include", ("vmids",), True), ("mode", "pool", ("pool",))]) |
| 248 |
|
| 249 |
|
| 250 |
class ProxmoxBackupAnsible(ProxmoxAnsible): |
| 251 |
def _get_permissions(self): |
| 252 |
return self.proxmox_api.access.permissions.get() |
| 253 |
|
| 254 |
def _get_resources(self, resource_type=None): |
| 255 |
return self.proxmox_api.cluster.resources.get(type=resource_type) |
| 256 |
|
| 257 |
def _get_tasklog(self, node, upid): |
| 258 |
return self.proxmox_api.nodes(node).tasks(upid).log.get() |
| 259 |
|
| 260 |
def _get_taskok(self, node, upid): |
| 261 |
return self.proxmox_api.nodes(node).tasks(upid).status.get() |
| 262 |
|
| 263 |
def _post_vzdump(self, node, request_body): |
| 264 |
return self.proxmox_api.nodes(node).vzdump.post(**request_body) |
| 265 |
|
| 266 |
def request_backup(self, request_body, node_endpoints): |
| 267 |
task_ids = [] |
| 268 |
|
| 269 |
for node in node_endpoints: |
| 270 |
upid = self._post_vzdump(node, request_body) |
| 271 |
if upid != "OK": |
| 272 |
tasklog = ", ".join(logentry["t"] for logentry in self._get_tasklog(node, upid)) |
| 273 |
else: |
| 274 |
tasklog = "" |
| 275 |
task_ids.extend([{"node": node, "upid": upid, "status": "unknown", "log": f"{tasklog}"}]) |
| 276 |
return task_ids |
| 277 |
|
| 278 |
def check_relevant_nodes(self, node): |
| 279 |
nodes = [item["node"] for item in self._get_resources("node") if item["status"] == "online"] |
| 280 |
if node and node not in nodes: |
| 281 |
self.module.fail_json(msg=f"Node {node} was specified, but does not exist on the cluster") |
| 282 |
elif node: |
| 283 |
return [node] |
| 284 |
return nodes |
| 285 |
|
| 286 |
def check_storage_permissions(self, permissions, storage, bandwidth, performance, retention): |
| 287 |
# Check for Datastore.AllocateSpace in the permission tree |
| 288 |
if not has_permission( |
| 289 |
permissions, "Datastore.AllocateSpace", search_scopes=["/", "/storage/", "/storage/" + storage] |
| 290 |
): |
| 291 |
self.module.fail_json(changed=False, msg="Insufficient permission: Datastore.AllocateSpace is missing") |
| 292 |
|
| 293 |
if (bandwidth or performance) and has_permission(permissions, "Sys.Modify", search_scopes=["/"], expected=0): |
| 294 |
self.module.fail_json( |
| 295 |
changed=False, |
| 296 |
msg="Insufficient permission: Performance_tweaks and bandwidth require 'Sys.Modify' permission for '/'", |
| 297 |
) |
| 298 |
|
| 299 |
if not retention: |
| 300 |
return |
| 301 |
|
| 302 |
if not has_permission( |
| 303 |
permissions, "Datastore.Allocate", search_scopes=["/", "/storage", "/storage/" + storage] |
| 304 |
): |
| 305 |
self.module.fail_json( |
| 306 |
changed=False, |
| 307 |
msg="Insufficient permissions: Custom retention was requested, but Datastore.Allocate is missing", |
| 308 |
) |
| 309 |
|
| 310 |
def check_vmid_backup_permission(self, permissions, vmids, pool): |
| 311 |
sufficient_permissions = has_permission(permissions, "VM.Backup", search_scopes=["/", "/vms"]) |
| 312 |
if pool and not sufficient_permissions: |
| 313 |
sufficient_permissions = has_permission( |
| 314 |
permissions, "VM.Backup", search_scopes=["/pool/" + pool, "/pool/" + pool + "/vms"] |
| 315 |
) |
| 316 |
|
| 317 |
if not sufficient_permissions: |
| 318 |
# Since VM.Backup can be given for each vmid at a time, iterate through all of them |
| 319 |
# and check, if the permission is set |
| 320 |
failed_vmids = [] |
| 321 |
for vm in vmids: |
| 322 |
vm_path = "/vms/" + str(vm) |
| 323 |
if has_permission(permissions, "VM.Backup", search_scopes=[vm_path], default=1, expected=0): |
| 324 |
failed_vmids.append(str(vm)) |
| 325 |
if failed_vmids: |
| 326 |
self.module.fail_json( |
| 327 |
changed=False, |
| 328 |
msg="Insufficient permissions: " |
| 329 |
f"You dont have the VM.Backup permission for VMID {', '.join(failed_vmids)}", |
| 330 |
) |
| 331 |
sufficient_permissions = True |
| 332 |
# Finally, when no check succeeded, fail |
| 333 |
if not sufficient_permissions: |
| 334 |
self.module.fail_json( |
| 335 |
changed=False, msg="Insufficient permissions: You do not have the VM.Backup permission" |
| 336 |
) |
| 337 |
|
| 338 |
def check_general_backup_permission(self, permissions, pool): |
| 339 |
if not has_permission( |
| 340 |
permissions, "VM.Backup", search_scopes=["/", "/vms"] + (["/pool/" + pool] if pool else []) |
| 341 |
): |
| 342 |
self.module.fail_json(changed=False, msg="Insufficient permissions: You dont have the VM.Backup permission") |
| 343 |
|
| 344 |
def check_if_storage_exists(self, storage, node): |
| 345 |
storages = self.get_storages(None) |
| 346 |
# Loop through all cluster storages and get all matching storages |
| 347 |
validated_storagepath = [storageentry for storageentry in storages if storageentry["storage"] == storage] |
| 348 |
if not validated_storagepath: |
| 349 |
self.module.fail_json(changed=False, msg=f"Storage {storage} does not exist in the cluster") |
| 350 |
|
| 351 |
def check_vmids(self, vmids): |
| 352 |
cluster_vmids = [vm["vmid"] for vm in self._get_resources("vm")] |
| 353 |
if not cluster_vmids: |
| 354 |
self.module.warn( |
| 355 |
"VM.Audit permission is missing or there are no VMs. This task might fail if one VMID does not exist" |
| 356 |
) |
| 357 |
return |
| 358 |
vmids_not_found = [str(vm) for vm in vmids if vm not in cluster_vmids] |
| 359 |
if vmids_not_found: |
| 360 |
self.module.warn( |
| 361 |
f"VMIDs {', '.join(vmids_not_found)} not found. This task will fail if one VMID does not exist" |
| 362 |
) |
| 363 |
|
| 364 |
def wait_for_timeout(self, timeout, raw_tasks): # noqa: PLR0912 |
| 365 |
# filter all entries, which did not get a task id from the Cluster |
| 366 |
tasks = [] |
| 367 |
ok_tasks = [] |
| 368 |
for node in raw_tasks: |
| 369 |
if node["upid"] != "OK": |
| 370 |
tasks.append(node) |
| 371 |
else: |
| 372 |
ok_tasks.append(node) |
| 373 |
|
| 374 |
start_time = time.time() |
| 375 |
# iterate through the task ids and check their values |
| 376 |
while True: |
| 377 |
for node in tasks: |
| 378 |
if node["status"] == "unknown": |
| 379 |
try: |
| 380 |
# proxmox.api_task_ok does not suffice, since it only |
| 381 |
# is true at `stopped` and `ok` |
| 382 |
status = self._get_taskok(node["node"], node["upid"]) |
| 383 |
if status["status"] == "stopped" and status["exitstatus"] == "OK": |
| 384 |
node["status"] = "success" |
| 385 |
if status["status"] == "stopped" and status["exitstatus"] == "job errors": |
| 386 |
node["status"] = "failed" |
| 387 |
except Exception as e: |
| 388 |
self.module.fail_json(msg=f"Unable to retrieve API task ID from node {node['node']}: {e}") |
| 389 |
if len([item for item in tasks if item["status"] != "unknown"]) == len(tasks): |
| 390 |
break |
| 391 |
if time.time() > start_time + timeout: |
| 392 |
timeouted_nodes = [node["node"] for node in tasks if node["status"] == "unknown"] |
| 393 |
failed_nodes = [node["node"] for node in tasks if node["status"] == "failed"] |
| 394 |
if failed_nodes: |
| 395 |
self.module.fail_json( |
| 396 |
msg="Reached timeout while waiting for backup task. " |
| 397 |
f"Nodes, who reached the timeout: {', '.join(timeouted_nodes)}. " |
| 398 |
f"Nodes, which failed: {', '.join(failed_nodes)}" |
| 399 |
) |
| 400 |
self.module.fail_json( |
| 401 |
msg="Reached timeout while waiting for creating VM snapshot. " |
| 402 |
f"Nodes who reached the timeout: {', '.join(timeouted_nodes)}" |
| 403 |
) |
| 404 |
time.sleep(1) |
| 405 |
|
| 406 |
error_logs = [] |
| 407 |
for node in tasks: |
| 408 |
if node["status"] == "failed": |
| 409 |
tasklog = ", ".join([logentry["t"] for logentry in self._get_tasklog(node["node"], node["upid"])]) |
| 410 |
error_logs.append(f"{node}: {tasklog}") |
| 411 |
if error_logs: |
| 412 |
self.module.fail_json( |
| 413 |
msg="An error occured creating the backups. " |
| 414 |
f"These are the last log lines from the failed nodes: {', '.join(error_logs)}" |
| 415 |
) |
| 416 |
|
| 417 |
for node in tasks: |
| 418 |
tasklog = ", ".join([logentry["t"] for logentry in self._get_tasklog(node["node"], node["upid"])]) |
| 419 |
node["log"] = tasklog |
| 420 |
|
| 421 |
# Finally, reattach ok tasks to show, that all nodes were contacted |
| 422 |
tasks.extend(ok_tasks) |
| 423 |
return tasks |
| 424 |
|
| 425 |
def permission_check(self, storage, mode, node, bandwidth, performance_tweaks, retention, pool, vmids): # noqa: PLR0913 |
| 426 |
permissions = self._get_permissions() |
| 427 |
self.check_if_storage_exists(storage, node) |
| 428 |
self.check_storage_permissions(permissions, storage, bandwidth, performance_tweaks, retention) |
| 429 |
if mode == "include": |
| 430 |
self.check_vmid_backup_permission(permissions, vmids, pool) |
| 431 |
else: |
| 432 |
self.check_general_backup_permission(permissions, pool) |
| 433 |
|
| 434 |
def prepare_request_parameters(self, module_arguments): |
| 435 |
# ensure only valid post parameters are passed to proxmox |
| 436 |
# list of dict items to replace with (new_val, old_val) |
| 437 |
post_params = [ |
| 438 |
("bwlimit", "bandwidth"), |
| 439 |
("compress", "compress"), |
| 440 |
("fleecing", "fleecing"), |
| 441 |
("mode", "backup_mode"), |
| 442 |
("notes-template", "description"), |
| 443 |
("notification-mode", "notification_mode"), |
| 444 |
("pbs-change-detection-mode", "change_detection_mode"), |
| 445 |
("performance", "performance_tweaks"), |
| 446 |
("pool", "pool"), |
| 447 |
("protected", "protected"), |
| 448 |
("prune-backups", "retention"), |
| 449 |
("storage", "storage"), |
| 450 |
("zstd", "compression_threads"), |
| 451 |
("vmid", "vmids"), |
| 452 |
] |
| 453 |
request_body = {} |
| 454 |
for new, old in post_params: |
| 455 |
if module_arguments.get(old): |
| 456 |
request_body.update({new: module_arguments[old]}) |
| 457 |
|
| 458 |
# Set mode specific values |
| 459 |
if module_arguments["mode"] == "include": |
| 460 |
request_body.pop("pool", None) |
| 461 |
request_body["all"] = 0 |
| 462 |
elif module_arguments["mode"] == "all": |
| 463 |
request_body.pop("vmid", None) |
| 464 |
request_body.pop("pool", None) |
| 465 |
request_body["all"] = 1 |
| 466 |
elif module_arguments["mode"] == "pool": |
| 467 |
request_body.pop("vmid", None) |
| 468 |
request_body["all"] = 0 |
| 469 |
|
| 470 |
# Create comma separated list from vmids, the API expects so |
| 471 |
if request_body.get("vmid"): |
| 472 |
request_body.update({"vmid": ",".join(str(vmid) for vmid in request_body["vmid"])}) |
| 473 |
|
| 474 |
# remove whitespaces from option strings |
| 475 |
for key in ("prune-backups", "performance"): |
| 476 |
if request_body.get(key): |
| 477 |
request_body[key] = request_body[key].replace(" ", "") |
| 478 |
# convert booleans to 0/1 |
| 479 |
for key in ("protected",): |
| 480 |
if request_body.get(key): |
| 481 |
request_body[key] = 1 |
| 482 |
return request_body |
| 483 |
|
| 484 |
def backup_create(self, module_arguments, check_mode, node_endpoints): |
| 485 |
request_body = self.prepare_request_parameters(module_arguments) |
| 486 |
# stop here, before anything gets changed |
| 487 |
if check_mode: |
| 488 |
return [] |
| 489 |
|
| 490 |
task_ids = self.request_backup(request_body, node_endpoints) |
| 491 |
updated_task_ids = [] |
| 492 |
if module_arguments["wait"]: |
| 493 |
updated_task_ids = self.wait_for_timeout(module_arguments["wait_timeout"], task_ids) |
| 494 |
return updated_task_ids if updated_task_ids else task_ids |
| 495 |
|
| 496 |
|
| 497 |
def main(): |
| 498 |
module = create_proxmox_module(module_args(), **module_options()) |
| 499 |
proxmox = ProxmoxBackupAnsible(module) |
| 500 |
|
| 501 |
bandwidth = module.params["bandwidth"] |
| 502 |
mode = module.params["mode"] |
| 503 |
node = module.params["node"] |
| 504 |
performance_tweaks = module.params["performance_tweaks"] |
| 505 |
pool = module.params["pool"] |
| 506 |
retention = module.params["retention"] |
| 507 |
storage = module.params["storage"] |
| 508 |
vmids = module.params["vmids"] |
| 509 |
|
| 510 |
proxmox.permission_check(storage, mode, node, bandwidth, performance_tweaks, retention, pool, vmids) |
| 511 |
if module.params["mode"] == "include": |
| 512 |
proxmox.check_vmids(module.params["vmids"]) |
| 513 |
node_endpoints = proxmox.check_relevant_nodes(module.params["node"]) |
| 514 |
try: |
| 515 |
result = proxmox.backup_create(module.params, module.check_mode, node_endpoints) |
| 516 |
except Exception as e: |
| 517 |
module.fail_json(msg=f"Creating backups failed with exception: {to_native(e)}") |
| 518 |
|
| 519 |
if module.check_mode: |
| 520 |
module.exit_json(backups=result, changed=True, msg="Backups would be created") |
| 521 |
|
| 522 |
elif len([entry for entry in result if entry["upid"] == "OK"]) == len(result): |
| 523 |
module.exit_json(backups=result, changed=False, msg="Backup request sent to proxmox, no tasks created") |
| 524 |
|
| 525 |
elif module.params["wait"]: |
| 526 |
module.exit_json(backups=result, changed=True, msg="Backups succeeded") |
| 527 |
|
| 528 |
else: |
| 529 |
module.exit_json(backups=result, changed=True, msg="Backup tasks created") |
| 530 |
|
| 531 |
|
| 532 |
if __name__ == "__main__": |
| 533 |
main() |