| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2019, Ansible Project |
| 5 |
# Copyright: (c) 2019, 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_guest_tools_info |
| 16 |
short_description: Gather info about VMware tools installed in VM |
| 17 |
description: |
| 18 |
- Gather information about the VMware tools installed in virtual machine. |
| 19 |
author: |
| 20 |
- Diane Wang (@Tomorrow9) <[email protected]> |
| 21 |
options: |
| 22 |
name: |
| 23 |
description: |
| 24 |
- Name of the VM to get VMware tools info. |
| 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 name is supplied. |
| 52 |
- The folder should include the datacenter. ESXi server'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 |
- The datacenter name to which virtual machine belongs to. |
| 67 |
type: str |
| 68 |
extends_documentation_fragment: |
| 69 |
- vmware.vmware.base_options |
| 70 |
|
| 71 |
''' |
| 72 |
|
| 73 |
EXAMPLES = r''' |
| 74 |
- name: Gather VMware tools info installed in VM specified by uuid |
| 75 |
community.vmware.vmware_guest_tools_info: |
| 76 |
hostname: "{{ vcenter_hostname }}" |
| 77 |
username: "{{ vcenter_username }}" |
| 78 |
password: "{{ vcenter_password }}" |
| 79 |
datacenter: "{{ datacenter_name }}" |
| 80 |
uuid: 421e4592-c069-924d-ce20-7e7533fab926 |
| 81 |
delegate_to: localhost |
| 82 |
register: vmtools_info |
| 83 |
|
| 84 |
- name: Gather VMware tools info installed in VM specified by name |
| 85 |
community.vmware.vmware_guest_tools_info: |
| 86 |
hostname: "{{ vcenter_hostname }}" |
| 87 |
username: "{{ vcenter_username }}" |
| 88 |
password: "{{ vcenter_password }}" |
| 89 |
datacenter: "{{ datacenter_name }}" |
| 90 |
name: "{{ vm_name }}" |
| 91 |
delegate_to: localhost |
| 92 |
register: vmtools_info |
| 93 |
''' |
| 94 |
|
| 95 |
RETURN = r''' |
| 96 |
vmtools_info: |
| 97 |
description: metadata about the VMware tools installed in virtual machine |
| 98 |
returned: always |
| 99 |
type: dict |
| 100 |
sample: { |
| 101 |
"vm_uuid": null, |
| 102 |
"vm_moid": null, |
| 103 |
"vm_use_instance_uuid": false, |
| 104 |
"vm_guest_fullname": "Microsoft Windows 10 (64-bit)", |
| 105 |
"vm_guest_hostname": "test", |
| 106 |
"vm_guest_id": "windows9_64Guest", |
| 107 |
"vm_hw_version": "vmx-14", |
| 108 |
"vm_ipaddress": "10.10.10.10", |
| 109 |
"vm_name": "test_vm", |
| 110 |
"vm_tools_install_type": "guestToolsTypeMSI", |
| 111 |
"vm_tools_last_install_count": 0, |
| 112 |
"vm_tools_running_status": "guestToolsRunning", |
| 113 |
"vm_tools_upgrade_policy": "manual", |
| 114 |
"vm_tools_version": 10341, |
| 115 |
"vm_tools_version_status": "guestToolsCurrent" |
| 116 |
} |
| 117 |
''' |
| 118 |
|
| 119 |
|
| 120 |
from ansible.module_utils.basic import AnsibleModule |
| 121 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi |
| 122 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 123 |
|
| 124 |
|
| 125 |
class PyVmomiHelper(PyVmomi): |
| 126 |
def __init__(self, module): |
| 127 |
super(PyVmomiHelper, self).__init__(module) |
| 128 |
self.name = self.params['name'] |
| 129 |
self.uuid = self.params['uuid'] |
| 130 |
self.moid = self.params['moid'] |
| 131 |
self.use_instance_uuid = self.params['use_instance_uuid'] |
| 132 |
|
| 133 |
def gather_vmtools_info(self): |
| 134 |
vmtools_info = dict( |
| 135 |
vm_name=self.name, |
| 136 |
vm_uuid=self.uuid, |
| 137 |
vm_moid=self.moid, |
| 138 |
vm_use_instance_uuid=self.use_instance_uuid, |
| 139 |
vm_hw_version=self.current_vm_obj.config.version, |
| 140 |
vm_guest_id=self.current_vm_obj.summary.guest.guestId, |
| 141 |
vm_guest_fullname=self.current_vm_obj.summary.guest.guestFullName, |
| 142 |
vm_guest_hostname=self.current_vm_obj.summary.guest.hostName, |
| 143 |
vm_ipaddress=self.current_vm_obj.summary.guest.ipAddress, |
| 144 |
vm_tools_running_status=self.current_vm_obj.summary.guest.toolsRunningStatus, |
| 145 |
vm_tools_version_status=self.current_vm_obj.summary.guest.toolsVersionStatus2, |
| 146 |
vm_tools_install_type=self.current_vm_obj.config.tools.toolsInstallType, |
| 147 |
vm_tools_version=self.current_vm_obj.config.tools.toolsVersion, |
| 148 |
vm_tools_upgrade_policy=self.current_vm_obj.config.tools.toolsUpgradePolicy, |
| 149 |
vm_tools_last_install_count=self.current_vm_obj.config.tools.lastInstallInfo.counter, |
| 150 |
) |
| 151 |
|
| 152 |
return {'changed': False, 'failed': False, 'vmtools_info': vmtools_info} |
| 153 |
|
| 154 |
|
| 155 |
def main(): |
| 156 |
argument_spec = base_argument_spec() |
| 157 |
argument_spec.update( |
| 158 |
name=dict(type='str'), |
| 159 |
uuid=dict(type='str'), |
| 160 |
moid=dict(type='str'), |
| 161 |
use_instance_uuid=dict(type='bool', default=False), |
| 162 |
name_match=dict( |
| 163 |
choices=['first', 'last'], |
| 164 |
default='first', |
| 165 |
type='str' |
| 166 |
), |
| 167 |
folder=dict(type='str'), |
| 168 |
datacenter=dict(type='str'), |
| 169 |
) |
| 170 |
|
| 171 |
module = AnsibleModule( |
| 172 |
argument_spec=argument_spec, |
| 173 |
required_one_of=[ |
| 174 |
['name', 'uuid', 'moid'] |
| 175 |
], |
| 176 |
mutually_exclusive=[ |
| 177 |
['name', 'uuid', 'moid'] |
| 178 |
], |
| 179 |
supports_check_mode=True, |
| 180 |
) |
| 181 |
|
| 182 |
pyv = PyVmomiHelper(module) |
| 183 |
vm = pyv.get_vm() |
| 184 |
if not vm: |
| 185 |
vm_id = (module.params.get('uuid') or module.params.get('name') or module.params.get('moid')) |
| 186 |
module.fail_json(msg='Unable to find the specified virtual machine using: %s' % vm_id) |
| 187 |
results = pyv.gather_vmtools_info() |
| 188 |
module.exit_json(**results) |
| 189 |
|
| 190 |
|
| 191 |
if __name__ == '__main__': |
| 192 |
main() |