Repository: ansible/ansible

Issue:
vmware_guest_snapshot should allow memory dump and quiese of FS

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Feature Idea

##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
vmware_guest_snapshot.py

##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
```
ansible 2.3.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609]
```

##### CONFIGURATION
<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->

##### OS / ENVIRONMENT
<!---
Mention the OS you are running Ansible from, and the OS you are
managing, or say “N/A” for anything that is not platform-specific.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.
-->
Ubuntu 16.04 64 bits

##### SUMMARY
<!--- Explain the problem briefly -->
currently snapshot operations call CreateSnapshot from pyvmomi (https://github.com/vmware/pyvmomi/blob/575ab56eb56f32f53c98f40b9b496c6219c161da/docs/vim/VirtualMachine.rst) with flags for memory dump and filesystem quiece in False. This leads to every snapshot of a VM be in a powered down state irregardless of the state of the VM when the snapshot is taken.
Having these flags in False makes for a really fast snapshot procedure, but I belive it is less usefull.

##### STEPS TO REPRODUCE
<!---
For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used.
-->
dummy_vm is powered on

<!--- Paste example playbooks or commands between quotes below -->
```yaml
- name: Create snapshot
  vmware_guest_snapshot:
    hostname: 192.168.1.209
    username: administrator@vsphere.local
    password: vmware
    name: dummy_vm
    state: present
    snapshot_name: snap1
    description: snap1_description
  delegate_to: localhost
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
restoring snapshot will result in dummy_vm in powered on state.

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

dummy_vm is restored in powered off state.

<!--- Paste verbatim command output between quotes below -->
```

```



Discussion:
cc @nerzhul
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/lib/ansible/modules/cloud/vmware/vmware_guest_snapshot.py b/lib/ansible/modules/cloud/vmware/vmware_guest_snapshot.py
--- a/lib/ansible/modules/cloud/vmware/vmware_guest_snapshot.py
+++ b/lib/ansible/modules/cloud/vmware/vmware_guest_snapshot.py
@@ -71,6 +71,25 @@
    description:
         description:
         - Define an arbitrary description to attach to snapshot.
+   quiesce:
+        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"
+   memory_dump:
+        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"
 extends_documentation_fragment: vmware.documentation
 '''
 
@@ -114,6 +133,18 @@
       name: dummy_vm
       state: remove_all
     delegate_to: localhost
+
+  - name: Take snapshot of a VM using quiesce and memory flag on
+    vmware_guest_snapshot:
+      hostname: 192.168.1.209
+      username: administrator@vsphere.local
+      password: vmware
+      name: dummy_vm
+      state: present
+      snapshot_name: dummy_vm_snap_0001
+      quiesce: True
+      memory_dump: True
+    delegate_to: localhost
 '''
 
 RETURN = """
@@ -207,11 +238,18 @@ def get_snapshots_by_name_recursively(self, snapshots, snapname):
         return snap_obj
 
     def snapshot_vm(self, vm):
-        dump_memory = False
+        memory_dump = False
         quiesce = False
+        # Check if Virtual Machine provides capabilities for Quiesce and Memory
+        # Snapshots
+        if vm.capability.quiescedSnapshotsSupported:
+            quiesce = self.module.params['quiesce']
+        if vm.capability.memorySnapshotsSupported:
+            memory_dump = self.module.params['memory_dump']
+
         return vm.CreateSnapshot(self.module.params["snapshot_name"],
                                  self.module.params["description"],
-                                 dump_memory,
+                                 memory_dump,
                                  quiesce)
 
     def remove_or_revert_snapshot(self, vm):
@@ -283,6 +321,8 @@ def main():
             datacenter=dict(required=True, type='str'),
             snapshot_name=dict(required=False, type='str'),
             description=dict(required=False, type='str', default=''),
+            quiesce=dict(type='bool', default=False),
+            memory_dump=dict(type='bool', default=False),
         ),
     )
 


Test patch: