| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# This module is also sponsored by E.T.A.I. (www.etai.fr) |
| 5 |
# Copyright (C) 2018 James E. King III (@jeking3) <[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_info |
| 16 |
short_description: Gather info about a single VM |
| 17 |
description: |
| 18 |
- Gather information about a single VM on a VMware ESX cluster. |
| 19 |
author: |
| 20 |
- Loic Blot (@nerzhul) <[email protected]> |
| 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 |
name_match: |
| 28 |
description: |
| 29 |
- If multiple VMs matching the name, use the first or last found |
| 30 |
default: 'first' |
| 31 |
choices: ['first', 'last'] |
| 32 |
type: str |
| 33 |
uuid: |
| 34 |
description: |
| 35 |
- UUID of the instance to manage if known, this is VMware's unique identifier. |
| 36 |
- This is required if O(name) or O(moid) 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 |
moid: |
| 44 |
description: |
| 45 |
- Managed Object ID of the instance to manage if known, this is a unique identifier only within a single vCenter instance. |
| 46 |
- This is required if O(name) or O(uuid) is not supplied. |
| 47 |
type: str |
| 48 |
folder: |
| 49 |
description: |
| 50 |
- Destination folder, absolute or relative path to find an existing guest. |
| 51 |
- This is required if O(name) is supplied and not unique. |
| 52 |
- The folder should include the datacenter. ESX's datacenter is ha-datacenter |
| 53 |
- 'Examples:' |
| 54 |
- ' folder: /ha-datacenter/vm' |
| 55 |
- ' folder: ha-datacenter/vm' |
| 56 |
- ' folder: /datacenter1/vm' |
| 57 |
- ' folder: datacenter1/vm' |
| 58 |
- ' folder: /datacenter1/vm/folder1' |
| 59 |
- ' folder: datacenter1/vm/folder1' |
| 60 |
- ' folder: /folder1/datacenter1/vm' |
| 61 |
- ' folder: folder1/datacenter1/vm' |
| 62 |
- ' folder: /folder1/datacenter1/vm/folder2' |
| 63 |
type: str |
| 64 |
datacenter: |
| 65 |
description: |
| 66 |
- Destination datacenter for the deploy operation |
| 67 |
- This is required if O(name) is supplied and not unique. |
| 68 |
required: false |
| 69 |
type: str |
| 70 |
tags: |
| 71 |
description: |
| 72 |
- Whether to show tags or not. |
| 73 |
- If set V(true), shows tags information. Returns a list of tag names. |
| 74 |
- If set V(false), hides tags information. |
| 75 |
- vSphere Automation SDK is required. |
| 76 |
default: false |
| 77 |
type: bool |
| 78 |
tag_details: |
| 79 |
description: |
| 80 |
- If set V(true), detail information about tags returned. |
| 81 |
- Without this flag, O(tags=true) returns a list of tag names. |
| 82 |
- With this flag, O(tags=true) returns a list of dict about tag information with additional details like category name, category id, and tag id. |
| 83 |
- This parameter is added to maintain backward compatability. |
| 84 |
default: false |
| 85 |
type: bool |
| 86 |
schema: |
| 87 |
description: |
| 88 |
- Specify the output schema desired. |
| 89 |
- The V(summary) output schema is the legacy output from the module |
| 90 |
- The V(vsphere) output schema is the vSphere API class definition |
| 91 |
choices: ['summary', 'vsphere'] |
| 92 |
default: 'summary' |
| 93 |
type: str |
| 94 |
properties: |
| 95 |
description: |
| 96 |
- Specify the properties to retrieve. |
| 97 |
- If not specified, all properties are retrieved (deeply). |
| 98 |
- Results are returned in a structure identical to the vsphere API. |
| 99 |
- 'Example:' |
| 100 |
- ' properties: [' |
| 101 |
- ' "config.hardware.memoryMB",' |
| 102 |
- ' "config.hardware.numCPU",' |
| 103 |
- ' "guest.disk",' |
| 104 |
- ' "overallStatus"' |
| 105 |
- ' ]' |
| 106 |
- Only valid when O(schema=vsphere). |
| 107 |
type: list |
| 108 |
elements: str |
| 109 |
required: false |
| 110 |
extends_documentation_fragment: |
| 111 |
- vmware.vmware.base_options |
| 112 |
|
| 113 |
''' |
| 114 |
|
| 115 |
EXAMPLES = r''' |
| 116 |
- name: Gather info from standalone ESXi server having datacenter as 'ha-datacenter' |
| 117 |
community.vmware.vmware_guest_info: |
| 118 |
hostname: "{{ vcenter_hostname }}" |
| 119 |
username: "{{ vcenter_username }}" |
| 120 |
password: "{{ vcenter_password }}" |
| 121 |
datacenter: ha-datacenter |
| 122 |
uuid: 421e4592-c069-924d-ce20-7e7533fab926 |
| 123 |
delegate_to: localhost |
| 124 |
register: info |
| 125 |
|
| 126 |
- name: Gather some info from a guest using the vSphere API output schema |
| 127 |
community.vmware.vmware_guest_info: |
| 128 |
hostname: "{{ vcenter_hostname }}" |
| 129 |
username: "{{ vcenter_username }}" |
| 130 |
password: "{{ vcenter_password }}" |
| 131 |
datacenter: "{{ datacenter_name }}" |
| 132 |
name: "{{ vm_name }}" |
| 133 |
schema: "vsphere" |
| 134 |
properties: ["config.hardware.memoryMB", "guest.disk", "overallStatus"] |
| 135 |
delegate_to: localhost |
| 136 |
register: info |
| 137 |
|
| 138 |
- name: Gather some information about a guest using MoID |
| 139 |
community.vmware.vmware_guest_info: |
| 140 |
hostname: "{{ vcenter_hostname }}" |
| 141 |
username: "{{ vcenter_username }}" |
| 142 |
password: "{{ vcenter_password }}" |
| 143 |
datacenter: "{{ datacenter_name }}" |
| 144 |
moid: vm-42 |
| 145 |
schema: "vsphere" |
| 146 |
properties: ["config.hardware.memoryMB", "guest.disk", "overallStatus"] |
| 147 |
delegate_to: localhost |
| 148 |
register: vm_moid_info |
| 149 |
|
| 150 |
- name: Gather Managed object ID (moid) from a guest using the vSphere API output schema for REST Calls |
| 151 |
community.vmware.vmware_guest_info: |
| 152 |
hostname: "{{ vcenter_hostname }}" |
| 153 |
username: "{{ vcenter_username }}" |
| 154 |
password: "{{ vcenter_password }}" |
| 155 |
datacenter: "{{ datacenter_name }}" |
| 156 |
name: "{{ vm_name }}" |
| 157 |
schema: "vsphere" |
| 158 |
properties: |
| 159 |
- _moId |
| 160 |
delegate_to: localhost |
| 161 |
register: moid_info |
| 162 |
|
| 163 |
- name: Gather detailed information about tags and category associated with the given VM |
| 164 |
community.vmware.vmware_guest_info: |
| 165 |
hostname: "{{ vcenter_hostname }}" |
| 166 |
username: "{{ vcenter_username }}" |
| 167 |
password: "{{ vcenter_password }}" |
| 168 |
datacenter: "{{ datacenter_name }}" |
| 169 |
name: "{{ vm_name }}" |
| 170 |
tags: true |
| 171 |
tag_details: true |
| 172 |
register: detailed_tag_info |
| 173 |
''' |
| 174 |
|
| 175 |
RETURN = r''' |
| 176 |
instance: |
| 177 |
description: metadata about the virtual machine |
| 178 |
returned: always |
| 179 |
type: dict |
| 180 |
sample: { |
| 181 |
"advanced_settings": {}, |
| 182 |
"annotation": "", |
| 183 |
"current_snapshot": null, |
| 184 |
"customvalues": {}, |
| 185 |
"guest_consolidation_needed": false, |
| 186 |
"guest_question": null, |
| 187 |
"guest_tools_status": "guestToolsNotRunning", |
| 188 |
"guest_tools_version": "10247", |
| 189 |
"hw_cores_per_socket": 1, |
| 190 |
"hw_datastores": [ |
| 191 |
"ds_226_3" |
| 192 |
], |
| 193 |
"hw_esxi_host": "10.76.33.226", |
| 194 |
"hw_eth0": { |
| 195 |
"addresstype": "assigned", |
| 196 |
"ipaddresses": null, |
| 197 |
"label": "Network adapter 1", |
| 198 |
"macaddress": "00:50:56:87:a5:9a", |
| 199 |
"macaddress_dash": "00-50-56-87-a5-9a", |
| 200 |
"portgroup_key": null, |
| 201 |
"portgroup_portkey": null, |
| 202 |
"summary": "VM Network" |
| 203 |
}, |
| 204 |
"hw_files": [ |
| 205 |
"[ds_226_3] ubuntu_t/ubuntu_t.vmx", |
| 206 |
"[ds_226_3] ubuntu_t/ubuntu_t.nvram", |
| 207 |
"[ds_226_3] ubuntu_t/ubuntu_t.vmsd", |
| 208 |
"[ds_226_3] ubuntu_t/vmware.log", |
| 209 |
"[ds_226_3] u0001/u0001.vmdk" |
| 210 |
], |
| 211 |
"hw_folder": "/DC0/vm/Discovered virtual machine", |
| 212 |
"hw_guest_full_name": null, |
| 213 |
"hw_guest_ha_state": null, |
| 214 |
"hw_guest_id": null, |
| 215 |
"hw_interfaces": [ |
| 216 |
"eth0" |
| 217 |
], |
| 218 |
"hw_is_template": false, |
| 219 |
"hw_memtotal_mb": 1024, |
| 220 |
"hw_name": "ubuntu_t", |
| 221 |
"hw_power_status": "poweredOff", |
| 222 |
"hw_processor_count": 1, |
| 223 |
"hw_product_uuid": "4207072c-edd8-3bd5-64dc-903fd3a0db04", |
| 224 |
"hw_version": "vmx-13", |
| 225 |
"instance_uuid": "5007769d-add3-1e12-f1fe-225ae2a07caf", |
| 226 |
"ipv4": null, |
| 227 |
"ipv6": null, |
| 228 |
"module_hw": true, |
| 229 |
"snapshots": [], |
| 230 |
"tags": [ |
| 231 |
"backup" |
| 232 |
], |
| 233 |
"vnc": {}, |
| 234 |
"moid": "vm-42", |
| 235 |
"vimref": "vim.VirtualMachine:vm-42" |
| 236 |
} |
| 237 |
''' |
| 238 |
|
| 239 |
from ansible.module_utils.basic import AnsibleModule |
| 240 |
from ansible.module_utils.common.text.converters import to_text |
| 241 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi |
| 242 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 243 |
from ansible_collections.community.vmware.plugins.module_utils.vmware_rest_client import VmwareRestClient |
| 244 |
try: |
| 245 |
from com.vmware.vapi.std_client import DynamicID # noqa: F401 |
| 246 |
HAS_VSPHERE = True |
| 247 |
except ImportError: |
| 248 |
HAS_VSPHERE = False |
| 249 |
|
| 250 |
|
| 251 |
class VmwareTag(VmwareRestClient): |
| 252 |
def __init__(self, module): |
| 253 |
super(VmwareTag, self).__init__(module) |
| 254 |
self.tag_service = self.api_client.tagging.Tag |
| 255 |
self.tag_association_svc = self.api_client.tagging.TagAssociation |
| 256 |
|
| 257 |
|
| 258 |
def main(): |
| 259 |
argument_spec = base_argument_spec() |
| 260 |
argument_spec.update( |
| 261 |
name=dict(type='str'), |
| 262 |
name_match=dict(type='str', choices=['first', 'last'], default='first'), |
| 263 |
uuid=dict(type='str'), |
| 264 |
use_instance_uuid=dict(type='bool', default=False), |
| 265 |
moid=dict(type='str'), |
| 266 |
folder=dict(type='str'), |
| 267 |
datacenter=dict(type='str'), |
| 268 |
tags=dict(type='bool', default=False), |
| 269 |
schema=dict(type='str', choices=['summary', 'vsphere'], default='summary'), |
| 270 |
properties=dict(type='list', elements='str'), |
| 271 |
tag_details=dict(type='bool', default=False), |
| 272 |
) |
| 273 |
module = AnsibleModule(argument_spec=argument_spec, |
| 274 |
required_one_of=[['name', 'uuid', 'moid']], |
| 275 |
supports_check_mode=True) |
| 276 |
|
| 277 |
if module.params.get('folder'): |
| 278 |
# FindByInventoryPath() does not require an absolute path |
| 279 |
# so we should leave the input folder path unmodified |
| 280 |
module.params['folder'] = module.params['folder'].rstrip('/') |
| 281 |
|
| 282 |
if module.params['schema'] != 'vsphere' and module.params.get('properties'): |
| 283 |
module.fail_json(msg="The option 'properties' is only valid when the schema is 'vsphere'") |
| 284 |
|
| 285 |
pyv = PyVmomi(module) |
| 286 |
# Check if the VM exists before continuing |
| 287 |
vm = pyv.get_vm() |
| 288 |
|
| 289 |
# VM already exists |
| 290 |
if vm: |
| 291 |
try: |
| 292 |
if module.params['schema'] == 'summary': |
| 293 |
instance = pyv.gather_facts(vm) |
| 294 |
else: |
| 295 |
instance = pyv.to_json(vm, module.params['properties']) |
| 296 |
if module.params.get('tags'): |
| 297 |
if not HAS_VSPHERE: |
| 298 |
module.fail_json(msg="Unable to find 'vCloud Suite SDK' Python library which is required." |
| 299 |
" Please refer this URL for installation steps" |
| 300 |
" - https://code.vmware.com/web/sdk/vsphere-automation-python") |
| 301 |
|
| 302 |
vm_rest_client = VmwareTag(module) |
| 303 |
tags = [] |
| 304 |
if module.params.get('tag_details'): |
| 305 |
tags = vm_rest_client.get_tags_for_vm(vm_mid=vm._moId) |
| 306 |
else: |
| 307 |
dynamic_obj = DynamicID(type='VirtualMachine', id=vm._moId) |
| 308 |
tags = vm_rest_client.get_vm_tags(vm_rest_client.tag_service, |
| 309 |
vm_rest_client.tag_association_svc, |
| 310 |
vm_mid=dynamic_obj) |
| 311 |
instance.update(tags=tags) |
| 312 |
module.exit_json(instance=instance) |
| 313 |
except Exception as exc: |
| 314 |
module.fail_json(msg="Information gathering failed with exception %s" % to_text(exc)) |
| 315 |
else: |
| 316 |
vm_id = (module.params.get('uuid') or module.params.get('name') or module.params.get('moid')) |
| 317 |
module.fail_json(msg="Unable to gather information for non-existing VM %s" % vm_id) |
| 318 |
|
| 319 |
|
| 320 |
if __name__ == '__main__': |
| 321 |
main() |