2020-12-21 14:37:30 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
2022-02-07 06:21:24 +01:00
|
|
|
#
|
2022-08-05 12:28:29 +02:00
|
|
|
# Copyright (c) 2020, Jeffrey van Pelt (@Thulium-Drake) <jeff@vanpelt.one>
|
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-12-21 14:37:30 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = r'''
|
|
|
|
---
|
|
|
|
module: proxmox_snap
|
|
|
|
short_description: Snapshot management of instances in Proxmox VE cluster
|
|
|
|
version_added: 2.0.0
|
|
|
|
description:
|
2022-04-25 06:39:15 +02:00
|
|
|
- Allows you to create/delete/restore snapshots from instances in Proxmox VE cluster.
|
2020-12-21 14:37:30 +01:00
|
|
|
- Supports both KVM and LXC, OpenVZ has not been tested, as it is no longer supported on Proxmox VE.
|
|
|
|
options:
|
|
|
|
hostname:
|
|
|
|
description:
|
|
|
|
- The instance name.
|
|
|
|
type: str
|
|
|
|
vmid:
|
|
|
|
description:
|
|
|
|
- The instance id.
|
|
|
|
- If not set, will be fetched from PromoxAPI based on the hostname.
|
|
|
|
type: str
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Indicate desired state of the instance snapshot.
|
2022-04-25 06:39:15 +02:00
|
|
|
- The C(rollback) value was added in community.general 4.8.0.
|
|
|
|
choices: ['present', 'absent', 'rollback']
|
2020-12-21 14:37:30 +01:00
|
|
|
default: present
|
|
|
|
type: str
|
|
|
|
force:
|
|
|
|
description:
|
|
|
|
- For removal from config file, even if removing disk snapshot fails.
|
2022-08-24 20:16:25 +02:00
|
|
|
default: false
|
2020-12-21 14:37:30 +01:00
|
|
|
type: bool
|
|
|
|
vmstate:
|
|
|
|
description:
|
|
|
|
- Snapshot includes RAM.
|
2022-08-24 20:16:25 +02:00
|
|
|
default: false
|
2020-12-21 14:37:30 +01:00
|
|
|
type: bool
|
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- Specify the description for the snapshot. Only used on the configuration web interface.
|
|
|
|
- This is saved as a comment inside the configuration file.
|
|
|
|
type: str
|
|
|
|
timeout:
|
|
|
|
description:
|
|
|
|
- Timeout for operations.
|
|
|
|
default: 30
|
|
|
|
type: int
|
|
|
|
snapname:
|
|
|
|
description:
|
2022-04-25 06:39:15 +02:00
|
|
|
- Name of the snapshot that has to be created/deleted/restored.
|
2020-12-21 14:37:30 +01:00
|
|
|
default: 'ansible_snap'
|
|
|
|
type: str
|
|
|
|
|
|
|
|
notes:
|
|
|
|
- Requires proxmoxer and requests modules on host. These modules can be installed with pip.
|
|
|
|
- Supports C(check_mode).
|
|
|
|
requirements: [ "proxmoxer", "python >= 2.7", "requests" ]
|
|
|
|
author: Jeffrey van Pelt (@Thulium-Drake)
|
2022-02-07 06:21:24 +01:00
|
|
|
extends_documentation_fragment:
|
|
|
|
- community.general.proxmox.documentation
|
2020-12-21 14:37:30 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = r'''
|
|
|
|
- name: Create new container snapshot
|
|
|
|
community.general.proxmox_snap:
|
|
|
|
api_user: root@pam
|
|
|
|
api_password: 1q2w3e
|
|
|
|
api_host: node1
|
|
|
|
vmid: 100
|
|
|
|
state: present
|
|
|
|
snapname: pre-updates
|
|
|
|
|
|
|
|
- name: Remove container snapshot
|
|
|
|
community.general.proxmox_snap:
|
|
|
|
api_user: root@pam
|
|
|
|
api_password: 1q2w3e
|
|
|
|
api_host: node1
|
|
|
|
vmid: 100
|
|
|
|
state: absent
|
|
|
|
snapname: pre-updates
|
2022-04-25 06:39:15 +02:00
|
|
|
|
|
|
|
- name: Rollback container snapshot
|
|
|
|
community.general.proxmox_snap:
|
|
|
|
api_user: root@pam
|
|
|
|
api_password: 1q2w3e
|
|
|
|
api_host: node1
|
|
|
|
vmid: 100
|
|
|
|
state: rollback
|
|
|
|
snapname: pre-updates
|
2020-12-21 14:37:30 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = r'''#'''
|
|
|
|
|
|
|
|
import time
|
|
|
|
import traceback
|
|
|
|
|
2022-09-09 22:23:48 +02:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2021-06-26 23:59:11 +02:00
|
|
|
from ansible.module_utils.common.text.converters import to_native
|
2022-09-09 22:23:48 +02:00
|
|
|
from ansible_collections.community.general.plugins.module_utils.proxmox import (proxmox_auth_argument_spec, ProxmoxAnsible)
|
2020-12-21 14:37:30 +01:00
|
|
|
|
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
class ProxmoxSnapAnsible(ProxmoxAnsible):
|
|
|
|
def snapshot(self, vm, vmid):
|
|
|
|
return getattr(self.proxmox_api.nodes(vm['node']), vm['type'])(vmid).snapshot
|
2020-12-21 14:37:30 +01:00
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
def snapshot_create(self, vm, vmid, timeout, snapname, description, vmstate):
|
|
|
|
if self.module.check_mode:
|
2020-12-21 14:37:30 +01:00
|
|
|
return True
|
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
if vm['type'] == 'lxc':
|
|
|
|
taskid = self.snapshot(vm, vmid).post(snapname=snapname, description=description)
|
|
|
|
else:
|
|
|
|
taskid = self.snapshot(vm, vmid).post(snapname=snapname, description=description, vmstate=int(vmstate))
|
|
|
|
while timeout:
|
2022-04-25 06:39:15 +02:00
|
|
|
status_data = self.proxmox_api.nodes(vm['node']).tasks(taskid).status.get()
|
|
|
|
if status_data['status'] == 'stopped' and status_data['exitstatus'] == 'OK':
|
2022-02-07 06:21:24 +01:00
|
|
|
return True
|
|
|
|
if timeout == 0:
|
|
|
|
self.module.fail_json(msg='Reached timeout while waiting for creating VM snapshot. Last line in task before timeout: %s' %
|
|
|
|
self.proxmox_api.nodes(vm['node']).tasks(taskid).log.get()[:1])
|
|
|
|
|
|
|
|
time.sleep(1)
|
2022-04-25 06:39:15 +02:00
|
|
|
timeout -= 1
|
2022-02-07 06:21:24 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
def snapshot_remove(self, vm, vmid, timeout, snapname, force):
|
|
|
|
if self.module.check_mode:
|
2020-12-21 14:37:30 +01:00
|
|
|
return True
|
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
taskid = self.snapshot(vm, vmid).delete(snapname, force=int(force))
|
|
|
|
while timeout:
|
2022-04-25 06:39:15 +02:00
|
|
|
status_data = self.proxmox_api.nodes(vm['node']).tasks(taskid).status.get()
|
|
|
|
if status_data['status'] == 'stopped' and status_data['exitstatus'] == 'OK':
|
2022-02-07 06:21:24 +01:00
|
|
|
return True
|
|
|
|
if timeout == 0:
|
|
|
|
self.module.fail_json(msg='Reached timeout while waiting for removing VM snapshot. Last line in task before timeout: %s' %
|
|
|
|
self.proxmox_api.nodes(vm['node']).tasks(taskid).log.get()[:1])
|
2020-12-21 14:37:30 +01:00
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
time.sleep(1)
|
2022-04-25 06:39:15 +02:00
|
|
|
timeout -= 1
|
|
|
|
return False
|
|
|
|
|
|
|
|
def snapshot_rollback(self, vm, vmid, timeout, snapname):
|
|
|
|
if self.module.check_mode:
|
|
|
|
return True
|
|
|
|
|
|
|
|
taskid = self.snapshot(vm, vmid)(snapname).post("rollback")
|
|
|
|
while timeout:
|
|
|
|
status_data = self.proxmox_api.nodes(vm['node']).tasks(taskid).status.get()
|
|
|
|
if status_data['status'] == 'stopped' and status_data['exitstatus'] == 'OK':
|
|
|
|
return True
|
|
|
|
if timeout == 0:
|
|
|
|
self.module.fail_json(msg='Reached timeout while waiting for rolling back VM snapshot. Last line in task before timeout: %s' %
|
|
|
|
self.proxmox_api.nodes(vm['node']).tasks(taskid).log.get()[:1])
|
|
|
|
|
|
|
|
time.sleep(1)
|
|
|
|
timeout -= 1
|
2022-02-07 06:21:24 +01:00
|
|
|
return False
|
2020-12-21 14:37:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2022-02-07 06:21:24 +01:00
|
|
|
module_args = proxmox_auth_argument_spec()
|
|
|
|
snap_args = dict(
|
|
|
|
vmid=dict(required=False),
|
|
|
|
hostname=dict(),
|
|
|
|
timeout=dict(type='int', default=30),
|
2022-04-25 06:39:15 +02:00
|
|
|
state=dict(default='present', choices=['present', 'absent', 'rollback']),
|
2022-02-07 06:21:24 +01:00
|
|
|
description=dict(type='str'),
|
|
|
|
snapname=dict(type='str', default='ansible_snap'),
|
2022-08-24 20:16:25 +02:00
|
|
|
force=dict(type='bool', default=False),
|
|
|
|
vmstate=dict(type='bool', default=False),
|
2022-02-07 06:21:24 +01:00
|
|
|
)
|
|
|
|
module_args.update(snap_args)
|
|
|
|
|
2020-12-21 14:37:30 +01:00
|
|
|
module = AnsibleModule(
|
2022-02-07 06:21:24 +01:00
|
|
|
argument_spec=module_args,
|
2020-12-21 14:37:30 +01:00
|
|
|
supports_check_mode=True
|
|
|
|
)
|
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
proxmox = ProxmoxSnapAnsible(module)
|
2020-12-21 14:37:30 +01:00
|
|
|
|
|
|
|
state = module.params['state']
|
|
|
|
vmid = module.params['vmid']
|
|
|
|
hostname = module.params['hostname']
|
|
|
|
description = module.params['description']
|
|
|
|
snapname = module.params['snapname']
|
|
|
|
timeout = module.params['timeout']
|
|
|
|
force = module.params['force']
|
|
|
|
vmstate = module.params['vmstate']
|
|
|
|
|
|
|
|
# If hostname is set get the VM id from ProxmoxAPI
|
|
|
|
if not vmid and hostname:
|
2022-04-26 11:45:15 +02:00
|
|
|
vmid = proxmox.get_vmid(hostname)
|
2020-12-21 14:37:30 +01:00
|
|
|
elif not vmid:
|
|
|
|
module.exit_json(changed=False, msg="Vmid could not be fetched for the following action: %s" % state)
|
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
vm = proxmox.get_vm(vmid)
|
2020-12-21 14:37:30 +01:00
|
|
|
|
|
|
|
if state == 'present':
|
|
|
|
try:
|
2022-02-07 06:21:24 +01:00
|
|
|
for i in proxmox.snapshot(vm, vmid).get():
|
2020-12-21 14:37:30 +01:00
|
|
|
if i['name'] == snapname:
|
|
|
|
module.exit_json(changed=False, msg="Snapshot %s is already present" % snapname)
|
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
if proxmox.snapshot_create(vm, vmid, timeout, snapname, description, vmstate):
|
2020-12-21 14:37:30 +01:00
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=False, msg="Snapshot %s would be created" % snapname)
|
|
|
|
else:
|
|
|
|
module.exit_json(changed=True, msg="Snapshot %s created" % snapname)
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg="Creating snapshot %s of VM %s failed with exception: %s" % (snapname, vmid, to_native(e)))
|
|
|
|
|
|
|
|
elif state == 'absent':
|
|
|
|
try:
|
|
|
|
snap_exist = False
|
|
|
|
|
2022-02-07 06:21:24 +01:00
|
|
|
for i in proxmox.snapshot(vm, vmid).get():
|
2020-12-21 14:37:30 +01:00
|
|
|
if i['name'] == snapname:
|
|
|
|
snap_exist = True
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not snap_exist:
|
|
|
|
module.exit_json(changed=False, msg="Snapshot %s does not exist" % snapname)
|
|
|
|
else:
|
2022-02-07 06:21:24 +01:00
|
|
|
if proxmox.snapshot_remove(vm, vmid, timeout, snapname, force):
|
2020-12-21 14:37:30 +01:00
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=False, msg="Snapshot %s would be removed" % snapname)
|
|
|
|
else:
|
|
|
|
module.exit_json(changed=True, msg="Snapshot %s removed" % snapname)
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg="Removing snapshot %s of VM %s failed with exception: %s" % (snapname, vmid, to_native(e)))
|
2022-04-25 06:39:15 +02:00
|
|
|
elif state == 'rollback':
|
|
|
|
try:
|
|
|
|
snap_exist = False
|
|
|
|
|
|
|
|
for i in proxmox.snapshot(vm, vmid).get():
|
|
|
|
if i['name'] == snapname:
|
|
|
|
snap_exist = True
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not snap_exist:
|
|
|
|
module.exit_json(changed=False, msg="Snapshot %s does not exist" % snapname)
|
|
|
|
if proxmox.snapshot_rollback(vm, vmid, timeout, snapname):
|
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=True, msg="Snapshot %s would be rolled back" % snapname)
|
|
|
|
else:
|
|
|
|
module.exit_json(changed=True, msg="Snapshot %s rolled back" % snapname)
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg="Rollback of snapshot %s of VM %s failed with exception: %s" % (snapname, vmid, to_native(e)))
|
2020-12-21 14:37:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|