| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright (c) 2025 Marzieh Raoufnezhad <[email protected]> |
| 4 |
# Copyright (c) 2025 Maryam Mayabi <mayabi.ahm at gmail.com> |
| 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 = """ |
| 10 |
--- |
| 11 |
module: proxmox_backup_schedule |
| 12 |
|
| 13 |
short_description: Schedule VM backups and removing them |
| 14 |
|
| 15 |
description: The module modifies backup jobs such as set or delete C(vmid). |
| 16 |
|
| 17 |
author: |
| 18 |
- "Marzieh Raoufnezhad (@raoufnezhad) <[email protected]>" |
| 19 |
- "Maryam Mayabi (@mmayabi) <[email protected]>" |
| 20 |
version_added: 1.0.0 |
| 21 |
options: |
| 22 |
vm_name: |
| 23 |
description: |
| 24 |
- The name of the Proxmox VM. |
| 25 |
- Mutually exclusive with O(vm_id). |
| 26 |
type: str |
| 27 |
vm_id: |
| 28 |
description: |
| 29 |
- The ID of the Proxmox VM. |
| 30 |
- Mutually exclusive with O(vm_name). |
| 31 |
type: str |
| 32 |
backup_id: |
| 33 |
description: The backup job ID. |
| 34 |
type: str |
| 35 |
state: |
| 36 |
description: |
| 37 |
- If V(present), ensure that VM is present in the backup job on defined O(backup_id). |
| 38 |
- If V(absent), ensure that VM is not present in the backup job if O(backup_id) is defined and otherwise, it should not exist in any of the backup jobs. |
| 39 |
required: true |
| 40 |
choices: ["present", "absent"] |
| 41 |
type: str |
| 42 |
|
| 43 |
extends_documentation_fragment: |
| 44 |
- community.proxmox.proxmox.documentation |
| 45 |
- community.proxmox.attributes |
| 46 |
- community.proxmox.attributes.info_module |
| 47 |
- community.proxmox.proxmox.actiongroup_proxmox |
| 48 |
""" |
| 49 |
|
| 50 |
EXAMPLES = """ |
| 51 |
- name: Ensure that VM is present in the backup job |
| 52 |
community.proxmox.proxmox_backup_schedule: |
| 53 |
vm_name: 'VM Name' |
| 54 |
backup_id: 'backup-b2adffdc-316e' |
| 55 |
state: 'present' |
| 56 |
|
| 57 |
- name: Ensure that vmid is present in the backup job |
| 58 |
community.proxmox.proxmox_backup_schedule: |
| 59 |
vm_id: 'VM ID' |
| 60 |
backup_id: 'backup-b2adffdc-316e' |
| 61 |
state: 'present' |
| 62 |
|
| 63 |
- name: Ensure that there is no scheduled backup for VM name within all backup jobs |
| 64 |
community.proxmox.proxmox_backup_schedule: |
| 65 |
vm_name: 'VM Name' |
| 66 |
state: 'absent' |
| 67 |
|
| 68 |
- name: Ensure that there is no scheduled backup for vmid within all backup jobs |
| 69 |
community.proxmox.proxmox_backup_schedule: |
| 70 |
vm_id: 'VM ID' |
| 71 |
state: 'absent' |
| 72 |
|
| 73 |
- name: Ensure that there is no scheduled backup for VM within specific backup job |
| 74 |
community.proxmox.proxmox_backup_schedule: |
| 75 |
vm_name: 'VM Name' |
| 76 |
backup_id: 'backup-b2adffdc-316e' |
| 77 |
state: 'absent' |
| 78 |
|
| 79 |
- name: Ensure that there is no scheduled backup for vmid within specific backup job |
| 80 |
community.proxmox.proxmox_backup_schedule: |
| 81 |
vm_id: 'VM ID' |
| 82 |
backup_id: 'backup-b2adffdc-316e' |
| 83 |
state: 'absent' |
| 84 |
""" |
| 85 |
|
| 86 |
RETURN = """ |
| 87 |
""" |
| 88 |
|
| 89 |
|
| 90 |
from ansible_collections.community.proxmox.plugins.module_utils.proxmox import ( |
| 91 |
ProxmoxAnsible, |
| 92 |
create_proxmox_module, |
| 93 |
) |
| 94 |
|
| 95 |
|
| 96 |
def module_args(): |
| 97 |
return dict( |
| 98 |
vm_name=dict(type="str"), |
| 99 |
vm_id=dict(type="str"), |
| 100 |
backup_id=dict(type="str"), |
| 101 |
state=dict(choices=["present", "absent"], required=True), |
| 102 |
) |
| 103 |
|
| 104 |
|
| 105 |
def module_options(): |
| 106 |
return dict(mutually_exclusive=[("vm_id", "vm_name")]) |
| 107 |
|
| 108 |
|
| 109 |
class ProxmoxSetVMBackupAnsible(ProxmoxAnsible): |
| 110 |
def list_backup_schedules(self): |
| 111 |
try: |
| 112 |
backupSections = self.proxmox_api.cluster.backup.get() |
| 113 |
except Exception as e: |
| 114 |
self.module.fail_json(msg=f"Getting backup sections failed: {e}") |
| 115 |
return backupSections |
| 116 |
|
| 117 |
def get_backup_job_info(self, backup_id): |
| 118 |
try: |
| 119 |
specificBackupID = self.proxmox_api.cluster.backup.get(backup_id) |
| 120 |
except Exception as e: |
| 121 |
self.module.fail_json(msg=f"Getting specific backup ID failed: {e}") |
| 122 |
return specificBackupID |
| 123 |
|
| 124 |
def update_backup_job_vmid(self, backup_id, updated_backup_vmids): |
| 125 |
try: |
| 126 |
self.proxmox_api.cluster.backup.put(backup_id, vmid=updated_backup_vmids) |
| 127 |
except Exception as e: |
| 128 |
self.module.fail_json(msg=f"Setting vmid backup failed: {e}") |
| 129 |
|
| 130 |
def get_vms_list(self): |
| 131 |
"""Retrieve the list of all virtual machines in the cluster.""" |
| 132 |
try: |
| 133 |
vms = self.proxmox_api.cluster.resources.get(type="vm") |
| 134 |
except Exception as e: |
| 135 |
self.module.fail_json(msg=f"Getting vms info from cluster failed: {e}") |
| 136 |
return vms |
| 137 |
|
| 138 |
def get_vmid_from_vmname(self, vmname): |
| 139 |
"""Convert vm name to vm ID.""" |
| 140 |
vmInfo = self.get_vms_list() |
| 141 |
vms = [vm for vm in vmInfo if vm["name"] == vmname] |
| 142 |
return vms[0]["vmid"] |
| 143 |
|
| 144 |
def ensure_backup_present(self, vm_id, backup_id): |
| 145 |
"""Add vmid to backup job.""" |
| 146 |
backup_job_info = self.get_backup_job_info(backup_id) |
| 147 |
|
| 148 |
"""If backup_job_info is a list, get the first item (assuming there's only one backup job returned).""" |
| 149 |
if isinstance(backup_job_info, list): |
| 150 |
backup_job_info = backup_job_info[0] |
| 151 |
vmids = backup_job_info["vmid"].split(",") |
| 152 |
if str(vm_id) not in vmids: |
| 153 |
updated_backup_vmids = backup_job_info["vmid"] + "," + str(vm_id) |
| 154 |
self.update_backup_job_vmid(backup_id, updated_backup_vmids) |
| 155 |
return True |
| 156 |
else: |
| 157 |
return False |
| 158 |
|
| 159 |
def ensure_backup_absent(self, vm_id, backup_id): |
| 160 |
"""Delete vmid from backup job.""" |
| 161 |
if backup_id is not None and backup_id != "": |
| 162 |
backup_job_info = self.get_backup_job_info(backup_id) |
| 163 |
if isinstance(backup_job_info, list): |
| 164 |
backup_job_info = backup_job_info[0] |
| 165 |
vmids = backup_job_info["vmid"].split(",") |
| 166 |
if str(vm_id) in vmids: |
| 167 |
if len(vmids) > 1: |
| 168 |
vmids.remove(str(vm_id)) |
| 169 |
new_vmids = ",".join(map(str, vmids)) |
| 170 |
self.update_backup_job_vmid(backup_job_info["id"], new_vmids) |
| 171 |
return True |
| 172 |
else: |
| 173 |
self.module.fail_json( |
| 174 |
msg=f"No more than one vmid is assigned to {backup_job_info['id']}. You just can remove job." |
| 175 |
) |
| 176 |
return False |
| 177 |
else: |
| 178 |
list_backup_jobs_vm_deleted = [] |
| 179 |
list_backups = self.list_backup_schedules() |
| 180 |
for backup_job in list_backups: |
| 181 |
vmids = list(backup_job["vmid"].split(",")) |
| 182 |
if str(vm_id) in vmids: |
| 183 |
if len(vmids) > 1: |
| 184 |
vmids.remove(str(vm_id)) |
| 185 |
new_vmids = ",".join(map(str, vmids)) |
| 186 |
self.update_backup_job_vmid(backup_job["id"], new_vmids) |
| 187 |
list_backup_jobs_vm_deleted.append(backup_job["id"]) |
| 188 |
else: |
| 189 |
self.module.fail_json( |
| 190 |
msg=f"No more than one vmid is assigned to {backup_job['id']}. You just can remove job." |
| 191 |
) |
| 192 |
return len(list_backup_jobs_vm_deleted) > 0 |
| 193 |
|
| 194 |
|
| 195 |
def main(): |
| 196 |
module = create_proxmox_module(module_args(), **module_options()) |
| 197 |
proxmox = ProxmoxSetVMBackupAnsible(module) |
| 198 |
|
| 199 |
result = dict(changed=False, message="") |
| 200 |
|
| 201 |
vm_name = module.params["vm_name"] |
| 202 |
vm_id = module.params["vm_id"] |
| 203 |
backup_id = module.params["backup_id"] |
| 204 |
state = module.params["state"] |
| 205 |
|
| 206 |
if vm_name: |
| 207 |
vm_id = proxmox.get_vmid_from_vmname(vm_name) |
| 208 |
|
| 209 |
if state == "present": |
| 210 |
backup_schedule = proxmox.ensure_backup_present(vm_id, backup_id) |
| 211 |
|
| 212 |
if state == "absent": |
| 213 |
backup_schedule = proxmox.ensure_backup_absent(vm_id, backup_id) |
| 214 |
|
| 215 |
if backup_schedule: |
| 216 |
result["changed"] = True |
| 217 |
result["message"] = "The backup schedule has been changed successfully." |
| 218 |
else: |
| 219 |
result["message"] = "The backup schedule did not change anything." |
| 220 |
|
| 221 |
module.exit_json(**result) |
| 222 |
|
| 223 |
|
| 224 |
if __name__ == "__main__": |
| 225 |
main() |