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
pull/61359/merge
Diane Wang 5 years ago committed by Gonéri Le Bouder
parent 20d9c34862
commit d987dca2b1

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

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

Loading…
Cancel
Save