Debian Codegraph @

ansible_collections/community/vmware/plugins/modules/vmware_guest_snapshot_info.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2018, Ansible Project
5 # Copyright: (c) 2018, Abhijeet Kasurde <[email protected]>
6 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
7 # SPDX-License-Identifier: GPL-3.0-or-later
8
9 from __future__ import absolute_import, division, print_function
10 __metaclass__ = type
11
12
13 DOCUMENTATION = r'''
14 ---
15 module: vmware_guest_snapshot_info
16 short_description: Gather info about virtual machine's snapshots in vCenter
17 description:
18 - This module can be used to gather information about virtual machine's snapshots.
19 author:
20 - Abhijeet Kasurde (@Akasurde)
21 options:
22 name:
23 description:
24 - Name of the VM to work with.
25 - This is required if O(uuid) or O(moid) is not supplied.
26 type: str
27 uuid:
28 description:
29 - UUID of the instance to manage if known, this is VMware's BIOS UUID by default.
30 - This is required if O(name) or O(moid) parameter is not supplied.
31 - The O(folder) is ignored, if O(uuid) is provided.
32 type: str
33 moid:
34 description:
35 - Managed Object ID of the instance to manage if known, this is a unique identifier only within a single vCenter instance.
36 - This is required if O(name) or O(uuid) is not supplied.
37 type: str
38 use_instance_uuid:
39 description:
40 - Whether to use the VMware instance UUID rather than the BIOS UUID.
41 default: false
42 type: bool
43 folder:
44 description:
45 - Destination folder, absolute or relative path to find an existing guest.
46 - This is required parameter, if O(name) is supplied.
47 - The folder should include the datacenter. ESX's datacenter is ha-datacenter
48 - 'Examples:'
49 - ' folder: /ha-datacenter/vm'
50 - ' folder: ha-datacenter/vm'
51 - ' folder: /datacenter1/vm'
52 - ' folder: datacenter1/vm'
53 - ' folder: /datacenter1/vm/folder1'
54 - ' folder: datacenter1/vm/folder1'
55 - ' folder: /folder1/datacenter1/vm'
56 - ' folder: folder1/datacenter1/vm'
57 - ' folder: /folder1/datacenter1/vm/folder2'
58 type: str
59 datacenter:
60 description:
61 - Name of the datacenter.
62 required: true
63 type: str
64 extends_documentation_fragment:
65 - vmware.vmware.base_options
66
67 '''
68
69 EXAMPLES = r'''
70 - name: Gather snapshot information about the virtual machine in the given vCenter
71 community.vmware.vmware_guest_snapshot_info:
72 hostname: "{{ vcenter_hostname }}"
73 username: "{{ vcenter_username }}"
74 password: "{{ vcenter_password }}"
75 datacenter: "{{ datacenter_name }}"
76 folder: "/{{ datacenter_name }}/vm/"
77 name: "{{ guest_name }}"
78 delegate_to: localhost
79 register: snapshot_info
80
81 - name: Gather snapshot information about the virtual machine using MoID
82 community.vmware.vmware_guest_snapshot_info:
83 hostname: "{{ vcenter_hostname }}"
84 username: "{{ vcenter_username }}"
85 password: "{{ vcenter_password }}"
86 datacenter: "{{ datacenter_name }}"
87 moid: vm-42
88 delegate_to: localhost
89 register: snapshot_info
90 '''
91
92 RETURN = r'''
93 guest_snapshots:
94 description: metadata about the snapshot information
95 returned: always
96 type: dict
97 sample: {
98 "current_snapshot": {
99 "creation_time": "2018-02-10T14:48:31.999459+00:00",
100 "description": "",
101 "id": 28,
102 "name": "snap_0003",
103 "state": "poweredOff",
104 "quiesced": false
105 },
106 "snapshots": [
107 {
108 "creation_time": "2018-02-10T14:48:31.999459+00:00",
109 "description": "",
110 "id": 28,
111 "name": "snap_0003",
112 "state": "poweredOff",
113 "quiesced": false
114 }
115 ]
116 }
117 '''
118
119 from ansible.module_utils.basic import AnsibleModule
120 from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi, list_snapshots
121 from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec
122
123
124 class PyVmomiHelper(PyVmomi):
125 def __init__(self, module):
126 super(PyVmomiHelper, self).__init__(module)
127
128 @staticmethod
129 def gather_guest_snapshot_info(vm_obj=None):
130 """
131 Return snapshot related information about given virtual machine
132 Args:
133 vm_obj: Virtual Machine Managed object
134
135 Returns: Dictionary containing snapshot information
136
137 """
138 if vm_obj is None:
139 return {}
140 return list_snapshots(vm=vm_obj)
141
142
143 def main():
144 argument_spec = base_argument_spec()
145 argument_spec.update(
146 name=dict(type='str'),
147 uuid=dict(type='str'),
148 moid=dict(type='str'),
149 use_instance_uuid=dict(type='bool', default=False),
150 folder=dict(type='str'),
151 datacenter=dict(required=True, type='str'),
152 )
153 module = AnsibleModule(
154 argument_spec=argument_spec,
155 required_together=[
156 ['name', 'folder']
157 ],
158 required_one_of=[
159 ['name', 'uuid', 'moid']
160 ],
161 supports_check_mode=True,
162 )
163
164 if module.params['folder']:
165 # FindByInventoryPath() does not require an absolute path
166 # so we should leave the input folder path unmodified
167 module.params['folder'] = module.params['folder'].rstrip('/')
168
169 pyv = PyVmomiHelper(module)
170 # Check if the VM exists before continuing
171 vm = pyv.get_vm()
172
173 if not vm:
174 # If UUID is set, get_vm select UUID, show error message accordingly.
175 vm_id = (module.params.get('uuid') or module.params.get('name') or module.params.get('moid'))
176 module.fail_json(msg="Unable to gather information about snapshots for"
177 " non-existing VM ['%s']" % vm_id)
178
179 results = dict(changed=False, guest_snapshots=pyv.gather_guest_snapshot_info(vm_obj=vm))
180 module.exit_json(**results)
181
182
183 if __name__ == '__main__':
184 main()