2017-02-06 16:52:17 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
2018-04-06 01:23:18 +00:00
|
|
|
# Copyright: (c) 2018, Ansible Project
|
2017-02-06 16:52:17 +00:00
|
|
|
# This module is also sponsored by E.T.A.I. (www.etai.fr)
|
2017-07-29 15:05:38 +00:00
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2017-08-16 03:16:38 +00:00
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
2017-03-14 16:07:22 +00:00
|
|
|
'status': ['preview'],
|
|
|
|
'supported_by': 'community'}
|
|
|
|
|
2017-02-06 16:52:17 +00:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: vmware_guest_snapshot
|
2017-12-01 15:51:45 +00:00
|
|
|
short_description: Manages virtual machines snapshots in vCenter
|
2017-02-06 16:52:17 +00:00
|
|
|
description:
|
2018-04-06 01:23:18 +00:00
|
|
|
- This module can be used to create, delete and update snapshot(s) of the given virtual machine.
|
|
|
|
- All parameters and VMware object names are case sensitive.
|
2017-02-06 16:52:17 +00:00
|
|
|
version_added: 2.3
|
|
|
|
author:
|
|
|
|
- Loic Blot (@nerzhul) <loic.blot@unix-experience.fr>
|
|
|
|
notes:
|
2018-04-06 01:23:18 +00:00
|
|
|
- Tested on vSphere 5.5, 6.0 and 6.5
|
2017-02-06 16:52:17 +00:00
|
|
|
requirements:
|
|
|
|
- "python >= 2.6"
|
|
|
|
- PyVmomi
|
|
|
|
options:
|
|
|
|
state:
|
2018-04-06 01:23:18 +00:00
|
|
|
description:
|
|
|
|
- Manage snapshot(s) attached to a specific virtual machine.
|
|
|
|
- If set to C(present) and snapshot absent, then will create a new snapshot with the given name.
|
|
|
|
- If set to C(present) and snapshot present, then no changes are made.
|
|
|
|
- If set to C(absent) and snapshot present, then snapshot with the given name is removed.
|
|
|
|
- If set to C(absent) and snapshot absent, then no changes are made.
|
|
|
|
- If set to C(revert) and snapshot present, then virtual machine state is reverted to the given snapshot.
|
|
|
|
- If set to C(revert) and snapshot absent, then no changes are made.
|
|
|
|
- If set to C(remove_all) and snapshot(s) present, then all snapshot(s) will be removed.
|
|
|
|
- If set to C(remove_all) and snapshot(s) absent, then no changes are made.
|
|
|
|
required: True
|
|
|
|
choices: ['present', 'absent', 'revert', 'remove_all']
|
|
|
|
default: 'present'
|
2017-02-06 16:52:17 +00:00
|
|
|
name:
|
2018-04-06 01:23:18 +00:00
|
|
|
description:
|
|
|
|
- Name of the virtual machine to work with.
|
|
|
|
- This is required parameter, if C(uuid) is not supplied.
|
2017-02-06 16:52:17 +00:00
|
|
|
name_match:
|
2018-04-06 01:23:18 +00:00
|
|
|
description:
|
|
|
|
- If multiple VMs matching the name, use the first or last found.
|
|
|
|
default: 'first'
|
|
|
|
choices: ['first', 'last']
|
2017-02-06 16:52:17 +00:00
|
|
|
uuid:
|
2018-04-06 01:23:18 +00:00
|
|
|
description:
|
2019-02-25 08:37:07 +00:00
|
|
|
- UUID of the instance to manage if known, this is VMware's BIOS UUID by default.
|
|
|
|
- This is required if C(name) parameter is not supplied.
|
|
|
|
use_instance_uuid:
|
|
|
|
description:
|
|
|
|
- Whether to use the VMWare instance UUID rather than the BIOS UUID.
|
|
|
|
default: no
|
|
|
|
type: bool
|
|
|
|
version_added: '2.8'
|
2017-02-06 16:52:17 +00:00
|
|
|
folder:
|
2018-04-06 01:23:18 +00:00
|
|
|
description:
|
|
|
|
- Destination folder, absolute or relative path to find an existing guest.
|
|
|
|
- This is required parameter, if C(name) is supplied.
|
|
|
|
- The folder should include the datacenter. ESX's datacenter is ha-datacenter.
|
|
|
|
- 'Examples:'
|
|
|
|
- ' folder: /ha-datacenter/vm'
|
|
|
|
- ' folder: ha-datacenter/vm'
|
|
|
|
- ' folder: /datacenter1/vm'
|
|
|
|
- ' folder: datacenter1/vm'
|
|
|
|
- ' folder: /datacenter1/vm/folder1'
|
|
|
|
- ' folder: datacenter1/vm/folder1'
|
|
|
|
- ' folder: /folder1/datacenter1/vm'
|
|
|
|
- ' folder: folder1/datacenter1/vm'
|
|
|
|
- ' folder: /folder1/datacenter1/vm/folder2'
|
|
|
|
- ' folder: vm/folder2'
|
|
|
|
- ' folder: folder2'
|
2017-02-06 16:52:17 +00:00
|
|
|
datacenter:
|
2018-04-06 01:23:18 +00:00
|
|
|
description:
|
|
|
|
- Destination datacenter for the deploy operation.
|
|
|
|
required: True
|
2017-02-06 16:52:17 +00:00
|
|
|
snapshot_name:
|
2018-04-06 01:23:18 +00:00
|
|
|
description:
|
|
|
|
- Sets the snapshot name to manage.
|
|
|
|
- This param is required only if state is not C(remove_all)
|
2017-02-06 16:52:17 +00:00
|
|
|
description:
|
2018-04-06 01:23:18 +00:00
|
|
|
description:
|
|
|
|
- Define an arbitrary description to attach to snapshot.
|
|
|
|
default: ''
|
2017-06-30 16:34:37 +00:00
|
|
|
quiesce:
|
2018-04-06 01:23:18 +00:00
|
|
|
description:
|
|
|
|
- If set to C(true) and virtual machine is powered on, it will quiesce the file system in virtual machine.
|
|
|
|
- Note that VMWare Tools are required for this flag.
|
|
|
|
- If virtual machine is powered off or VMware Tools are not available, then this flag is set to C(false).
|
|
|
|
- If virtual machine does not provide capability to take quiesce snapshot, then this flag is set to C(false).
|
|
|
|
required: False
|
|
|
|
version_added: "2.4"
|
|
|
|
type: bool
|
|
|
|
default: False
|
2017-06-30 16:34:37 +00:00
|
|
|
memory_dump:
|
2018-04-06 01:23:18 +00:00
|
|
|
description:
|
|
|
|
- If set to C(true), memory dump of virtual machine is also included in snapshot.
|
|
|
|
- Note that memory snapshots take time and resources, this will take longer time to create.
|
|
|
|
- If virtual machine does not provide capability to take memory snapshot, then this flag is set to C(false).
|
|
|
|
required: False
|
|
|
|
version_added: "2.4"
|
|
|
|
type: bool
|
|
|
|
default: False
|
2017-07-14 14:00:37 +00:00
|
|
|
remove_children:
|
2018-04-06 01:23:18 +00:00
|
|
|
description:
|
|
|
|
- If set to C(true) and state is set to C(absent), then entire snapshot subtree is set for removal.
|
|
|
|
required: False
|
|
|
|
version_added: "2.4"
|
|
|
|
type: bool
|
|
|
|
default: False
|
2017-10-30 16:20:03 +00:00
|
|
|
new_snapshot_name:
|
2018-04-06 01:23:18 +00:00
|
|
|
description:
|
|
|
|
- Value to rename the existing snapshot to.
|
|
|
|
version_added: "2.5"
|
2017-10-30 16:20:03 +00:00
|
|
|
new_description:
|
2018-04-06 01:23:18 +00:00
|
|
|
description:
|
|
|
|
- Value to change the description of an existing snapshot to.
|
|
|
|
version_added: "2.5"
|
2017-02-06 16:52:17 +00:00
|
|
|
extends_documentation_fragment: vmware.documentation
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2018-04-06 01:23:18 +00:00
|
|
|
- name: Create a snapshot
|
2017-02-06 16:52:17 +00:00
|
|
|
vmware_guest_snapshot:
|
2018-08-08 11:44:27 +00:00
|
|
|
hostname: "{{ vcenter_hostname }}"
|
|
|
|
username: "{{ vcenter_username }}"
|
|
|
|
password: "{{ vcenter_password }}"
|
|
|
|
datacenter: "{{ datacenter_name }}"
|
2019-03-26 04:40:12 +00:00
|
|
|
folder: "/{{ datacenter_name }}/vm/"
|
2018-08-08 11:44:27 +00:00
|
|
|
name: "{{ guest_name }}"
|
2017-02-06 16:52:17 +00:00
|
|
|
state: present
|
|
|
|
snapshot_name: snap1
|
|
|
|
description: snap1_description
|
2017-04-18 12:53:55 +00:00
|
|
|
delegate_to: localhost
|
2017-02-06 16:52:17 +00:00
|
|
|
|
|
|
|
- name: Remove a snapshot
|
|
|
|
vmware_guest_snapshot:
|
2018-08-08 11:44:27 +00:00
|
|
|
hostname: "{{ vcenter_hostname }}"
|
|
|
|
username: "{{ vcenter_username }}"
|
|
|
|
password: "{{ vcenter_password }}"
|
|
|
|
datacenter: "{{ datacenter_name }}"
|
2019-03-26 04:40:12 +00:00
|
|
|
folder: "/{{ datacenter_name }}/vm/"
|
2018-08-08 11:44:27 +00:00
|
|
|
name: "{{ guest_name }}"
|
2018-04-06 01:23:18 +00:00
|
|
|
state: absent
|
2017-02-06 16:52:17 +00:00
|
|
|
snapshot_name: snap1
|
2017-04-18 12:53:55 +00:00
|
|
|
delegate_to: localhost
|
2017-02-06 16:52:17 +00:00
|
|
|
|
|
|
|
- name: Revert to a snapshot
|
|
|
|
vmware_guest_snapshot:
|
2018-08-08 11:44:27 +00:00
|
|
|
hostname: "{{ vcenter_hostname }}"
|
|
|
|
username: "{{ vcenter_username }}"
|
|
|
|
password: "{{ vcenter_password }}"
|
|
|
|
datacenter: "{{ datacenter_name }}"
|
2019-03-26 04:40:12 +00:00
|
|
|
folder: "/{{ datacenter_name }}/vm/"
|
2018-08-08 11:44:27 +00:00
|
|
|
name: "{{ guest_name }}"
|
2017-02-06 16:52:17 +00:00
|
|
|
state: revert
|
|
|
|
snapshot_name: snap1
|
2017-04-18 12:53:55 +00:00
|
|
|
delegate_to: localhost
|
2017-02-06 16:52:17 +00:00
|
|
|
|
|
|
|
- name: Remove all snapshots of a VM
|
|
|
|
vmware_guest_snapshot:
|
2018-08-08 11:44:27 +00:00
|
|
|
hostname: "{{ vcenter_hostname }}"
|
|
|
|
username: "{{ vcenter_username }}"
|
|
|
|
password: "{{ vcenter_password }}"
|
|
|
|
datacenter: "{{ datacenter_name }}"
|
2019-03-26 04:40:12 +00:00
|
|
|
folder: "/{{ datacenter_name }}/vm/"
|
2018-08-08 11:44:27 +00:00
|
|
|
name: "{{ guest_name }}"
|
2017-02-06 16:52:17 +00:00
|
|
|
state: remove_all
|
2017-04-18 12:53:55 +00:00
|
|
|
delegate_to: localhost
|
2017-06-30 16:34:37 +00:00
|
|
|
|
|
|
|
- name: Take snapshot of a VM using quiesce and memory flag on
|
|
|
|
vmware_guest_snapshot:
|
2018-08-08 11:44:27 +00:00
|
|
|
hostname: "{{ vcenter_hostname }}"
|
|
|
|
username: "{{ vcenter_username }}"
|
|
|
|
password: "{{ vcenter_password }}"
|
|
|
|
datacenter: "{{ datacenter_name }}"
|
2019-03-26 04:40:12 +00:00
|
|
|
folder: "/{{ datacenter_name }}/vm/"
|
2018-08-08 11:44:27 +00:00
|
|
|
name: "{{ guest_name }}"
|
2017-06-30 16:34:37 +00:00
|
|
|
state: present
|
|
|
|
snapshot_name: dummy_vm_snap_0001
|
2018-08-08 11:44:27 +00:00
|
|
|
quiesce: yes
|
|
|
|
memory_dump: yes
|
2017-06-30 16:34:37 +00:00
|
|
|
delegate_to: localhost
|
2017-07-14 14:00:37 +00:00
|
|
|
|
|
|
|
- name: Remove a snapshot and snapshot subtree
|
|
|
|
vmware_guest_snapshot:
|
2018-08-08 11:44:27 +00:00
|
|
|
hostname: "{{ vcenter_hostname }}"
|
|
|
|
username: "{{ vcenter_username }}"
|
|
|
|
password: "{{ vcenter_password }}"
|
|
|
|
datacenter: "{{ datacenter_name }}"
|
2019-03-26 04:40:12 +00:00
|
|
|
folder: "/{{ datacenter_name }}/vm/"
|
2018-08-08 11:44:27 +00:00
|
|
|
name: "{{ guest_name }}"
|
2018-04-06 01:23:18 +00:00
|
|
|
state: absent
|
2018-08-08 11:44:27 +00:00
|
|
|
remove_children: yes
|
2017-07-14 14:00:37 +00:00
|
|
|
snapshot_name: snap1
|
|
|
|
delegate_to: localhost
|
2017-10-30 16:20:03 +00:00
|
|
|
|
|
|
|
- name: Rename a snapshot
|
|
|
|
vmware_guest_snapshot:
|
2018-08-08 11:44:27 +00:00
|
|
|
hostname: "{{ vcenter_hostname }}"
|
|
|
|
username: "{{ vcenter_username }}"
|
|
|
|
password: "{{ vcenter_password }}"
|
|
|
|
datacenter: "{{ datacenter_name }}"
|
2019-03-26 04:40:12 +00:00
|
|
|
folder: "/{{ datacenter_name }}/vm/"
|
2018-08-08 11:44:27 +00:00
|
|
|
name: "{{ guest_name }}"
|
2017-10-30 16:20:03 +00:00
|
|
|
state: present
|
|
|
|
snapshot_name: current_snap_name
|
|
|
|
new_snapshot_name: im_renamed
|
2018-08-08 11:44:27 +00:00
|
|
|
new_description: "{{ new_snapshot_description }}"
|
2017-10-30 16:20:03 +00:00
|
|
|
delegate_to: localhost
|
2017-02-06 16:52:17 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = """
|
|
|
|
instance:
|
2018-04-06 01:23:18 +00:00
|
|
|
description: metadata about the new virtual machine snapshot
|
2017-02-06 16:52:17 +00:00
|
|
|
returned: always
|
|
|
|
type: dict
|
|
|
|
sample: None
|
|
|
|
"""
|
|
|
|
|
2017-07-21 16:12:43 +00:00
|
|
|
import time
|
2017-02-06 16:52:17 +00:00
|
|
|
try:
|
|
|
|
from pyVmomi import vim
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
2017-07-29 15:05:38 +00:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from ansible.module_utils._text import to_native
|
2017-12-01 15:51:45 +00:00
|
|
|
from ansible.module_utils.vmware import PyVmomi, list_snapshots, vmware_argument_spec
|
2017-07-29 15:05:38 +00:00
|
|
|
|
2017-02-06 16:52:17 +00:00
|
|
|
|
2017-12-01 15:51:45 +00:00
|
|
|
class PyVmomiHelper(PyVmomi):
|
2017-02-06 16:52:17 +00:00
|
|
|
def __init__(self, module):
|
2017-12-01 15:51:45 +00:00
|
|
|
super(PyVmomiHelper, self).__init__(module)
|
2017-02-06 16:52:17 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def wait_for_task(task):
|
|
|
|
# https://www.vmware.com/support/developer/vc-sdk/visdk25pubs/ReferenceGuide/vim.Task.html
|
|
|
|
# https://www.vmware.com/support/developer/vc-sdk/visdk25pubs/ReferenceGuide/vim.TaskInfo.html
|
|
|
|
# https://github.com/virtdevninja/pyvmomi-community-samples/blob/master/samples/tools/tasks.py
|
|
|
|
while task.info.state not in ['success', 'error']:
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
def get_snapshots_by_name_recursively(self, snapshots, snapname):
|
|
|
|
snap_obj = []
|
|
|
|
for snapshot in snapshots:
|
|
|
|
if snapshot.name == snapname:
|
|
|
|
snap_obj.append(snapshot)
|
|
|
|
else:
|
|
|
|
snap_obj = snap_obj + self.get_snapshots_by_name_recursively(snapshot.childSnapshotList, snapname)
|
|
|
|
return snap_obj
|
|
|
|
|
|
|
|
def snapshot_vm(self, vm):
|
2017-06-30 16:34:37 +00:00
|
|
|
memory_dump = False
|
2017-02-06 16:52:17 +00:00
|
|
|
quiesce = False
|
2017-12-15 03:04:39 +00:00
|
|
|
# Check if there is a latest snapshot already present as specified by user
|
2018-02-10 15:41:11 +00:00
|
|
|
if vm.snapshot is not None:
|
|
|
|
snap_obj = self.get_snapshots_by_name_recursively(vm.snapshot.rootSnapshotList,
|
|
|
|
self.module.params["snapshot_name"])
|
|
|
|
if snap_obj:
|
|
|
|
# Snapshot already exists, do not anything.
|
|
|
|
self.module.exit_json(changed=False,
|
|
|
|
msg="Snapshot named [%(snapshot_name)s] already exists and is current." % self.module.params)
|
2017-12-15 03:04:39 +00:00
|
|
|
# Check if Virtual Machine provides capabilities for Quiesce and Memory Snapshots
|
2017-06-30 16:34:37 +00:00
|
|
|
if vm.capability.quiescedSnapshotsSupported:
|
|
|
|
quiesce = self.module.params['quiesce']
|
|
|
|
if vm.capability.memorySnapshotsSupported:
|
|
|
|
memory_dump = self.module.params['memory_dump']
|
|
|
|
|
2017-07-03 17:39:29 +00:00
|
|
|
task = None
|
|
|
|
try:
|
|
|
|
task = vm.CreateSnapshot(self.module.params["snapshot_name"],
|
|
|
|
self.module.params["description"],
|
|
|
|
memory_dump,
|
|
|
|
quiesce)
|
|
|
|
except vim.fault.RestrictedVersion as exc:
|
2018-04-06 01:23:18 +00:00
|
|
|
self.module.fail_json(msg="Failed to take snapshot due to VMware Licence"
|
|
|
|
" restriction : %s" % to_native(exc.msg))
|
2017-07-03 17:39:29 +00:00
|
|
|
except Exception as exc:
|
2018-04-06 01:23:18 +00:00
|
|
|
self.module.fail_json(msg="Failed to create snapshot of virtual machine"
|
|
|
|
" %s due to %s" % (self.module.params['name'], to_native(exc)))
|
2017-07-03 17:39:29 +00:00
|
|
|
return task
|
2017-02-06 16:52:17 +00:00
|
|
|
|
2017-10-30 16:20:03 +00:00
|
|
|
def rename_snapshot(self, vm):
|
|
|
|
if vm.snapshot is None:
|
2018-05-07 07:11:49 +00:00
|
|
|
self.module.fail_json(msg="virtual machine - %s doesn't have any"
|
2018-04-06 01:23:18 +00:00
|
|
|
" snapshots" % (self.module.params.get('uuid') or self.module.params.get('name')))
|
2017-10-30 16:20:03 +00:00
|
|
|
|
|
|
|
snap_obj = self.get_snapshots_by_name_recursively(vm.snapshot.rootSnapshotList,
|
|
|
|
self.module.params["snapshot_name"])
|
|
|
|
task = None
|
|
|
|
if len(snap_obj) == 1:
|
|
|
|
snap_obj = snap_obj[0].snapshot
|
|
|
|
if self.module.params["new_snapshot_name"] and self.module.params["new_description"]:
|
|
|
|
task = snap_obj.RenameSnapshot(name=self.module.params["new_snapshot_name"],
|
|
|
|
description=self.module.params["new_description"])
|
|
|
|
elif self.module.params["new_snapshot_name"]:
|
|
|
|
task = snap_obj.RenameSnapshot(name=self.module.params["new_snapshot_name"])
|
|
|
|
else:
|
|
|
|
task = snap_obj.RenameSnapshot(description=self.module.params["new_description"])
|
|
|
|
else:
|
|
|
|
self.module.exit_json(
|
|
|
|
msg="Couldn't find any snapshots with specified name: %s on VM: %s" %
|
|
|
|
(self.module.params["snapshot_name"],
|
|
|
|
self.module.params.get('uuid') or self.module.params.get('name')))
|
|
|
|
return task
|
|
|
|
|
2017-02-06 16:52:17 +00:00
|
|
|
def remove_or_revert_snapshot(self, vm):
|
|
|
|
if vm.snapshot is None:
|
2018-05-07 07:11:49 +00:00
|
|
|
vm_name = (self.module.params.get('uuid') or self.module.params.get('name'))
|
|
|
|
if self.module.params.get('state') == 'revert':
|
|
|
|
self.module.fail_json(msg="virtual machine - %s does not"
|
|
|
|
" have any snapshots to revert to." % vm_name)
|
2018-04-06 01:23:18 +00:00
|
|
|
self.module.exit_json(msg="virtual machine - %s doesn't have any"
|
2018-05-07 07:11:49 +00:00
|
|
|
" snapshots to remove." % vm_name)
|
2017-02-06 16:52:17 +00:00
|
|
|
|
|
|
|
snap_obj = self.get_snapshots_by_name_recursively(vm.snapshot.rootSnapshotList,
|
|
|
|
self.module.params["snapshot_name"])
|
|
|
|
task = None
|
|
|
|
if len(snap_obj) == 1:
|
|
|
|
snap_obj = snap_obj[0].snapshot
|
|
|
|
if self.module.params["state"] == "absent":
|
2017-07-14 14:00:37 +00:00
|
|
|
# Remove subtree depending upon the user input
|
|
|
|
remove_children = self.module.params.get('remove_children', False)
|
|
|
|
task = snap_obj.RemoveSnapshot_Task(remove_children)
|
2017-02-06 16:52:17 +00:00
|
|
|
elif self.module.params["state"] == "revert":
|
|
|
|
task = snap_obj.RevertToSnapshot_Task()
|
|
|
|
else:
|
2018-04-06 01:23:18 +00:00
|
|
|
self.module.exit_json(msg="Couldn't find any snapshots with"
|
|
|
|
" specified name: %s on VM: %s" % (self.module.params["snapshot_name"],
|
|
|
|
self.module.params.get('uuid') or self.module.params.get('name')))
|
2017-02-06 16:52:17 +00:00
|
|
|
|
|
|
|
return task
|
|
|
|
|
|
|
|
def apply_snapshot_op(self, vm):
|
|
|
|
result = {}
|
|
|
|
if self.module.params["state"] == "present":
|
2017-10-30 16:20:03 +00:00
|
|
|
if self.module.params["new_snapshot_name"] or self.module.params["new_description"]:
|
|
|
|
self.rename_snapshot(vm)
|
|
|
|
result = {'changed': True, 'failed': False, 'renamed': True}
|
|
|
|
task = None
|
|
|
|
else:
|
|
|
|
task = self.snapshot_vm(vm)
|
2017-02-06 16:52:17 +00:00
|
|
|
elif self.module.params["state"] in ["absent", "revert"]:
|
|
|
|
task = self.remove_or_revert_snapshot(vm)
|
|
|
|
elif self.module.params["state"] == "remove_all":
|
|
|
|
task = vm.RemoveAllSnapshots()
|
|
|
|
else:
|
|
|
|
# This should not happen
|
2017-11-13 16:51:18 +00:00
|
|
|
raise AssertionError()
|
2017-02-06 16:52:17 +00:00
|
|
|
|
|
|
|
if task:
|
|
|
|
self.wait_for_task(task)
|
|
|
|
if task.info.state == 'error':
|
|
|
|
result = {'changed': False, 'failed': True, 'msg': task.info.error.msg}
|
|
|
|
else:
|
2017-11-09 16:42:30 +00:00
|
|
|
result = {'changed': True, 'failed': False, 'results': list_snapshots(vm)}
|
2017-02-06 16:52:17 +00:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2017-07-21 16:12:43 +00:00
|
|
|
argument_spec = vmware_argument_spec()
|
|
|
|
argument_spec.update(
|
|
|
|
state=dict(default='present', choices=['present', 'absent', 'revert', 'remove_all']),
|
2017-12-01 15:51:45 +00:00
|
|
|
name=dict(type='str'),
|
2017-07-24 16:51:26 +00:00
|
|
|
name_match=dict(type='str', choices=['first', 'last'], default='first'),
|
2017-07-21 16:12:43 +00:00
|
|
|
uuid=dict(type='str'),
|
2019-02-25 08:37:07 +00:00
|
|
|
use_instance_uuid=dict(type='bool', default=False),
|
2017-12-01 15:51:45 +00:00
|
|
|
folder=dict(type='str'),
|
2017-07-21 16:12:43 +00:00
|
|
|
datacenter=dict(required=True, type='str'),
|
|
|
|
snapshot_name=dict(type='str'),
|
|
|
|
description=dict(type='str', default=''),
|
|
|
|
quiesce=dict(type='bool', default=False),
|
|
|
|
memory_dump=dict(type='bool', default=False),
|
|
|
|
remove_children=dict(type='bool', default=False),
|
2017-10-30 16:20:03 +00:00
|
|
|
new_snapshot_name=dict(type='str'),
|
|
|
|
new_description=dict(type='str'),
|
2017-02-06 16:52:17 +00:00
|
|
|
)
|
2017-12-01 15:51:45 +00:00
|
|
|
module = AnsibleModule(argument_spec=argument_spec,
|
|
|
|
required_together=[['name', 'folder']],
|
|
|
|
required_one_of=[['name', 'uuid']],
|
|
|
|
)
|
2017-02-06 16:52:17 +00:00
|
|
|
|
2017-12-01 15:51:45 +00:00
|
|
|
if module.params['folder']:
|
|
|
|
# FindByInventoryPath() does not require an absolute path
|
|
|
|
# so we should leave the input folder path unmodified
|
|
|
|
module.params['folder'] = module.params['folder'].rstrip('/')
|
2017-02-06 16:52:17 +00:00
|
|
|
|
|
|
|
pyv = PyVmomiHelper(module)
|
|
|
|
# Check if the VM exists before continuing
|
2017-12-01 15:51:45 +00:00
|
|
|
vm = pyv.get_vm()
|
2017-02-06 16:52:17 +00:00
|
|
|
|
|
|
|
if not vm:
|
2017-04-20 10:11:04 +00:00
|
|
|
# If UUID is set, getvm select UUID, show error message accordingly.
|
2017-11-09 16:42:30 +00:00
|
|
|
module.fail_json(msg="Unable to manage snapshots for non-existing VM %s" % (module.params.get('uuid') or
|
|
|
|
module.params.get('name')))
|
2017-02-06 16:52:17 +00:00
|
|
|
|
|
|
|
if not module.params['snapshot_name'] and module.params['state'] != 'remove_all':
|
|
|
|
module.fail_json(msg="snapshot_name param is required when state is '%(state)s'" % module.params)
|
|
|
|
|
|
|
|
result = pyv.apply_snapshot_op(vm)
|
|
|
|
|
|
|
|
if 'failed' not in result:
|
|
|
|
result['failed'] = False
|
|
|
|
|
|
|
|
if result['failed']:
|
|
|
|
module.fail_json(**result)
|
|
|
|
else:
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
2017-07-29 15:05:38 +00:00
|
|
|
|
2017-02-06 16:52:17 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|