Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2018, Ansible Project
5 # Copyright: (c) 2024, Fernando Mendieta <[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_all_snapshots_info
16 short_description: Gathers information about all snapshots across virtual machines in a specified vmware datacenter
17 description:
18 - This module collects detailed information of all the snapshots of the datacenter, can be used with filter options
19 author:
20 - Fernando Mendieta (@valkiriaaquatica)
21 options:
22 datacenter:
23 description:
24 - The name of the datacenter to gather snapshot information from. You can get it in the vmware UI.
25 required: true
26 type: str
27 filters:
28 description:
29 - Optional filters to apply to the snapshot data being gathered, you can apply one or more.
30 - Filters are applied based on the variable match_type specified. If match_type exact, filters require exact matches.
31 - On the other hand when match_type includes it gets the values that contain that value.
32 - Available filter options creation_time, description, folder, id, name, quiesced, state, vm_name.
33 - Multiple filters can be applied the snapshot must meet all filter criteria to be included in the results.
34 required: false
35 type: dict
36 default: {}
37 match_type:
38 description:
39 - Indicates whether the filter match should be exact or includes.
40 - For example when you want to get all the snapshots that contain in their name the word test you place the filter name test and the match_type includes.
41 - For example when you want to get all snapshots that are in state poweredOn you skip the match_type default is exact or you write match_type exact.
42 required: false
43 type: str
44 choices: ['exact', 'includes']
45 default: exact
46 extends_documentation_fragment:
47 - vmware.vmware.base_options
48 '''
49
50 EXAMPLES = r'''
51 - name: Gather information about all snapshots in VMware vCenter
52 vmware_all_snapshots_info:
53 hostname: '{{ vcenter_hostname }}'
54 username: '{{ vcenter_username }}'
55 password: '{{ vcenter_password }}'
56 validate_certs: no
57 datacenter: '{{ datacenter_name }}'
58 delegate_to: localhost
59 - name: Gather information of a snapshot with filters applied and match_type in exacts.
60 vmware_all_snapshots_info:
61 hostname: '{{ vcenter_hostname }}'
62 username: '{{ vcenter_username }}'
63 password: '{{ vcenter_password }}'
64 validate_certs: yes
65 datacenter: '{{ datacenter_name }}'
66 filters:
67 state: "poweredOn"
68 vm_name: "you_marchine_name"
69 delegate_to: localhost
70 - name: Gather information of snapshots that in their name contain the "test" in their name.
71 vmware_all_snapshots_info:
72 hostname: '{{ vcenter_hostname }}'
73 username: '{{ vcenter_username }}'
74 password: '{{ vcenter_password }}'
75 validate_certs: yes
76 datacenter: '{{ datacenter_name }}'
77 match_type: "includes"
78 filters:
79 name: "test"
80 delegate_to: localhost
81 '''
82
83 RETURN = r'''
84 vmware_all_snapshots_info:
85 description: A list of all snapshots information across all virtual machines in the specified datacenter
86 returned: always
87 type: list
88 elements: dict
89 contains:
90 vm_name:
91 description: The name of the virtual machine that appears in the iu.
92 type: str
93 returned: always
94 folder:
95 description: The folder path of the virtual machine in the datacenter, normally is vm
96 type: str
97 returned: always
98 name:
99 description: The name of the snapshot
100 type: str
101 returned: always
102 description:
103 description: The description of the snapshot.
104 type: str
105 returned: when it exists because depends if it has or not
106 creation_time:
107 description: The time the snapshot was created
108 type: str
109 returned: always
110 state:
111 description: The state of the virtual machine at the snapshot (powered on/off)
112 type: str
113 returned: always
114 id:
115 description: The unique identifier of the snapshot
116 type: int
117 returned: always
118 quiesced:
119 description: Indicates if the snapshot was created with the virtual machines file system quiesced
120 type: bool
121 returned: always
122 '''
123
124
125 from ansible.module_utils.basic import AnsibleModule
126 from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi, list_snapshots_recursively
127 from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec
128
129 try:
130 from pyVmomi import vim
131 except ImportError:
132 pass
133
134
135 class VMwareSnapshotInfo(PyVmomi):
136 def __init__(self, module):
137 super(VMwareSnapshotInfo, self).__init__(module)
138
139 def list_snapshots(self, vm):
140 return (
141 list_snapshots_recursively(vm.snapshot.rootSnapshotList)
142 if vm.snapshot
143 else []
144 )
145
146 def get_all_vms(self, datacenter):
147 content = self.content
148
149 datacenter_obj = self.find_datacenter_by_name(datacenter)
150 container = content.viewManager.CreateContainerView(
151 datacenter_obj, [vim.VirtualMachine], True
152 )
153 vms = container.view
154 container.Destroy()
155 return vms
156
157 def gather_snapshots_info(self, filters, match_type, datacenter=None):
158 snapshot_data = []
159 for vm in self.get_all_vms(datacenter):
160 for snapshot in self.list_snapshots(vm):
161 snapshot_info = {
162 "vm_name": vm.name,
163 "folder": vm.parent.name,
164 **snapshot,
165 }
166 if self.passes_filters(snapshot_info, filters, match_type):
167 snapshot_data.append(snapshot_info)
168 return snapshot_data
169
170 def passes_filters(self, snapshot_info, filters, match_type):
171 for key, value in filters.items():
172 if key not in snapshot_info:
173 continue
174 actual_value = str(snapshot_info[key]).lower()
175 desired_value = str(value).lower()
176
177 if match_type == "exact" and actual_value != desired_value:
178 return False
179 elif match_type == "includes" and desired_value not in actual_value:
180 return False
181 return True
182
183
184 def main():
185 argument_spec = base_argument_spec()
186 argument_spec.update(
187 datacenter=dict(required=True, type="str"),
188 filters=dict(required=False, type="dict", default={}),
189 match_type=dict(
190 required=False, type="str", choices=["exact", "includes"], default="exact"
191 ),
192 )
193
194 module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
195 vmware_snapshot_info = VMwareSnapshotInfo(module)
196 datacenter = module.params.get("datacenter")
197 filters = module.params.get("filters")
198 match_type = module.params.get("match_type")
199 all_snapshots = vmware_snapshot_info.gather_snapshots_info(filters, match_type, datacenter)
200 module.exit_json(changed=False, vmware_all_snapshots_info=all_snapshots)
201
202
203 if __name__ == "__main__":
204 main()