VMware: vmware_guest_screenshot module fix get_parent_datacenter issue (#60626)

Fix get_parent_datacenter not found issue, and change "local_path" parameter to "path" type.

closes: #60565
This commit is contained in:
Diane Wang 2019-08-28 10:04:30 -07:00 committed by Gonéri Le Bouder
parent 20d9c34862
commit d987dca2b1
2 changed files with 11 additions and 17 deletions

View file

@ -84,7 +84,7 @@ options:
downloaded from ESXi host to the local directory.'
- 'If not download screenshot file to local machine, you can open it through the returned file URL in screenshot
facts manually.'
type: str
type: path
extends_documentation_fragment: vmware.documentation
'''
@ -142,7 +142,7 @@ from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.six.moves.urllib.parse import urlencode, quote
from ansible.module_utils._text import to_native
from ansible.module_utils.urls import open_url
from ansible.module_utils.vmware import PyVmomi, vmware_argument_spec, wait_for_task
from ansible.module_utils.vmware import PyVmomi, vmware_argument_spec, wait_for_task, get_parent_datacenter
import os
@ -163,11 +163,7 @@ class PyVmomiHelper(PyVmomi):
if not self.is_vcenter():
datacenter = 'ha-datacenter'
else:
if self.params.get('datacenter'):
datacenter = self.params['datacenter']
else:
datacenter = self.get_parent_datacenter(self.current_vm_obj).name
datacenter = datacenter.replace('&', '%26')
datacenter = get_parent_datacenter(self.current_vm_obj).name.replace('&', '%26')
params['dcPath'] = datacenter
url_path = "https://%s%s?%s" % (self.params['hostname'], path, urlencode(params))
@ -177,8 +173,6 @@ class PyVmomiHelper(PyVmomi):
response = None
download_size = 0
# file is downloaded as local_file_name when specified, or use original file name
if not local_file_path.startswith("/"):
local_file_path = "/" + local_file_path
if local_file_path.endswith('.png'):
local_file_name = local_file_path.split('/')[-1]
local_file_path = local_file_path.rsplit('/', 1)[0]
@ -268,7 +262,7 @@ def main():
datacenter=dict(type='str'),
esxi_hostname=dict(type='str'),
cluster=dict(type='str'),
local_path=dict(type='str'),
local_path=dict(type='path'),
)
module = AnsibleModule(
argument_spec=argument_spec,

View file

@ -11,13 +11,15 @@
setup_datastore: true
setup_virtualmachines: true
- name: set state to poweroff on all VMs
- name: set VM state to powered on
vmware_guest:
validate_certs: False
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
name: "{{ virtual_machines_in_cluster[0].name }}"
datacenter: "{{ dc1 }}"
folder: "{{ virtual_machines[0].folder }}"
name: "{{ virtual_machines[0].name }}"
state: poweredon
- name: take screenshot of virtual machine's console
@ -26,9 +28,7 @@
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
name: "{{ virtual_machines_in_cluster[0].name }}"
# Note: set the datacenter key because of https://github.com/ansible/ansible/issues/60565
datacenter: "{{ dc1 }}"
name: "{{ virtual_machines[0].name }}"
register: take_screenshot
- debug: var=take_screenshot
- name: assert the screenshot captured
@ -42,10 +42,10 @@
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
name: "{{ virtual_machines_in_cluster[0].name }}"
name: "{{ virtual_machines[0].name }}"
local_path: "/tmp/screenshot_test.png"
# Note: set the datacenter key because of https://github.com/ansible/ansible/issues/60565
datacenter: "{{ dc1 }}"
folder: "{{ virtual_machines[0].folder }}"
register: take_screenshot
- debug: var=take_screenshot
- name: assert the screenshot captured