Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2020, Jeffrey van Pelt (@Thulium-Drake) <[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_snap_info
10 short_description: Snapshot management of instances in Proxmox VE cluster
11 description:
12 - Allows you to list snapshots from instances in Proxmox VE cluster.
13 - Supports both KVM and LXC, OpenVZ has not been tested, as it is no longer supported on Proxmox VE.
14 attributes:
15 check_mode:
16 support: full
17 diff_mode:
18 support: none
19 options:
20 hostname:
21 description:
22 - The instance name.
23 type: str
24 vmid:
25 description:
26 - The instance ID.
27 - If not set, will be fetched from PromoxAPI based on the hostname.
28 type: str
29 snapname:
30 description:
31 - Name of the snapshot that has to be listed.
32 type: str
33 required: false
34
35 notes:
36 - Requires proxmoxer and requests modules on host. These modules can be installed with pip.
37 author: Jeffrey van Pelt (@Thulium-Drake)
38 extends_documentation_fragment:
39 - community.proxmox.proxmox.actiongroup_proxmox
40 - community.proxmox.proxmox.documentation
41 - community.proxmox.attributes
42 """
43
44 EXAMPLES = r"""
45 - name: List all snapshots for container or VM
46 community.proxmox.proxmox_snap_info:
47 vmid: 100
48
49 - name: List specific snapshot for container or VM
50 community.proxmox.proxmox_snap_info:
51 vmid: 100
52 snapname: my-snapshot
53 """
54
55 RETURN = r"""
56 snapshot:
57 description: Snapshot information when snapname is provided and found.
58 returned: when snapname is provided and the snapshot exists
59 type: dict
60 snapshots:
61 description: List of snapshots when snapname is not provided.
62 returned: when snapname is not provided
63 type: list
64 """
65
66 from ansible.module_utils.common.text.converters import to_native
67
68 from ansible_collections.community.proxmox.plugins.module_utils.proxmox import (
69 ProxmoxAnsible,
70 create_proxmox_module,
71 )
72
73
74 def module_args():
75 return dict(
76 vmid=dict(required=False),
77 hostname=dict(),
78 snapname=dict(type="str", required=False),
79 )
80
81
82 def module_options():
83 return {}
84
85
86 class ProxmoxSnapAnsible(ProxmoxAnsible):
87 def snapshot(self, vm, vmid):
88 return getattr(self.proxmox_api.nodes(vm["node"]), vm["type"])(vmid).snapshot
89
90
91 def main():
92 module = create_proxmox_module(module_args(), **module_options())
93 proxmox = ProxmoxSnapAnsible(module)
94
95 vmid = module.params["vmid"]
96 hostname = module.params["hostname"]
97 snapname = module.params["snapname"]
98
99 # If hostname is set get the VM id from ProxmoxAPI
100 if not vmid and hostname:
101 vmid = proxmox.get_vmid(hostname)
102 elif not vmid:
103 module.exit_json(changed=False, msg="Vmid could not be fetched (vmid or hostname required)")
104
105 vm = proxmox.get_vm(vmid)
106
107 try:
108 snapshots = proxmox.snapshot(vm, vmid).get()
109 if snapname:
110 snapshot = next((s for s in snapshots if s.get("name") == snapname), None)
111
112 if not snapshot:
113 module.exit_json(changed=False, snapshots=[], msg=f"Snapshot '{snapname}' not found")
114
115 module.exit_json(changed=False, snapshot=snapshot)
116
117 # No snapname → return all snapshots
118 module.exit_json(changed=False, snapshots=snapshots)
119
120 except Exception as e:
121 module.fail_json(msg=f"Failed to list snapshots: {to_native(e)}")
122
123
124 if __name__ == "__main__":
125 main()