Update proxmox_backup_schedule.py

This commit is contained in:
raoufnezhad 2025-03-03 08:32:25 +03:30 committed by GitHub
commit 575648f5d8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -13,7 +13,7 @@ DOCUMENTATION = """
--- ---
module: proxmox_backup_schedule module: proxmox_backup_schedule
short_description: Scheduling VM backups and removing it. short_description: Scheduling VM backups and removing them.
version_added: 10.3.0 version_added: 10.3.0
@ -37,12 +37,12 @@ options:
backup_id: backup_id:
description: The backup job ID. description: The backup job ID.
type: str type: str
backup_action: state:
description: description:
- If V(update_vmid), the module will update backup job with new VM ID. - If V(present), the module will update backup job with new VM ID.
- If V(delete_vmid), the module will remove the VM ID from all backup jobs where the VM ID has existed. - If V(absent), the module will remove the VM ID from all backup jobs where the VM ID has existed.
required: true required: true
choices: ["update_vmid", "delete_vmid"] choices: ["present", "absent"]
type: str type: str
extends_documentation_fragment: extends_documentation_fragment:
@ -53,47 +53,47 @@ extends_documentation_fragment:
""" """
EXAMPLES = """ EXAMPLES = """
- name: Scheduling VM Backups based on VM name. - name: Scheduling VM Backups based on VM name
proxmox_backup_schedule: proxmox_backup_schedule:
api_user: 'myUser@pam' api_user: 'myUser@pam'
api_password: '*******' api_password: '*******'
api_host: '192.168.20.20' api_host: '192.168.20.20'
vm_name: 'VM Name' vm_name: 'VM Name'
backup_id: 'backup-b2adffdc-316e' backup_id: 'backup-b2adffdc-316e'
backup_action: 'update_vmid' state: 'present'
- name: Scheduling VM Backups based on VM ID. - name: Scheduling VM Backups based on VM ID
proxmox_backup_schedule: proxmox_backup_schedule:
api_user: 'myUser@pam' api_user: 'myUser@pam'
api_password: '*******' api_password: '*******'
api_host: '192.168.20.20' api_host: '192.168.20.20'
vm_id: 'VM ID' vm_id: 'VM ID'
backup_id: 'backup-b2adffdc-316e' backup_id: 'backup-b2adffdc-316e'
backup_action: 'update_vmid' state: 'present'
- name: Removing backup setting based on VM name. - name: Removing backup setting based on VM name
proxmox_backup_schedule: proxmox_backup_schedule:
api_user: 'myUser@pam' api_user: 'myUser@pam'
api_password: '*******' api_password: '*******'
api_host: '192.168.20.20' api_host: '192.168.20.20'
vm_name: 'VM Name' vm_name: 'VM Name'
backup_action: 'delete_vmid' state: 'absent'
- name: Removing backup setting based on VM ID. - name: Removing backup setting based on VM ID
proxmox_backup_schedule: proxmox_backup_schedule:
api_user: 'myUser@pam' api_user: 'myUser@pam'
api_password: '*******' api_password: '*******'
api_host: '192.168.20.20' api_host: '192.168.20.20'
vm_id: 'VM ID' vm_id: 'VM ID'
backup_action: 'delete_vmid' state: 'absent'
""" """
RETURN = """ RETURN = """
--- ---
backup_schedule: backup_schedule:
description: description:
- If V(update_vmid), the backup_schedule will return True after adding the VM ID to the backup job. - If V(present), the backup_schedule will return True after adding the VM ID to the backup job.
- If V(delete_vmid), the backup_schedule will return a list of backup job IDs where the VM ID has existed after removing it. - If V(absent), the backup_schedule will return a list of backup job IDs where the VM ID has existed after removing it.
returned: always, but can be empty returned: always, but can be empty
type: raw type: raw
sample: sample:
@ -143,7 +143,7 @@ class ProxmoxSetVMBackupAnsible(ProxmoxAnsible):
return (vms[0]['vmid']) return (vms[0]['vmid'])
# add vmid to backup job # add vmid to backup job
def backup_update_vmid(self, vm_id, backup_id): def backup_present(self, vm_id, backup_id):
bk_id_info = self.get_cluster_specific_bkjobid(backup_id) bk_id_info = self.get_cluster_specific_bkjobid(backup_id)
# If bk_id_info is a list, get the first item (assuming there's only one backup job returned) # If bk_id_info is a list, get the first item (assuming there's only one backup job returned)
@ -154,9 +154,26 @@ class ProxmoxSetVMBackupAnsible(ProxmoxAnsible):
bk_id_vmids = bk_id_info['vmid'] + ',' + str(vm_id) bk_id_vmids = bk_id_info['vmid'] + ',' + str(vm_id)
self.set_vmid_backup(backup_id, bk_id_vmids) self.set_vmid_backup(backup_id, bk_id_vmids)
return True return True
else:
return False
# delete vmid from backup job # delete vmid from backup job
def backup_delete_vmid(self, vm_id): def backup_absent(self, vm_id, backup_id):
if backup_id:
bk_id_info = self.get_cluster_specific_bkjobid(backup_id)
if isinstance(bk_id_info, list):
bk_id_info = bk_id_info[0] # Access the first item in the list
vmids = bk_id_info['vmid'].split(',')
if vm_id in vmids:
if len(vmids) > 1:
vmids.remove(vm_id)
new_vmids = ','.join(map(str, vmids))
self.set_vmid_backup(bk_id_info['id'], new_vmids)
return True
else:
self.module.fail_json(msg="No more than one vmid is assigned to %s. You just can remove job." % bk_id_info['id'])
return False
else:
bkID_delvm = [] bkID_delvm = []
backupList = self.get_cluster_bklist() backupList = self.get_cluster_bklist()
for backupItem in backupList: for backupItem in backupList:
@ -169,7 +186,10 @@ class ProxmoxSetVMBackupAnsible(ProxmoxAnsible):
bkID_delvm.append(backupItem['id']) bkID_delvm.append(backupItem['id'])
else: else:
self.module.fail_json(msg="No more than one vmid is assigned to %s. You just can remove job." % backupItem['id']) self.module.fail_json(msg="No more than one vmid is assigned to %s. You just can remove job." % backupItem['id'])
return bkID_delvm if bkID_delvm:
return True
else:
return False
# main function # main function
@ -180,7 +200,7 @@ def main():
vm_name=dict(type='str'), vm_name=dict(type='str'),
vm_id=dict(type='str'), vm_id=dict(type='str'),
backup_id=dict(type='str'), backup_id=dict(type='str'),
backup_action=dict(choices=['update_vmid', 'delete_vmid'], required=True) state=dict(choices=['present', 'absent'], required=True)
) )
args.update(backup_schedule_args) args.update(backup_schedule_args)
@ -205,20 +225,22 @@ def main():
vm_name = module.params['vm_name'] vm_name = module.params['vm_name']
vm_id = module.params['vm_id'] vm_id = module.params['vm_id']
backup_id = module.params['backup_id'] backup_id = module.params['backup_id']
backup_action = module.params['backup_action'] state = module.params['state']
if vm_name: if vm_name:
vm_id = proxmox.vmname_2_vmid(vm_name) vm_id = proxmox.vmname_2_vmid(vm_name)
if backup_action == 'update_vmid': if state == 'present':
result['backup_schedule'] = proxmox.backup_update_vmid(vm_id, backup_id) result['backup_schedule'] = proxmox.backup_present(vm_id, backup_id)
if backup_action == 'delete_vmid': if state == 'absent':
result['backup_schedule'] = proxmox.backup_delete_vmid(vm_id) result['backup_schedule'] = proxmox.backup_absent(vm_id, backup_id)
if result['backup_schedule']: if result['backup_schedule']:
result['changed'] = True result['changed'] = True
result['message'] = 'The backup schedule has been changed successfully' result['message'] = 'The backup schedule has been changed successfully.'
else:
result['message'] = 'The backup schedule did not change anything.'
module.exit_json(**result) module.exit_json(**result)