| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2015, Joseph Callen <jcallen () csc.com> |
| 5 |
# Copyright: (c) 2018, Ansible Project |
| 6 |
# Copyright: (c) 2018, Fedor Vompe <f.vompe () comptek.ru> |
| 7 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 8 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 9 |
|
| 10 |
from __future__ import absolute_import, division, print_function |
| 11 |
__metaclass__ = type |
| 12 |
|
| 13 |
|
| 14 |
DOCUMENTATION = r''' |
| 15 |
--- |
| 16 |
module: vmware_vm_info |
| 17 |
short_description: Return basic info pertaining to a VMware machine guest |
| 18 |
description: |
| 19 |
- Return basic information pertaining to a vSphere or ESXi virtual machine guest. |
| 20 |
author: |
| 21 |
- Joseph Callen (@jcpowermac) |
| 22 |
- Abhijeet Kasurde (@Akasurde) |
| 23 |
- Fedor Vompe (@sumkincpp) |
| 24 |
options: |
| 25 |
vm_type: |
| 26 |
description: |
| 27 |
- If set to V(vm), then information are gathered for virtual machines only. |
| 28 |
- If set to V(template), then information are gathered for virtual machine templates only. |
| 29 |
- If set to V(all), then information are gathered for all virtual machines and virtual machine templates. |
| 30 |
required: false |
| 31 |
default: 'all' |
| 32 |
choices: [ all, vm, template ] |
| 33 |
type: str |
| 34 |
show_attribute: |
| 35 |
description: |
| 36 |
- Attributes related to VM guest shown in information only when this is set V(true). |
| 37 |
default: false |
| 38 |
type: bool |
| 39 |
folder: |
| 40 |
description: |
| 41 |
- Specify a folder location of VMs to gather information from. |
| 42 |
- 'Examples:' |
| 43 |
- ' folder: /ha-datacenter/vm' |
| 44 |
- ' folder: ha-datacenter/vm' |
| 45 |
- ' folder: /datacenter1/vm' |
| 46 |
- ' folder: datacenter1/vm' |
| 47 |
- ' folder: /datacenter1/vm/folder1' |
| 48 |
- ' folder: datacenter1/vm/folder1' |
| 49 |
- ' folder: /folder1/datacenter1/vm' |
| 50 |
- ' folder: folder1/datacenter1/vm' |
| 51 |
- ' folder: /folder1/datacenter1/vm/folder2' |
| 52 |
type: str |
| 53 |
show_cluster: |
| 54 |
description: |
| 55 |
- Tags virtual machine's cluster is shown if set to V(true). |
| 56 |
version_added: '3.5.0' |
| 57 |
default: true |
| 58 |
type: bool |
| 59 |
show_datacenter: |
| 60 |
description: |
| 61 |
- Tags virtual machine's datacenter is shown if set to V(true). |
| 62 |
version_added: '3.5.0' |
| 63 |
default: true |
| 64 |
type: bool |
| 65 |
show_datastore: |
| 66 |
description: |
| 67 |
- Tags virtual machine's datastore is shown if set to V(true). |
| 68 |
version_added: '3.5.0' |
| 69 |
default: true |
| 70 |
type: bool |
| 71 |
show_esxi_hostname: |
| 72 |
description: |
| 73 |
- Tags virtual machine's ESXi host is shown if set to V(true). |
| 74 |
version_added: '3.5.0' |
| 75 |
default: true |
| 76 |
type: bool |
| 77 |
show_folder: |
| 78 |
description: |
| 79 |
- Show folders |
| 80 |
version_added: '3.7.0' |
| 81 |
default: true |
| 82 |
type: bool |
| 83 |
show_mac_address: |
| 84 |
description: |
| 85 |
- Tags virtual machine's mac address is shown if set to V(true). |
| 86 |
version_added: '3.5.0' |
| 87 |
default: true |
| 88 |
type: bool |
| 89 |
show_net: |
| 90 |
description: |
| 91 |
- Tags virtual machine's network is shown if set to V(true). |
| 92 |
version_added: '3.5.0' |
| 93 |
default: true |
| 94 |
type: bool |
| 95 |
show_resource_pool: |
| 96 |
description: |
| 97 |
- Tags virtual machine's resource pool is shown if set to V(true). |
| 98 |
version_added: '3.5.0' |
| 99 |
default: true |
| 100 |
type: bool |
| 101 |
show_tag: |
| 102 |
description: |
| 103 |
- Tags related to virtual machine are shown if set to V(true). |
| 104 |
default: false |
| 105 |
type: bool |
| 106 |
show_allocated: |
| 107 |
description: |
| 108 |
- Allocated storage in byte and memory in MB are shown if it set to True. |
| 109 |
default: false |
| 110 |
type: bool |
| 111 |
vm_name: |
| 112 |
description: |
| 113 |
- Name of the virtual machine to get related configurations information from. |
| 114 |
type: str |
| 115 |
extends_documentation_fragment: |
| 116 |
- vmware.vmware.base_options |
| 117 |
''' |
| 118 |
|
| 119 |
EXAMPLES = r''' |
| 120 |
- name: Gather all registered virtual machines |
| 121 |
community.vmware.vmware_vm_info: |
| 122 |
hostname: '{{ vcenter_hostname }}' |
| 123 |
username: '{{ vcenter_username }}' |
| 124 |
password: '{{ vcenter_password }}' |
| 125 |
delegate_to: localhost |
| 126 |
register: vm_info |
| 127 |
|
| 128 |
- debug: |
| 129 |
var: vm_info.virtual_machines |
| 130 |
|
| 131 |
- name: Gather one specific VM |
| 132 |
community.vmware.vmware_vm_info: |
| 133 |
hostname: '{{ vcenter_hostname }}' |
| 134 |
username: '{{ vcenter_username }}' |
| 135 |
password: '{{ vcenter_password }}' |
| 136 |
vm_name: 'vm_name_as_per_vcenter' |
| 137 |
delegate_to: localhost |
| 138 |
register: vm_info |
| 139 |
|
| 140 |
- debug: |
| 141 |
var: vm_info.virtual_machines |
| 142 |
|
| 143 |
- name: Gather only registered virtual machine templates |
| 144 |
community.vmware.vmware_vm_info: |
| 145 |
hostname: '{{ vcenter_hostname }}' |
| 146 |
username: '{{ vcenter_username }}' |
| 147 |
password: '{{ vcenter_password }}' |
| 148 |
vm_type: template |
| 149 |
delegate_to: localhost |
| 150 |
register: template_info |
| 151 |
|
| 152 |
- debug: |
| 153 |
var: template_info.virtual_machines |
| 154 |
|
| 155 |
- name: Gather only registered virtual machines |
| 156 |
community.vmware.vmware_vm_info: |
| 157 |
hostname: '{{ vcenter_hostname }}' |
| 158 |
username: '{{ vcenter_username }}' |
| 159 |
password: '{{ vcenter_password }}' |
| 160 |
vm_type: vm |
| 161 |
delegate_to: localhost |
| 162 |
register: vm_info |
| 163 |
|
| 164 |
- debug: |
| 165 |
var: vm_info.virtual_machines |
| 166 |
|
| 167 |
- name: Get UUID from given VM Name |
| 168 |
block: |
| 169 |
- name: Get virtual machine info |
| 170 |
community.vmware.vmware_vm_info: |
| 171 |
hostname: '{{ vcenter_hostname }}' |
| 172 |
username: '{{ vcenter_username }}' |
| 173 |
password: '{{ vcenter_password }}' |
| 174 |
folder: "/datacenter/vm/folder" |
| 175 |
delegate_to: localhost |
| 176 |
register: vm_info |
| 177 |
|
| 178 |
- debug: |
| 179 |
msg: "{{ item.uuid }}" |
| 180 |
with_items: |
| 181 |
- "{{ vm_info.virtual_machines | community.general.json_query(query) }}" |
| 182 |
vars: |
| 183 |
query: "[?guest_name=='DC0_H0_VM0']" |
| 184 |
|
| 185 |
- name: Get Tags from given VM Name |
| 186 |
block: |
| 187 |
- name: Get virtual machine info |
| 188 |
community.vmware.vmware_vm_info: |
| 189 |
hostname: '{{ vcenter_hostname }}' |
| 190 |
username: '{{ vcenter_username }}' |
| 191 |
password: '{{ vcenter_password }}' |
| 192 |
folder: "/datacenter/vm/folder" |
| 193 |
delegate_to: localhost |
| 194 |
register: vm_info |
| 195 |
|
| 196 |
- debug: |
| 197 |
msg: "{{ item.tags }}" |
| 198 |
with_items: |
| 199 |
- "{{ vm_info.virtual_machines | community.general.json_query(query) }}" |
| 200 |
vars: |
| 201 |
query: "[?guest_name=='DC0_H0_VM0']" |
| 202 |
|
| 203 |
- name: Gather all VMs from a specific folder |
| 204 |
community.vmware.vmware_vm_info: |
| 205 |
hostname: '{{ vcenter_hostname }}' |
| 206 |
username: '{{ vcenter_username }}' |
| 207 |
password: '{{ vcenter_password }}' |
| 208 |
folder: "/Asia-Datacenter1/vm/prod" |
| 209 |
delegate_to: localhost |
| 210 |
register: vm_info |
| 211 |
|
| 212 |
- name: Get datastore_url from given VM name |
| 213 |
block: |
| 214 |
- name: Get virtual machine info |
| 215 |
community.vmware.vmware_vm_info: |
| 216 |
hostname: '{{ vcenter_hostname }}' |
| 217 |
username: '{{ vcenter_username }}' |
| 218 |
password: '{{ vcenter_password }}' |
| 219 |
delegate_to: localhost |
| 220 |
register: vm_info |
| 221 |
|
| 222 |
- debug: |
| 223 |
msg: "{{ item.datastore_url }}" |
| 224 |
with_items: |
| 225 |
- "{{ vm_info.virtual_machines | community.general.json_query(query) }}" |
| 226 |
vars: |
| 227 |
query: "[?guest_name=='DC0_H0_VM0']" |
| 228 |
''' |
| 229 |
|
| 230 |
RETURN = r''' |
| 231 |
virtual_machines: |
| 232 |
description: list of dictionary of virtual machines and their information |
| 233 |
returned: success |
| 234 |
type: list |
| 235 |
sample: [ |
| 236 |
{ |
| 237 |
"guest_name": "ubuntu_t", |
| 238 |
"datacenter": "Datacenter-1", |
| 239 |
"cluster": null, |
| 240 |
"esxi_hostname": "10.76.33.226", |
| 241 |
"folder": "/Datacenter-1/vm", |
| 242 |
"guest_fullname": "Ubuntu Linux (64-bit)", |
| 243 |
"ip_address": "", |
| 244 |
"mac_address": [ |
| 245 |
"00:50:56:87:a5:9a" |
| 246 |
], |
| 247 |
"power_state": "poweredOff", |
| 248 |
"uuid": "4207072c-edd8-3bd5-64dc-903fd3a0db04", |
| 249 |
"vm_network": { |
| 250 |
"00:50:56:87:a5:9a": { |
| 251 |
"ipv4": [ |
| 252 |
"10.76.33.228/24" |
| 253 |
], |
| 254 |
"ipv6": [] |
| 255 |
} |
| 256 |
}, |
| 257 |
"attributes": { |
| 258 |
"job": "backup-prepare" |
| 259 |
}, |
| 260 |
"datastore_url": [ |
| 261 |
{ |
| 262 |
"name": "t880-o2g", |
| 263 |
"url": "/vmfs/volumes/e074264a-e5c82a58" |
| 264 |
} |
| 265 |
], |
| 266 |
"tags": [ |
| 267 |
{ |
| 268 |
"category_id": "urn:vmomi:InventoryServiceCategory:b316cc45-f1a9-4277-811d-56c7e7975203:GLOBAL", |
| 269 |
"category_name": "cat_0001", |
| 270 |
"description": "", |
| 271 |
"id": "urn:vmomi:InventoryServiceTag:43737ec0-b832-4abf-abb1-fd2448ce3b26:GLOBAL", |
| 272 |
"name": "tag_0001" |
| 273 |
} |
| 274 |
], |
| 275 |
"moid": "vm-24", |
| 276 |
"allocated": { |
| 277 |
"storage": 500000000, |
| 278 |
"cpu": 2, |
| 279 |
"memory": 16 |
| 280 |
}, |
| 281 |
} |
| 282 |
] |
| 283 |
''' |
| 284 |
|
| 285 |
try: |
| 286 |
from pyVmomi import vim |
| 287 |
except ImportError: |
| 288 |
pass |
| 289 |
|
| 290 |
from ansible.module_utils.basic import AnsibleModule |
| 291 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi, \ |
| 292 |
get_all_objs, _get_vm_prop, get_parent_datacenter, find_vm_by_name |
| 293 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 294 |
from ansible_collections.community.vmware.plugins.module_utils.vmware_rest_client import VmwareRestClient |
| 295 |
|
| 296 |
|
| 297 |
class VmwareVmInfo(PyVmomi): |
| 298 |
def __init__(self, module): |
| 299 |
super(VmwareVmInfo, self).__init__(module) |
| 300 |
if self.module.params.get('show_tag'): |
| 301 |
self.vmware_client = VmwareRestClient(self.module) |
| 302 |
|
| 303 |
def get_tag_info(self, vm_dynamic_obj): |
| 304 |
return self.vmware_client.get_tags_for_vm(vm_mid=vm_dynamic_obj._moId) |
| 305 |
|
| 306 |
def get_vm_attributes(self, vm): |
| 307 |
custom_field_values = vm.customValue |
| 308 |
custom_field_mgr = self.custom_field_mgr |
| 309 |
vm_attributes = {} |
| 310 |
for custom_field in custom_field_mgr: |
| 311 |
for custom_value in custom_field_values: |
| 312 |
if custom_field.key == custom_value.key: |
| 313 |
vm_attributes[custom_field.name] = custom_value.value |
| 314 |
return vm_attributes |
| 315 |
|
| 316 |
# https://github.com/vmware/pyvmomi-community-samples/blob/master/samples/getallvms.py |
| 317 |
def get_virtual_machines(self): |
| 318 |
""" |
| 319 |
Get one/all virtual machines and related configurations information. |
| 320 |
""" |
| 321 |
folder = self.params.get('folder') |
| 322 |
folder_obj = None |
| 323 |
if folder: |
| 324 |
folder_obj = self.content.searchIndex.FindByInventoryPath(folder) |
| 325 |
if not folder_obj: |
| 326 |
self.module.fail_json(msg="Failed to find folder specified by %(folder)s" % self.params) |
| 327 |
|
| 328 |
vm_name = self.params.get('vm_name') |
| 329 |
if vm_name: |
| 330 |
virtual_machine = find_vm_by_name(self.content, vm_name=vm_name, folder=folder_obj) |
| 331 |
if not virtual_machine: |
| 332 |
self.module.fail_json(msg="Failed to find virtual machine %s" % vm_name) |
| 333 |
else: |
| 334 |
virtual_machines = [virtual_machine] |
| 335 |
else: |
| 336 |
virtual_machines = get_all_objs(self.content, [vim.VirtualMachine], folder=folder_obj) |
| 337 |
_virtual_machines = [] |
| 338 |
|
| 339 |
for vm in virtual_machines: |
| 340 |
_ip_address = "" |
| 341 |
summary = vm.summary |
| 342 |
if summary.guest is not None: |
| 343 |
_ip_address = summary.guest.ipAddress |
| 344 |
if _ip_address is None: |
| 345 |
_ip_address = "" |
| 346 |
_mac_address = [] |
| 347 |
if self.module.params.get('show_mac_address'): |
| 348 |
all_devices = _get_vm_prop(vm, ('config', 'hardware', 'device')) |
| 349 |
if all_devices: |
| 350 |
for dev in all_devices: |
| 351 |
if isinstance(dev, vim.vm.device.VirtualEthernetCard): |
| 352 |
_mac_address.append(dev.macAddress) |
| 353 |
|
| 354 |
net_dict = {} |
| 355 |
if self.module.params.get('show_net'): |
| 356 |
vmnet = _get_vm_prop(vm, ('guest', 'net')) |
| 357 |
if vmnet: |
| 358 |
for device in vmnet: |
| 359 |
net_dict[device.macAddress] = dict() |
| 360 |
net_dict[device.macAddress]['ipv4'] = [] |
| 361 |
net_dict[device.macAddress]['ipv6'] = [] |
| 362 |
if device.ipConfig is not None: |
| 363 |
for ip_addr in device.ipConfig.ipAddress: |
| 364 |
if "::" in ip_addr.ipAddress: |
| 365 |
net_dict[device.macAddress]['ipv6'].append(ip_addr.ipAddress + "/" + str(ip_addr.prefixLength)) |
| 366 |
else: |
| 367 |
net_dict[device.macAddress]['ipv4'].append(ip_addr.ipAddress + "/" + str(ip_addr.prefixLength)) |
| 368 |
|
| 369 |
esxi_hostname = None |
| 370 |
esxi_parent = None |
| 371 |
|
| 372 |
if self.module.params.get('show_esxi_hostname') or self.module.params.get('show_cluster'): |
| 373 |
if summary.runtime.host: |
| 374 |
esxi_hostname = summary.runtime.host.summary.config.name |
| 375 |
esxi_parent = summary.runtime.host.parent |
| 376 |
|
| 377 |
cluster_name = None |
| 378 |
if self.module.params.get('show_cluster'): |
| 379 |
if esxi_parent and isinstance(esxi_parent, vim.ClusterComputeResource): |
| 380 |
cluster_name = summary.runtime.host.parent.name |
| 381 |
|
| 382 |
resource_pool = None |
| 383 |
if self.module.params.get('show_resource_pool'): |
| 384 |
if vm.resourcePool and vm.resourcePool != vm.resourcePool.owner.resourcePool: |
| 385 |
resource_pool = vm.resourcePool.name |
| 386 |
|
| 387 |
vm_attributes = dict() |
| 388 |
if self.module.params.get('show_attribute'): |
| 389 |
vm_attributes = self.get_vm_attributes(vm) |
| 390 |
|
| 391 |
vm_tags = list() |
| 392 |
if self.module.params.get('show_tag'): |
| 393 |
vm_tags = self.get_tag_info(vm) |
| 394 |
|
| 395 |
allocated = {} |
| 396 |
if self.module.params.get('show_allocated'): |
| 397 |
storage_allocated = 0 |
| 398 |
for device in vm.config.hardware.device: |
| 399 |
if isinstance(device, vim.vm.device.VirtualDisk): |
| 400 |
storage_allocated += device.capacityInBytes |
| 401 |
allocated = { |
| 402 |
"storage": storage_allocated, |
| 403 |
"cpu": vm.config.hardware.numCPU, |
| 404 |
"memory": vm.config.hardware.memoryMB} |
| 405 |
|
| 406 |
vm_folder = None |
| 407 |
if self.module.params.get('show_folder'): |
| 408 |
vm_folder = PyVmomi.get_vm_path(content=self.content, vm_name=vm) |
| 409 |
|
| 410 |
datacenter = None |
| 411 |
if self.module.params.get('show_datacenter'): |
| 412 |
datacenter = get_parent_datacenter(vm) |
| 413 |
datastore_url = list() |
| 414 |
if self.module.params.get('show_datastore'): |
| 415 |
datastore_attributes = ('name', 'url') |
| 416 |
vm_datastore_urls = _get_vm_prop(vm, ('config', 'datastoreUrl')) |
| 417 |
if vm_datastore_urls: |
| 418 |
for entry in vm_datastore_urls: |
| 419 |
datastore_url.append({key: getattr(entry, key) for key in dir(entry) if key in datastore_attributes}) |
| 420 |
virtual_machine = { |
| 421 |
"guest_name": summary.config.name, |
| 422 |
"guest_fullname": summary.config.guestFullName, |
| 423 |
"power_state": summary.runtime.powerState, |
| 424 |
"ip_address": _ip_address, # Kept for backward compatibility |
| 425 |
"mac_address": _mac_address, # Kept for backward compatibility |
| 426 |
"uuid": summary.config.uuid, |
| 427 |
"instance_uuid": summary.config.instanceUuid, |
| 428 |
"vm_network": net_dict, |
| 429 |
"esxi_hostname": esxi_hostname, |
| 430 |
"datacenter": None if datacenter is None else datacenter.name, |
| 431 |
"cluster": cluster_name, |
| 432 |
"resource_pool": resource_pool, |
| 433 |
"attributes": vm_attributes, |
| 434 |
"tags": vm_tags, |
| 435 |
"folder": vm_folder, |
| 436 |
"moid": vm._moId, |
| 437 |
"datastore_url": datastore_url, |
| 438 |
"allocated": allocated |
| 439 |
} |
| 440 |
|
| 441 |
vm_type = self.module.params.get('vm_type') |
| 442 |
is_template = _get_vm_prop(vm, ('config', 'template')) |
| 443 |
if vm_type == 'vm' and not is_template: |
| 444 |
_virtual_machines.append(virtual_machine) |
| 445 |
elif vm_type == 'template' and is_template: |
| 446 |
_virtual_machines.append(virtual_machine) |
| 447 |
elif vm_type == 'all': |
| 448 |
_virtual_machines.append(virtual_machine) |
| 449 |
return _virtual_machines |
| 450 |
|
| 451 |
|
| 452 |
def main(): |
| 453 |
argument_spec = base_argument_spec() |
| 454 |
argument_spec.update( |
| 455 |
vm_type=dict(type='str', choices=['vm', 'all', 'template'], default='all'), |
| 456 |
show_attribute=dict(type='bool', default='no'), |
| 457 |
show_cluster=dict(type='bool', default=True), |
| 458 |
show_datacenter=dict(type='bool', default=True), |
| 459 |
show_datastore=dict(type='bool', default=True), |
| 460 |
show_folder=dict(type='bool', default=True), |
| 461 |
show_esxi_hostname=dict(type='bool', default=True), |
| 462 |
show_mac_address=dict(type='bool', default=True), |
| 463 |
show_net=dict(type='bool', default=True), |
| 464 |
show_resource_pool=dict(type='bool', default=True), |
| 465 |
show_tag=dict(type='bool', default=False), |
| 466 |
show_allocated=dict(type='bool', default=False), |
| 467 |
folder=dict(type='str'), |
| 468 |
vm_name=dict(type='str') |
| 469 |
) |
| 470 |
|
| 471 |
module = AnsibleModule( |
| 472 |
argument_spec=argument_spec, |
| 473 |
supports_check_mode=True |
| 474 |
) |
| 475 |
|
| 476 |
vmware_vm_info = VmwareVmInfo(module) |
| 477 |
_virtual_machines = vmware_vm_info.get_virtual_machines() |
| 478 |
|
| 479 |
module.exit_json(changed=False, virtual_machines=_virtual_machines) |
| 480 |
|
| 481 |
|
| 482 |
if __name__ == '__main__': |
| 483 |
main() |