| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2017, Philippe Dellaert <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
from __future__ import (absolute_import, division, print_function) |
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = r''' |
| 13 |
--- |
| 14 |
module: vmware_guest_tools_wait |
| 15 |
short_description: Wait for VMware tools to become available |
| 16 |
description: |
| 17 |
- This module can be used to wait for VMware tools to become available on the given VM and return facts. |
| 18 |
author: |
| 19 |
- Philippe Dellaert (@pdellaert) <[email protected]> |
| 20 |
options: |
| 21 |
name: |
| 22 |
description: |
| 23 |
- Name of the VM for which to wait until the tools become available. |
| 24 |
- This is required if O(uuid) or O(moid) is not supplied. |
| 25 |
type: str |
| 26 |
name_match: |
| 27 |
description: |
| 28 |
- If multiple VMs match the name, use the first or last found. |
| 29 |
default: 'first' |
| 30 |
choices: ['first', 'last'] |
| 31 |
type: str |
| 32 |
folder: |
| 33 |
description: |
| 34 |
- Destination folder, absolute or relative path to find an existing guest. |
| 35 |
- This is required only, if multiple VMs with same O(name) is found. |
| 36 |
- The folder should include the datacenter. ESX's datacenter is C(ha-datacenter). |
| 37 |
- 'Examples:' |
| 38 |
- ' folder: /ha-datacenter/vm' |
| 39 |
- ' folder: ha-datacenter/vm' |
| 40 |
- ' folder: /datacenter1/vm' |
| 41 |
- ' folder: datacenter1/vm' |
| 42 |
- ' folder: /datacenter1/vm/folder1' |
| 43 |
- ' folder: datacenter1/vm/folder1' |
| 44 |
- ' folder: /folder1/datacenter1/vm' |
| 45 |
- ' folder: folder1/datacenter1/vm' |
| 46 |
- ' folder: /folder1/datacenter1/vm/folder2' |
| 47 |
type: str |
| 48 |
uuid: |
| 49 |
description: |
| 50 |
- UUID of the VM for which to wait until the tools become available, if known. This is VMware's unique identifier. |
| 51 |
- This is required, if O(name) or O(moid) is not supplied. |
| 52 |
type: str |
| 53 |
moid: |
| 54 |
description: |
| 55 |
- Managed Object ID of the instance to manage if known, this is a unique identifier only within a single vCenter instance. |
| 56 |
- This is required if O(name) or O(uuid) is not supplied. |
| 57 |
type: str |
| 58 |
use_instance_uuid: |
| 59 |
description: |
| 60 |
- Whether to use the VMware instance UUID rather than the BIOS UUID. |
| 61 |
default: false |
| 62 |
type: bool |
| 63 |
timeout: |
| 64 |
description: |
| 65 |
- Max duration of the waiting period (seconds). |
| 66 |
default: 500 |
| 67 |
type: int |
| 68 |
datacenter: |
| 69 |
description: |
| 70 |
- Name of the datacenter. |
| 71 |
- The datacenter to search for a virtual machine. |
| 72 |
type: str |
| 73 |
extends_documentation_fragment: |
| 74 |
- vmware.vmware.base_options |
| 75 |
|
| 76 |
''' |
| 77 |
|
| 78 |
EXAMPLES = r''' |
| 79 |
- name: Wait for VMware tools to become available by UUID |
| 80 |
vmware_guest_facts: |
| 81 |
hostname: "{{ vcenter_hostname }}" |
| 82 |
username: "{{ vcenter_username }}" |
| 83 |
password: "{{ vcenter_password }}" |
| 84 |
datacenter: "{{ datacenter }}" |
| 85 |
folder: "/{{datacenter}}/vm" |
| 86 |
name: "{{ vm_name }}" |
| 87 |
delegate_to: localhost |
| 88 |
register: vm_facts |
| 89 |
|
| 90 |
- name: Get UUID from previous task and pass it to this task |
| 91 |
community.vmware.vmware_guest_tools_wait: |
| 92 |
hostname: "{{ vcenter_hostname }}" |
| 93 |
username: "{{ vcenter_username }}" |
| 94 |
password: "{{ vcenter_password }}" |
| 95 |
uuid: "{{ vm_facts.instance.hw_product_uuid }}" |
| 96 |
delegate_to: localhost |
| 97 |
register: facts |
| 98 |
|
| 99 |
|
| 100 |
- name: Wait for VMware tools to become available by MoID |
| 101 |
community.vmware.vmware_guest_tools_wait: |
| 102 |
hostname: "{{ vcenter_hostname }}" |
| 103 |
username: "{{ vcenter_username }}" |
| 104 |
password: "{{ vcenter_password }}" |
| 105 |
moid: vm-42 |
| 106 |
delegate_to: localhost |
| 107 |
register: facts |
| 108 |
|
| 109 |
- name: Wait for VMware tools to become available by name |
| 110 |
community.vmware.vmware_guest_tools_wait: |
| 111 |
hostname: "{{ vcenter_hostname }}" |
| 112 |
username: "{{ vcenter_username }}" |
| 113 |
password: "{{ vcenter_password }}" |
| 114 |
name: test-vm |
| 115 |
folder: "/{{datacenter}}/vm" |
| 116 |
datacenter: "{{ datacenter }}" |
| 117 |
delegate_to: localhost |
| 118 |
register: facts |
| 119 |
''' |
| 120 |
|
| 121 |
RETURN = r''' |
| 122 |
instance: |
| 123 |
description: metadata about the virtual machine |
| 124 |
returned: always |
| 125 |
type: dict |
| 126 |
sample: None |
| 127 |
''' |
| 128 |
|
| 129 |
import datetime |
| 130 |
import time |
| 131 |
|
| 132 |
from ansible.module_utils.basic import AnsibleModule |
| 133 |
from ansible.module_utils.common.text.converters import to_native |
| 134 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi, gather_vm_facts |
| 135 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 136 |
|
| 137 |
|
| 138 |
class PyVmomiHelper(PyVmomi): |
| 139 |
def __init__(self, module): |
| 140 |
super(PyVmomiHelper, self).__init__(module) |
| 141 |
|
| 142 |
def gather_facts(self, vm): |
| 143 |
return gather_vm_facts(self.content, vm) |
| 144 |
|
| 145 |
def wait_for_tools(self, vm, timeout): |
| 146 |
tools_running = False |
| 147 |
vm_facts = {} |
| 148 |
start_at = datetime.datetime.now() |
| 149 |
|
| 150 |
while start_at + timeout > datetime.datetime.now(): |
| 151 |
newvm = self.get_vm() |
| 152 |
vm_facts = self.gather_facts(newvm) |
| 153 |
if vm_facts['guest_tools_status'] == 'guestToolsRunning': |
| 154 |
return {'changed': True, 'failed': False, 'instance': vm_facts} |
| 155 |
time.sleep(5) |
| 156 |
|
| 157 |
if not tools_running: |
| 158 |
return {'failed': True, 'msg': 'VMware tools either not present or not running after {0} seconds'.format(timeout.total_seconds())} |
| 159 |
|
| 160 |
|
| 161 |
def main(): |
| 162 |
argument_spec = base_argument_spec() |
| 163 |
argument_spec.update( |
| 164 |
name=dict(type='str'), |
| 165 |
name_match=dict(type='str', default='first', choices=['first', 'last']), |
| 166 |
folder=dict(type='str'), |
| 167 |
uuid=dict(type='str'), |
| 168 |
moid=dict(type='str'), |
| 169 |
use_instance_uuid=dict(type='bool', default=False), |
| 170 |
timeout=dict(type='int', default=500), |
| 171 |
datacenter=dict(type='str'), |
| 172 |
) |
| 173 |
module = AnsibleModule( |
| 174 |
argument_spec=argument_spec, |
| 175 |
required_one_of=[ |
| 176 |
['name', 'uuid', 'moid'] |
| 177 |
] |
| 178 |
) |
| 179 |
|
| 180 |
if module.params['folder']: |
| 181 |
# FindByInventoryPath() does not require an absolute path |
| 182 |
# so we should leave the input folder path unmodified |
| 183 |
module.params['folder'] = module.params['folder'].rstrip('/') |
| 184 |
|
| 185 |
pyv = PyVmomiHelper(module) |
| 186 |
# Check if the VM exists before continuing |
| 187 |
vm = pyv.get_vm() |
| 188 |
|
| 189 |
if not vm: |
| 190 |
vm_id = module.params.get('name') or module.params.get('uuid') or module.params.get('moid') |
| 191 |
module.fail_json(msg="Unable to wait for VMware tools for non-existing VM '%s'." % vm_id) |
| 192 |
|
| 193 |
timeout = datetime.timedelta(seconds=module.params['timeout']) |
| 194 |
|
| 195 |
result = dict(changed=False) |
| 196 |
try: |
| 197 |
result = pyv.wait_for_tools(vm, timeout) |
| 198 |
except Exception as e: |
| 199 |
module.fail_json(msg="Waiting for VMware tools failed with" |
| 200 |
" exception: {0:s}".format(to_native(e))) |
| 201 |
|
| 202 |
if result['failed']: |
| 203 |
module.fail_json(**result) |
| 204 |
else: |
| 205 |
module.exit_json(**result) |
| 206 |
|
| 207 |
|
| 208 |
if __name__ == '__main__': |
| 209 |
main() |