Debian Codegraph @

ansible_collections/community/vmware/plugins/modules/vmware_guest_boot_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_boot_info
16 short_description: Gather info about boot options for the given virtual machine
17 description:
18 - Gather information about boot options for the given virtual machine.
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) parameter 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 type: str
32 moid:
33 description:
34 - Managed Object ID of the instance to manage if known, this is a unique identifier only within a single vCenter instance.
35 - This is required if O(name) or O(uuid) is not supplied.
36 type: str
37 use_instance_uuid:
38 description:
39 - Whether to use the VMware instance UUID rather than the BIOS UUID.
40 default: false
41 type: bool
42 name_match:
43 description:
44 - If multiple virtual machines matching the name, use the first or last found.
45 default: 'first'
46 choices: ['first', 'last']
47 type: str
48 extends_documentation_fragment:
49 - vmware.vmware.base_options
50
51 '''
52
53 EXAMPLES = r'''
54 - name: Gather info about virtual machine's boot order and related parameters
55 community.vmware.vmware_guest_boot_info:
56 hostname: "{{ vcenter_hostname }}"
57 username: "{{ vcenter_username }}"
58 password: "{{ vcenter_password }}"
59 name: "{{ vm_name }}"
60 register: vm_boot_order_info
61
62 - name: Gather information about virtual machine's boot order using MoID
63 community.vmware.vmware_guest_boot_info:
64 hostname: "{{ vcenter_hostname }}"
65 username: "{{ vcenter_username }}"
66 password: "{{ vcenter_password }}"
67 moid: "vm-42"
68 register: vm_moid_boot_order_info
69 '''
70
71 RETURN = r'''
72 vm_boot_info:
73 description: metadata about boot order of virtual machine
74 returned: always
75 type: dict
76 sample: {
77 "current_boot_order": [
78 "floppy",
79 "disk",
80 "ethernet",
81 "cdrom"
82 ],
83 "current_boot_delay": 2000,
84 "current_boot_retry_delay": 22300,
85 "current_boot_retry_enabled": true,
86 "current_enter_bios_setup": true,
87 "current_boot_firmware": "bios",
88 "current_secure_boot_enabled": false,
89 }
90 '''
91
92
93 from ansible.module_utils.basic import AnsibleModule
94 from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi, find_vm_by_id
95 from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec
96
97 try:
98 from pyVmomi import vim, VmomiJSONEncoder
99 except ImportError:
100 pass
101
102
103 class VmBootInfoManager(PyVmomi):
104 def __init__(self, module):
105 super(VmBootInfoManager, self).__init__(module)
106 self.name = self.params['name']
107 self.uuid = self.params['uuid']
108 self.moid = self.params['moid']
109 self.use_instance_uuid = self.params['use_instance_uuid']
110 self.vm = None
111
112 def _get_vm(self):
113 vms = []
114
115 if self.uuid:
116 if self.use_instance_uuid:
117 vm_obj = find_vm_by_id(self.content, vm_id=self.uuid, vm_id_type="use_instance_uuid")
118 else:
119 vm_obj = find_vm_by_id(self.content, vm_id=self.uuid, vm_id_type="uuid")
120 if vm_obj is None:
121 self.module.fail_json(msg="Failed to find the virtual machine with UUID : %s" % self.uuid)
122 vms = [vm_obj]
123
124 elif self.name:
125 objects = self.get_managed_objects_properties(vim_type=vim.VirtualMachine, properties=['name'])
126 for temp_vm_object in objects:
127 if temp_vm_object.obj.name == self.name:
128 vms.append(temp_vm_object.obj)
129
130 elif self.moid:
131 vm_obj = VmomiJSONEncoder.templateOf('VirtualMachine')(self.module.params['moid'], self.si._stub)
132 if vm_obj:
133 vms.append(vm_obj)
134
135 if vms:
136 if self.params.get('name_match') == 'first':
137 self.vm = vms[0]
138 elif self.params.get('name_match') == 'last':
139 self.vm = vms[-1]
140 else:
141 self.module.fail_json(msg="Failed to find virtual machine using %s" % (self.name or self.uuid or self.moid))
142
143 @staticmethod
144 def humanize_boot_order(boot_order):
145 results = []
146 for device in boot_order:
147 if isinstance(device, vim.vm.BootOptions.BootableCdromDevice):
148 results.append('cdrom')
149 elif isinstance(device, vim.vm.BootOptions.BootableDiskDevice):
150 results.append('disk')
151 elif isinstance(device, vim.vm.BootOptions.BootableEthernetDevice):
152 results.append('ethernet')
153 elif isinstance(device, vim.vm.BootOptions.BootableFloppyDevice):
154 results.append('floppy')
155 return results
156
157 def ensure(self):
158 self._get_vm()
159
160 results = dict()
161 if self.vm and self.vm.config:
162 results = dict(
163 current_boot_order=self.humanize_boot_order(self.vm.config.bootOptions.bootOrder),
164 current_boot_delay=self.vm.config.bootOptions.bootDelay,
165 current_enter_bios_setup=self.vm.config.bootOptions.enterBIOSSetup,
166 current_boot_retry_enabled=self.vm.config.bootOptions.bootRetryEnabled,
167 current_boot_retry_delay=self.vm.config.bootOptions.bootRetryDelay,
168 current_boot_firmware=self.vm.config.firmware,
169 current_secure_boot_enabled=self.vm.config.bootOptions.efiSecureBootEnabled
170 )
171
172 self.module.exit_json(changed=False, vm_boot_info=results)
173
174
175 def main():
176 argument_spec = base_argument_spec()
177 argument_spec.update(
178 name=dict(type='str'),
179 uuid=dict(type='str'),
180 moid=dict(type='str'),
181 use_instance_uuid=dict(type='bool', default=False),
182 name_match=dict(
183 choices=['first', 'last'],
184 default='first'
185 ),
186 )
187
188 module = AnsibleModule(
189 argument_spec=argument_spec,
190 required_one_of=[
191 ['name', 'uuid', 'moid']
192 ],
193 mutually_exclusive=[
194 ['name', 'uuid', 'moid']
195 ],
196 supports_check_mode=True,
197 )
198
199 pyv = VmBootInfoManager(module)
200 pyv.ensure()
201
202
203 if __name__ == '__main__':
204 main()