Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2017, Ansible Project
5 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
6 # SPDX-License-Identifier: GPL-3.0-or-later
7
8 from __future__ import absolute_import, division, print_function
9 __metaclass__ = type
10
11
12 DOCUMENTATION = r'''
13 ---
14 module: vmware_guest_find
15 short_description: Find the folder path(s) for a virtual machine by name or UUID
16 description:
17 - Find the folder path(s) for a virtual machine by name or UUID
18 author:
19 - Abhijeet Kasurde (@Akasurde) <[email protected]>
20 options:
21 name:
22 description:
23 - Name of the VM to work with.
24 - This is required if O(uuid) parameter is not supplied.
25 type: str
26 uuid:
27 description:
28 - UUID of the instance to manage if known, this is VMware's BIOS UUID by default.
29 - This is required if O(name) parameter is not supplied.
30 type: str
31 use_instance_uuid:
32 description:
33 - Whether to use the VMware instance UUID rather than the BIOS UUID.
34 default: false
35 type: bool
36 extends_documentation_fragment:
37 - vmware.vmware.base_options
38
39 '''
40
41 EXAMPLES = r'''
42 - name: Find Guest's Folder using name
43 community.vmware.vmware_guest_find:
44 hostname: "{{ vcenter_hostname }}"
45 username: "{{ vcenter_username }}"
46 password: "{{ vcenter_password }}"
47 name: testvm
48 delegate_to: localhost
49 register: vm_folder
50
51 - name: Find Guest's Folder using UUID
52 community.vmware.vmware_guest_find:
53 hostname: "{{ vcenter_hostname }}"
54 username: "{{ vcenter_username }}"
55 password: "{{ vcenter_password }}"
56 uuid: 38c4c89c-b3d7-4ae6-ae4e-43c5118eae49
57 delegate_to: localhost
58 register: vm_folder
59 '''
60
61 RETURN = r'''
62 folders:
63 description: List of folders for user specified virtual machine
64 returned: on success
65 type: list
66 sample: [
67 '/DC0/vm',
68 ]
69 '''
70
71
72 from ansible.module_utils.basic import AnsibleModule
73 from ansible.module_utils.common.text.converters import to_native
74 from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi, find_vm_by_id
75 from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec
76
77 try:
78 from pyVmomi import vim
79 except ImportError:
80 pass
81
82
83 class PyVmomiHelper(PyVmomi):
84 def __init__(self, module):
85 super(PyVmomiHelper, self).__init__(module)
86 self.name = self.params['name']
87 self.uuid = self.params['uuid']
88 self.use_instance_uuid = self.params['use_instance_uuid']
89
90 def getvm_folder_paths(self):
91 results = []
92 vms = []
93
94 if self.uuid:
95 if self.use_instance_uuid:
96 vm_obj = find_vm_by_id(self.content, vm_id=self.uuid, vm_id_type="instance_uuid")
97 else:
98 vm_obj = find_vm_by_id(self.content, vm_id=self.uuid, vm_id_type="uuid")
99 if vm_obj is None:
100 self.module.fail_json(msg="Failed to find the virtual machine with UUID : %s" % self.uuid)
101 vms = [vm_obj]
102
103 elif self.name:
104 objects = self.get_managed_objects_properties(vim_type=vim.VirtualMachine, properties=['name'])
105 for temp_vm_object in objects:
106 if temp_vm_object.obj.name == self.name:
107 vms.append(temp_vm_object.obj)
108
109 for vm in vms:
110 folder_path = self.get_vm_path(self.content, vm)
111 results.append(folder_path)
112
113 return results
114
115
116 def main():
117 argument_spec = base_argument_spec()
118 argument_spec.update(
119 name=dict(type='str'),
120 uuid=dict(type='str'),
121 use_instance_uuid=dict(type='bool', default=False),
122 )
123
124 module = AnsibleModule(argument_spec=argument_spec,
125 required_one_of=[['name', 'uuid']],
126 mutually_exclusive=[['name', 'uuid']],
127 )
128
129 pyv = PyVmomiHelper(module)
130 # Check if the VM exists before continuing
131 folders = pyv.getvm_folder_paths()
132
133 # VM already exists
134 if folders:
135 try:
136 module.exit_json(folders=folders)
137 except Exception as exc:
138 module.fail_json(msg="Folder enumeration failed with exception %s" % to_native(exc))
139 else:
140 module.fail_json(msg="Unable to find folders for virtual machine %s" % (
141 module.params.get('name')
142 or module.params.get('uuid')))
143
144
145 if __name__ == '__main__':
146 main()