Add details about snapshot in result (#32730)

This fix adds details about snapshots in result of
vmware_guest_snapshot, when operation is successful.

Fixes: #32154

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
pull/32751/head
Abhijeet Kasurde 7 years ago committed by Sam Doran
parent 1a3037ff63
commit dbadb5a014

@ -207,18 +207,15 @@ instance:
import time import time
HAS_PYVMOMI = False
try: try:
import pyVmomi import pyVmomi
from pyVmomi import vim from pyVmomi import vim
HAS_PYVMOMI = True
except ImportError: except ImportError:
pass pass
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils._text import to_native from ansible.module_utils._text import to_native
from ansible.module_utils.vmware import connect_to_api, vmware_argument_spec, find_vm_by_id from ansible.module_utils.vmware import connect_to_api, find_vm_by_id, HAS_PYVMOMI, list_snapshots, vmware_argument_spec
class PyVmomiHelper(object): class PyVmomiHelper(object):
@ -277,7 +274,7 @@ class PyVmomiHelper(object):
except vim.fault.RestrictedVersion as exc: except vim.fault.RestrictedVersion as exc:
self.module.fail_json(msg="Failed to take snapshot due to VMware Licence: %s" % to_native(exc.msg)) self.module.fail_json(msg="Failed to take snapshot due to VMware Licence: %s" % to_native(exc.msg))
except Exception as exc: except Exception as exc:
self.module.fail_json(msg="Failed to create snapshot of VM %s due to %s" % (self.module.params['name'], to_native(exc.msg))) self.module.fail_json(msg="Failed to create snapshot of VM %s due to %s" % (self.module.params['name'], to_native(exc)))
return task return task
@ -352,7 +349,7 @@ class PyVmomiHelper(object):
if task.info.state == 'error': if task.info.state == 'error':
result = {'changed': False, 'failed': True, 'msg': task.info.error.msg} result = {'changed': False, 'failed': True, 'msg': task.info.error.msg}
else: else:
result = {'changed': True, 'failed': False} result = {'changed': True, 'failed': False, 'results': list_snapshots(vm)}
return result return result
@ -388,10 +385,8 @@ def main():
if not vm: if not vm:
# If UUID is set, getvm select UUID, show error message accordingly. # If UUID is set, getvm select UUID, show error message accordingly.
if module.params['uuid'] is not None: module.fail_json(msg="Unable to manage snapshots for non-existing VM %s" % (module.params.get('uuid') or
module.fail_json(msg="Unable to manage snapshots for non-existing VM %(uuid)s" % module.params) module.params.get('name')))
else:
module.fail_json(msg="Unable to manage snapshots for non-existing VM %(name)s" % module.params)
if not module.params['snapshot_name'] and module.params['state'] != 'remove_all': 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) module.fail_json(msg="snapshot_name param is required when state is '%(state)s'" % module.params)

@ -176,3 +176,23 @@
# name: "{{ vm1 | basename }}" # name: "{{ vm1 | basename }}"
# state: revert # state: revert
# snapshot_name: snap_a # snapshot_name: snap_a
# # Test0009: Create snap_a and check in result
# - name: 0009 - create snapshot a
# vmware_guest_snapshot:
# validate_certs: False
# hostname: "{{ vcsim }}"
# username: "{{ vcsim_instance['json']['username'] }}"
# password: "{{ vcsim_instance['json']['password'] }}"
# datacenter: "{{ dc1 | basename }}"
# folder: "{{ vm1 | dirname }}"
# name: "{{ vm1 | basename }}"
# state: present
# snapshot_name: snap_a
# description: "snap named a"
# register: snapshot_details
# - name: Check if snapshot details available or not
# assert:
# that:
# - "snapshot_details.results['current_snapshot']['name'] == 'snap_a'

Loading…
Cancel
Save