| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2021, Ansible Project |
| 5 |
# Copyright: (c) 2021, VMware, Inc. All Rights Reserved. |
| 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_vm_config_option |
| 16 |
short_description: Return supported guest ID list and VM recommended config option for specific guest OS |
| 17 |
description: > |
| 18 |
This module is used for getting the hardware versions supported for creation, the guest ID list supported by ESXi |
| 19 |
host for the most recent virtual hardware supported or specified hardware version, the VM recommended config options |
| 20 |
for specified guest OS ID. |
| 21 |
author: |
| 22 |
- Diane Wang (@Tomorrow9) <[email protected]> |
| 23 |
notes: |
| 24 |
- Known issue on vSphere 7.0 (https://github.com/vmware/pyvmomi/issues/915) |
| 25 |
options: |
| 26 |
datacenter: |
| 27 |
description: |
| 28 |
- The datacenter name used to get specified cluster or host. |
| 29 |
default: ha-datacenter |
| 30 |
type: str |
| 31 |
cluster_name: |
| 32 |
description: |
| 33 |
- Name of the cluster. |
| 34 |
- If O(esxi_hostname) is not given, this parameter is required. |
| 35 |
type: str |
| 36 |
esxi_hostname: |
| 37 |
description: |
| 38 |
- ESXi hostname. |
| 39 |
- Obtain VM configure options on this ESXi host. |
| 40 |
- If O(cluster_name) is not given, this parameter is required. |
| 41 |
type: str |
| 42 |
get_hardware_versions: |
| 43 |
description: |
| 44 |
- Return the list of VM hardware versions supported for creation and the default hardware version on the |
| 45 |
specified entity. |
| 46 |
type: bool |
| 47 |
default: false |
| 48 |
get_guest_os_ids: |
| 49 |
description: |
| 50 |
- Return the list of guest OS IDs supported on the specified entity. |
| 51 |
- If O(hardware_version) is set, will return the corresponding guest OS ID list supported, or will return the |
| 52 |
guest OS ID list for the default hardware version. |
| 53 |
type: bool |
| 54 |
default: false |
| 55 |
get_config_options: |
| 56 |
description: |
| 57 |
- Return the dict of VM recommended config options for guest ID specified by O(guest_id) with hardware version |
| 58 |
specified by O(hardware_version) or the default hardware version. |
| 59 |
- When set to V(true), O(guest_id) must be set. |
| 60 |
type: bool |
| 61 |
default: false |
| 62 |
guest_id: |
| 63 |
description: |
| 64 |
- The guest OS ID from the returned list when O(get_guest_os_ids=true), e.g., 'rhel8_64Guest'. |
| 65 |
- This parameter must be set when O(get_config_options=true). |
| 66 |
type: str |
| 67 |
hardware_version: |
| 68 |
description: |
| 69 |
- The hardware version from the returned list when O(get_hardware_versions=true), e.g., 'vmx-19'. |
| 70 |
type: str |
| 71 |
extends_documentation_fragment: |
| 72 |
- vmware.vmware.base_options |
| 73 |
|
| 74 |
''' |
| 75 |
|
| 76 |
EXAMPLES = r''' |
| 77 |
- name: Get supported guest ID list on given ESXi host for with default hardware version |
| 78 |
community.vmware.vmware_vm_config_option: |
| 79 |
hostname: "{{ vcenter_hostname }}" |
| 80 |
username: "{{ vcenter_username }}" |
| 81 |
password: "{{ vcenter_password }}" |
| 82 |
esxi_hostname: "{{ esxi_hostname }}" |
| 83 |
get_guest_os_ids: true |
| 84 |
delegate_to: localhost |
| 85 |
|
| 86 |
- name: Get VM recommended config option for Windows 10 guest OS on given ESXi host |
| 87 |
community.vmware.vmware_vm_config_option: |
| 88 |
hostname: "{{ vcenter_hostname }}" |
| 89 |
username: "{{ vcenter_username }}" |
| 90 |
password: "{{ vcenter_password }}" |
| 91 |
esxi_hostname: "{{ esxi_hostname }}" |
| 92 |
get_config_options: true |
| 93 |
guest_id: "windows9_64Guest" |
| 94 |
delegate_to: localhost |
| 95 |
''' |
| 96 |
|
| 97 |
RETURN = r''' |
| 98 |
instance: |
| 99 |
description: metadata about the VM recommended configuration |
| 100 |
returned: always |
| 101 |
type: dict |
| 102 |
sample: None |
| 103 |
''' |
| 104 |
|
| 105 |
try: |
| 106 |
from pyVmomi import vim |
| 107 |
except ImportError: |
| 108 |
pass |
| 109 |
|
| 110 |
from ansible.module_utils.basic import AnsibleModule |
| 111 |
from ansible.module_utils.common.text.converters import to_native |
| 112 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import find_obj, PyVmomi |
| 113 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 114 |
from ansible_collections.community.vmware.plugins.module_utils.vm_device_helper import PyVmomiDeviceHelper |
| 115 |
|
| 116 |
|
| 117 |
class VmConfigOption(PyVmomi): |
| 118 |
def __init__(self, module): |
| 119 |
super(VmConfigOption, self).__init__(module) |
| 120 |
self.device_helper = PyVmomiDeviceHelper(self.module) |
| 121 |
self.ctl_device_type = self.device_helper.disk_ctl_device_type.copy() |
| 122 |
self.ctl_device_type.update(self.device_helper.usb_device_type) |
| 123 |
self.ctl_device_type.update(self.device_helper.nic_device_type) |
| 124 |
self.target_host = None |
| 125 |
|
| 126 |
def get_hardware_versions(self, env_browser): |
| 127 |
support_create = [] |
| 128 |
default_config = '' |
| 129 |
try: |
| 130 |
desc = env_browser.QueryConfigOptionDescriptor() |
| 131 |
except Exception as e: |
| 132 |
self.module.fail_json(msg="Failed to obtain VM config option descriptor due to fault: %s" % to_native(e)) |
| 133 |
if desc: |
| 134 |
for option_desc in desc: |
| 135 |
if option_desc.createSupported: |
| 136 |
support_create = support_create + [option_desc.key] |
| 137 |
if option_desc.defaultConfigOption: |
| 138 |
default_config = option_desc.key |
| 139 |
|
| 140 |
return support_create, default_config |
| 141 |
|
| 142 |
def get_config_option_by_spec(self, env_browser, guest_id=None, key=''): |
| 143 |
vm_config_option = None |
| 144 |
if guest_id is None: |
| 145 |
guest_id = [] |
| 146 |
if self.is_vcenter(): |
| 147 |
host = self.target_host |
| 148 |
else: |
| 149 |
host = None |
| 150 |
config_query_spec = vim.EnvironmentBrowser.ConfigOptionQuerySpec(guestId=guest_id, host=host, key=key) |
| 151 |
try: |
| 152 |
vm_config_option = env_browser.QueryConfigOptionEx(spec=config_query_spec) |
| 153 |
except Exception as e: |
| 154 |
self.module.fail_json(msg="Failed to obtain VM config options due to fault: %s" % to_native(e)) |
| 155 |
|
| 156 |
return vm_config_option |
| 157 |
|
| 158 |
def get_config_option_recommended(self, guest_os_desc, hwv_version=''): |
| 159 |
guest_os_option_dict = {} |
| 160 |
support_usb_controller = [] |
| 161 |
support_disk_controller = [] |
| 162 |
support_ethernet_card = [] |
| 163 |
if guest_os_desc and len(guest_os_desc) != 0: |
| 164 |
default_disk_ctl = default_ethernet = default_cdrom_ctl = default_usb_ctl = '' |
| 165 |
for name, dev_type in self.ctl_device_type.items(): |
| 166 |
for supported_type in guest_os_desc[0].supportedUSBControllerList: |
| 167 |
if supported_type == dev_type: |
| 168 |
support_usb_controller = support_usb_controller + [name] |
| 169 |
if dev_type == guest_os_desc[0].recommendedUSBController: |
| 170 |
default_usb_ctl = name |
| 171 |
for supported_type in guest_os_desc[0].supportedEthernetCard: |
| 172 |
if supported_type == dev_type: |
| 173 |
support_ethernet_card = support_ethernet_card + [name] |
| 174 |
if dev_type == guest_os_desc[0].recommendedEthernetCard: |
| 175 |
default_ethernet = name |
| 176 |
for supported_type in guest_os_desc[0].supportedDiskControllerList: |
| 177 |
if supported_type == dev_type: |
| 178 |
support_disk_controller = support_disk_controller + [name] |
| 179 |
if dev_type == guest_os_desc[0].recommendedDiskController: |
| 180 |
default_disk_ctl = name |
| 181 |
if dev_type == guest_os_desc[0].recommendedCdromController: |
| 182 |
default_cdrom_ctl = name |
| 183 |
guest_os_option_dict = { |
| 184 |
'hardware_version': hwv_version, |
| 185 |
'guest_id': guest_os_desc[0].id, |
| 186 |
'guest_fullname': guest_os_desc[0].fullName, |
| 187 |
'rec_cpu_cores_per_socket': guest_os_desc[0].numRecommendedCoresPerSocket, |
| 188 |
'rec_cpu_socket': guest_os_desc[0].numRecommendedPhysicalSockets, |
| 189 |
'rec_memory_mb': guest_os_desc[0].recommendedMemMB, |
| 190 |
'rec_firmware': guest_os_desc[0].recommendedFirmware, |
| 191 |
'default_secure_boot': guest_os_desc[0].defaultSecureBoot, |
| 192 |
'support_secure_boot': guest_os_desc[0].supportsSecureBoot, |
| 193 |
'default_disk_controller': default_disk_ctl, |
| 194 |
'rec_disk_mb': guest_os_desc[0].recommendedDiskSizeMB, |
| 195 |
'default_ethernet': default_ethernet, |
| 196 |
'default_cdrom_controller': default_cdrom_ctl, |
| 197 |
'default_usb_controller': default_usb_ctl, |
| 198 |
'support_tpm_20': guest_os_desc[0].supportsTPM20, |
| 199 |
'support_persistent_memory': guest_os_desc[0].persistentMemorySupported, |
| 200 |
'rec_persistent_memory': guest_os_desc[0].recommendedPersistentMemoryMB, |
| 201 |
'support_min_persistent_mem_mb': guest_os_desc[0].supportedMinPersistentMemoryMB, |
| 202 |
'rec_vram_kb': guest_os_desc[0].vRAMSizeInKB.defaultValue, |
| 203 |
'support_usb_controller': support_usb_controller, |
| 204 |
'support_disk_controller': support_disk_controller, |
| 205 |
'support_ethernet_card': support_ethernet_card, |
| 206 |
'support_cpu_hotadd': guest_os_desc[0].supportsCpuHotAdd, |
| 207 |
'support_memory_hotadd': guest_os_desc[0].supportsMemoryHotAdd, |
| 208 |
'support_for_create': guest_os_desc[0].supportedForCreate |
| 209 |
} |
| 210 |
|
| 211 |
return guest_os_option_dict |
| 212 |
|
| 213 |
def get_guest_id_list(self, guest_os_desc): |
| 214 |
gos_id_list = [] |
| 215 |
if guest_os_desc: |
| 216 |
for gos_desc in guest_os_desc.guestOSDescriptor: |
| 217 |
gos_id_list = gos_id_list + [gos_desc.id] |
| 218 |
|
| 219 |
return gos_id_list |
| 220 |
|
| 221 |
def get_config_option_for_guest(self): |
| 222 |
results = {} |
| 223 |
guest_id = [] |
| 224 |
datacenter_name = self.params.get('datacenter') |
| 225 |
cluster_name = self.params.get('cluster_name') |
| 226 |
esxi_host_name = self.params.get('esxi_hostname') |
| 227 |
if self.params.get('guest_id'): |
| 228 |
guest_id = [self.params.get('guest_id')] |
| 229 |
|
| 230 |
if not self.params.get('get_hardware_versions') and not self.params.get('get_guest_os_ids') \ |
| 231 |
and not self.params.get('get_config_options'): |
| 232 |
self.module.exit_json(msg="Please set at least one of these parameters 'get_hardware_versions'," |
| 233 |
" 'get_guest_os_ids', 'get_config_options' to True to get the desired info.") |
| 234 |
if self.params.get('get_config_options') and len(guest_id) == 0: |
| 235 |
self.module.fail_json(msg="Please set 'guest_id' when 'get_config_options' is set to True," |
| 236 |
" to get the VM recommended config option for specific guest OS.") |
| 237 |
|
| 238 |
# Get the datacenter object |
| 239 |
datacenter = find_obj(self.content, [vim.Datacenter], datacenter_name) |
| 240 |
if not datacenter: |
| 241 |
self.module.fail_json(msg='Unable to find datacenter "%s"' % datacenter_name) |
| 242 |
# Get the cluster object |
| 243 |
if cluster_name: |
| 244 |
cluster = find_obj(self.content, [vim.ComputeResource], cluster_name, folder=datacenter) |
| 245 |
if not cluster: |
| 246 |
self.module.fail_json(msg='Unable to find cluster "%s"' % cluster_name) |
| 247 |
# If host is given, get the cluster object using the host |
| 248 |
elif esxi_host_name: |
| 249 |
host = find_obj(self.content, [vim.HostSystem], esxi_host_name, folder=datacenter) |
| 250 |
if not host: |
| 251 |
self.module.fail_json(msg='Unable to find host "%s"' % esxi_host_name) |
| 252 |
self.target_host = host |
| 253 |
cluster = host.parent |
| 254 |
# Define the environment browser object the ComputeResource presents |
| 255 |
env_browser = cluster.environmentBrowser |
| 256 |
if env_browser is None: |
| 257 |
self.module.fail_json(msg="The environmentBrowser of the ComputeResource is None, so can not get the" |
| 258 |
" desired config option info, please check your vSphere environment.") |
| 259 |
# Get supported hardware versions list |
| 260 |
support_create_list, default_config = self.get_hardware_versions(env_browser=env_browser) |
| 261 |
if self.params.get('get_hardware_versions'): |
| 262 |
results.update({'supported_hardware_versions': support_create_list, |
| 263 |
'default_hardware_version': default_config}) |
| 264 |
|
| 265 |
if self.params.get('get_guest_os_ids') or self.params.get('get_config_options'): |
| 266 |
# Get supported guest ID list |
| 267 |
hardware_version = self.params.get('hardware_version', '') |
| 268 |
if hardware_version and len(support_create_list) != 0 and hardware_version not in support_create_list: |
| 269 |
self.module.fail_json(msg="Specified hardware version '%s' is not in the supported create list: %s" |
| 270 |
% (hardware_version, support_create_list)) |
| 271 |
vm_config_option_all = self.get_config_option_by_spec(env_browser=env_browser, key=hardware_version) |
| 272 |
supported_gos_list = self.get_guest_id_list(guest_os_desc=vm_config_option_all) |
| 273 |
if self.params.get('get_guest_os_ids'): |
| 274 |
results.update({vm_config_option_all.version: supported_gos_list}) |
| 275 |
|
| 276 |
if self.params.get('get_config_options') and len(guest_id) != 0: |
| 277 |
if supported_gos_list and guest_id[0] not in supported_gos_list: |
| 278 |
self.module.fail_json(msg="Specified guest ID '%s' is not in the supported guest ID list: '%s'" |
| 279 |
% (guest_id[0], supported_gos_list)) |
| 280 |
vm_config_option_guest = self.get_config_option_by_spec(env_browser=env_browser, guest_id=guest_id, |
| 281 |
key=hardware_version) |
| 282 |
guest_os_options = vm_config_option_guest.guestOSDescriptor |
| 283 |
guest_os_option_dict = self.get_config_option_recommended(guest_os_desc=guest_os_options, |
| 284 |
hwv_version=vm_config_option_guest.version) |
| 285 |
results.update({'recommended_config_options': guest_os_option_dict}) |
| 286 |
|
| 287 |
self.module.exit_json(changed=False, failed=False, instance=results) |
| 288 |
|
| 289 |
|
| 290 |
def main(): |
| 291 |
argument_spec = base_argument_spec() |
| 292 |
argument_spec.update( |
| 293 |
datacenter=dict(type='str', default='ha-datacenter'), |
| 294 |
cluster_name=dict(type='str'), |
| 295 |
esxi_hostname=dict(type='str'), |
| 296 |
get_hardware_versions=dict(type='bool', default=False), |
| 297 |
get_guest_os_ids=dict(type='bool', default=False), |
| 298 |
get_config_options=dict(type='bool', default=False), |
| 299 |
guest_id=dict(type='str'), |
| 300 |
hardware_version=dict(type='str'), |
| 301 |
) |
| 302 |
module = AnsibleModule( |
| 303 |
argument_spec=argument_spec, |
| 304 |
supports_check_mode=True, |
| 305 |
required_one_of=[ |
| 306 |
['cluster_name', 'esxi_hostname'], |
| 307 |
] |
| 308 |
) |
| 309 |
vm_config_option_guest = VmConfigOption(module) |
| 310 |
vm_config_option_guest.get_config_option_for_guest() |
| 311 |
|
| 312 |
|
| 313 |
if __name__ == "__main__": |
| 314 |
main() |