Debian Codegraph @

ansible_collections/community/proxmox/plugins/modules/proxmox_backup_info.py

raw
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2024 Marzieh Raoufnezhad <raoufnezhad at gmail.com>
4 # Copyright (c) 2024 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_info
12
13 short_description: Retrieve information on Proxmox scheduled backups
14
15 description:
16 - Retrieve information such as backup times, VM name, VM ID, mode, backup type, and backup schedule using the Proxmox Server API.
17
18 author:
19 - "Marzieh Raoufnezhad (@raoufnezhad) <[email protected]>"
20 - "Maryam Mayabi (@mmayabi) <[email protected]>"
21
22 options:
23 vm_name:
24 description:
25 - The name of the Proxmox VM.
26 - If defined, the returned list will contain backup jobs that have been parsed and filtered based on O(vm_name) value.
27 - Mutually exclusive with O(vm_id) and O(backup_jobs).
28 type: str
29 vm_id:
30 description:
31 - The ID of the Proxmox VM.
32 - If defined, the returned list will contain backup jobs that have been parsed and filtered based on O(vm_id) value.
33 - Mutually exclusive with O(vm_name) and O(backup_jobs).
34 type: str
35 backup_jobs:
36 description:
37 - If V(true), the module will return all backup jobs information.
38 - If V(false), the module will parse all backup jobs based on VM IDs and return a list of VMs' backup information.
39 - Mutually exclusive with O(vm_id) and O(vm_name).
40 default: false
41 type: bool
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: Print all backup information by VM ID and VM name
52 community.proxmox.proxmox_backup_info:
53
54 - name: Print Proxmox backup information for a specific VM based on its name
55 community.proxmox.proxmox_backup_info:
56 vm_name: 'mailsrv'
57
58 - name: Print Proxmox backup information for a specific VM based on its VM ID
59 community.proxmox.proxmox_backup_info:
60 vm_id: '150'
61
62 - name: Print Proxmox all backup job information
63 community.proxmox.proxmox_backup_info:
64 backup_jobs: true
65 """
66
67 RETURN = """
68 ---
69 backup_info:
70 description: The return value provides backup job information based on VM ID or VM name, or total backup job information.
71 returned: on success, but can be empty
72 type: list
73 elements: dict
74 contains:
75 bktype:
76 description: The type of the backup.
77 returned: on success
78 type: str
79 sample: vzdump
80 enabled:
81 description: V(1) if backup is enabled else V(0).
82 returned: on success
83 type: int
84 sample: 1
85 id:
86 description: The backup job ID.
87 returned: on success
88 type: str
89 sample: backup-83831498-c631
90 mode:
91 description: The backup job mode such as snapshot.
92 returned: on success
93 type: str
94 sample: snapshot
95 next-run:
96 description: The next backup time.
97 returned: on success
98 type: str
99 sample: "2024-12-28 11:30:00"
100 schedule:
101 description: The backup job schedule.
102 returned: on success
103 type: str
104 sample: "sat 15:00"
105 storage:
106 description: The backup storage location.
107 returned: on success
108 type: str
109 sample: local
110 vm_name:
111 description: The VM name.
112 returned: on success
113 type: str
114 sample: test01
115 vmid:
116 description: The VM ID.
117 returned: on success
118 type: str
119 sample: "100"
120 """
121
122 from datetime import datetime
123
124 from ansible_collections.community.proxmox.plugins.module_utils.proxmox import (
125 ProxmoxAnsible,
126 create_proxmox_module,
127 )
128
129
130 def module_args():
131 return dict(
132 vm_id=dict(type="str"),
133 vm_name=dict(type="str"),
134 backup_jobs=dict(type="bool", default=False),
135 )
136
137
138 def module_options():
139 return dict(mutually_exclusive=[("backup_jobs", "vm_id", "vm_name")])
140
141
142 class ProxmoxBackupInfoAnsible(ProxmoxAnsible):
143 # Get all backup information
144 def get_jobs_list(self):
145 try:
146 backupJobs = self.proxmox_api.cluster.backup.get()
147 except Exception as e:
148 self.module.fail_json(msg=f"Getting backup jobs failed: {e}")
149 return backupJobs
150
151 # Get VM information
152 def get_vms_list(self):
153 try:
154 vms = self.proxmox_api.cluster.resources.get(type="vm")
155 except Exception as e:
156 self.module.fail_json(msg=f"Getting VMs info from cluster failed: {e}")
157 return vms
158
159 # Get all backup information by VM ID and VM name
160 def vms_backup_info(self):
161 backupList = self.get_jobs_list()
162 vmInfo = self.get_vms_list()
163 bkInfo = []
164 for backupItem in backupList:
165 nextrun = datetime.fromtimestamp(backupItem["next-run"])
166 vmids = backupItem["vmid"].split(",")
167 for vmid in vmids:
168 for vm in vmInfo:
169 if vm["vmid"] == int(vmid):
170 vmName = vm["name"]
171 break
172 bkInfoData = {
173 "id": backupItem["id"],
174 "schedule": backupItem["schedule"],
175 "storage": backupItem["storage"],
176 "mode": backupItem["mode"],
177 "next-run": nextrun.strftime("%Y-%m-%d %H:%M:%S"),
178 "enabled": backupItem["enabled"],
179 "bktype": backupItem["type"],
180 "vmid": vmid,
181 "vm_name": vmName,
182 }
183 bkInfo.append(bkInfoData)
184 return bkInfo
185
186 # Get proxmox backup information for a specific VM based on its VM ID or VM name
187 def specific_vmbackup_info(self, vm_name_id):
188 fullBackupInfo = self.vms_backup_info()
189 vmBackupJobs = []
190 for vm in fullBackupInfo:
191 if vm["vm_name"] == vm_name_id or vm["vmid"] == vm_name_id:
192 vmBackupJobs.append(vm)
193 return vmBackupJobs
194
195
196 def main():
197 module = create_proxmox_module(module_args(), **module_options())
198 proxmox = ProxmoxBackupInfoAnsible(module)
199
200 # Define (init) result value
201 result = dict(changed=False)
202
203 vm_id = module.params["vm_id"]
204 vm_name = module.params["vm_name"]
205 backup_jobs = module.params["backup_jobs"]
206
207 # Update result value based on what requested (module args)
208 if backup_jobs:
209 result["backup_info"] = proxmox.get_jobs_list()
210 elif vm_id:
211 result["backup_info"] = proxmox.specific_vmbackup_info(vm_id)
212 elif vm_name:
213 result["backup_info"] = proxmox.specific_vmbackup_info(vm_name)
214 else:
215 result["backup_info"] = proxmox.vms_backup_info()
216
217 # Return result value
218 module.exit_json(**result)
219
220
221 if __name__ == "__main__":
222 main()