vmware_guest_tools_wait: add timeout parameter

Add a new `timeout` parameter to `vmware_guest_tools_wait`.

Also, a functional test to cover the module.
This commit is contained in:
Gonéri Le Bouder 2020-01-31 12:18:18 -05:00
parent 57d43d1932
commit 802cc60242
4 changed files with 63 additions and 72 deletions
changelogs/fragments
lib/ansible/modules/cloud/vmware
test/integration/targets
vmware_guest_network/tasks
vmware_guest_tools_wait/tasks

View file

@ -0,0 +1,3 @@
---
minor_changes:
- vmware_guest_tools_wait now exposes a ``timeout`` parameter that allow the user to adjust the timeout (second).

View file

@ -72,6 +72,12 @@ options:
default: no
type: bool
version_added: '2.8'
timeout:
description:
- Max duration of the waiting period (seconds).
default: 500
type: int
version_added: '2.10'
extends_documentation_fragment: vmware.documentation
'''
@ -129,6 +135,7 @@ instance:
sample: None
"""
import datetime
import time
from ansible.module_utils.basic import AnsibleModule
@ -143,26 +150,20 @@ class PyVmomiHelper(PyVmomi):
def gather_facts(self, vm):
return gather_vm_facts(self.content, vm)
def wait_for_tools(self, vm, poll=100, sleep=5):
def wait_for_tools(self, vm, timeout):
tools_running = False
vm_facts = {}
poll_num = 0
while not tools_running and poll_num <= poll:
start_at = datetime.datetime.now()
while start_at + timeout > datetime.datetime.now():
newvm = self.get_vm()
vm_facts = self.gather_facts(newvm)
if vm_facts['guest_tools_status'] == 'guestToolsRunning':
tools_running = True
else:
time.sleep(sleep)
poll_num += 1
return {'changed': True, 'failed': False, 'instance': vm_facts}
time.sleep(5)
if not tools_running:
return {'failed': True, 'msg': 'VMware tools either not present or not running after {0} seconds'.format((poll * sleep))}
changed = False
if poll_num > 0:
changed = True
return {'changed': changed, 'failed': False, 'instance': vm_facts}
return {'failed': True, 'msg': 'VMware tools either not present or not running after {0} seconds'.format(timeout.total_seconds())}
def main():
@ -174,6 +175,7 @@ def main():
uuid=dict(type='str'),
moid=dict(type='str'),
use_instance_uuid=dict(type='bool', default=False),
timeout=dict(type='int', default=500),
)
module = AnsibleModule(
argument_spec=argument_spec,
@ -195,9 +197,11 @@ def main():
vm_id = module.params.get('name') or module.params.get('uuid') or module.params.get('moid')
module.fail_json(msg="Unable to wait for VMware tools for non-existing VM '%s'." % vm_id)
timeout = datetime.timedelta(seconds=module.params['timeout'])
result = dict(changed=False)
try:
result = pyv.wait_for_tools(vm)
result = pyv.wait_for_tools(vm, timeout)
except Exception as e:
module.fail_json(msg="Waiting for VMware tools failed with"
" exception: {0:s}".format(to_native(e)))

View file

@ -42,6 +42,7 @@
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
timeout: 600
validate_certs: no
name: test_vm1

View file

@ -2,63 +2,46 @@
# Copyright: (c) 2017 Philippe Dellaert <philippe@dellaert.org>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
- import_role:
name: prepare_vmware_tests
vars:
setup_attach_host: true
setup_datastore: true
setup_virtualmachines: true
- when: vcsim is not defined
block:
- import_role:
name: prepare_vmware_tests
vars:
setup_attach_host: true
setup_datastore: true
setup_dvs_portgroup: true
setup_dvswitch: true
- name: Power on VM1
vmware_guest:
validate_certs: False
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
name: "{{ virtual_machines[0].name }}"
folder: "{{ virtual_machines[0].folder }}"
datacenter: "{{ dc1 }}"
state: poweredon
- name: Create VMs
vmware_guest:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datacenter: "{{ dc1 }}"
validate_certs: no
folder: '/DC0/vm/F0'
name: test_vm1
state: poweredon
guest_id: centos7_64Guest
disk:
- size_gb: 1
type: thin
datastore: '{{ rw_datastore }}'
hardware:
version: latest
memory_mb: 1024
num_cpus: 1
scsi: paravirtual
cdrom:
type: iso
iso_path: "[{{ ro_datastore }}] fedora.iso"
networks:
- name: VM Network
# FixMe: govcsim does not support VMware tools status reporting
## Testcase 0001: Wait for VMware tools to become available by name
#- name: Waiting until VMware tools becomes available
# vmware_guest_tools_wait:
# validate_certs: False
# hostname: "{{ vcsim }}"
# username: "{{ vcsim_instance['json']['username'] }}"
# password: "{{ vcsim_instance['json']['password'] }}"
# name: "{{ vm1 | basename }}"
# folder: "{{ vm1 | dirname }}"
# register: guest_info_0001
#
#- debug: msg="{{ guest_info_0001 }}"
#
#- assert:
# that:
# - "guest_info_0001['instance']['hw_name'] == vm1 | basename"
# - "guest_info_0001['instance']['hw_product_uuid'] is defined"
# - "guest_info_0001['instance']['guest_tools_status'] == 'guestToolsRunning'"
#
#- set_fact: vm1_uuid="{{ guest_info_0001['instance']['hw_product_uuid'] }}"
#
#- debug: var=vm1_uuid
#
# Testcase 0002: Wait for VMware tools to become available by UUID
#- name: Waiting until VMware tools becomes available
# vmware_guest_tools_wait:
# validate_certs: False
# hostname: "{{ vcsim }}"
# username: "{{ vcsim_instance['json']['username'] }}"
# password: "{{ vcsim_instance['json']['password'] }}"
# uuid: "{{ vm1_uuid }}"
# register: guest_info_0002
#
#- debug: msg="{{ guest_info_0002 }}"
#
#- assert:
# that:
# - "guest_info_0002['instance']['hw_name'] == vm1 | basename"
# - "guest_info_0002['instance']['hw_product_uuid'] is defined"
# - "guest_info_0002['instance']['hw_product_uuid'] == vm1_uuid"
# - "guest_info_0002['instance']['guest_tools_status'] == 'guestToolsRunning'"
- vmware_guest_tools_wait:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
timeout: 800
validate_certs: no
name: test_vm1