ansible-collections / community.vmware

Ansible Collection for VMware
GNU General Public License v3.0
348 stars 340 forks source link

vmware_content_deploy_ovf_template Does not respect proxy variable #843

Closed nsbender closed 3 years ago

nsbender commented 3 years ago
SUMMARY

When specifying VMWARE_PROXY_HOST and VMWARE_PROXY_PORT, vmware_content_deploy_ovf_template does not attempt to use the proxy, and fails to connect to a VCenter.

ISSUE TYPE
COMPONENT NAME

vmware_content_deploy_ovf_template

ANSIBLE VERSION
ansible 2.10.8
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/ubuntu/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.8/dist-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.8.5 (default, Jan 27 2021, 15:41:15) [GCC 9.3.0]
CONFIGURATION
DEFAULT_GATHERING(/etc/ansible/ansible.cfg) = implicit
DEFAULT_GATHER_SUBSET(/etc/ansible/ansible.cfg) = ['all']
DEFAULT_GATHER_TIMEOUT(/etc/ansible/ansible.cfg) = 10
DEFAULT_ROLES_PATH(/etc/ansible/ansible.cfg) = ['/etc/ansible/roles']
DEFAULT_STDOUT_CALLBACK(/etc/ansible/ansible.cfg) = skippy
HOST_KEY_CHECKING(/etc/ansible/ansible.cfg) = False
OS / ENVIRONMENT

Ubuntu 20.04.1 LTS VSphere Client Version 6.7.0.46000

STEPS TO REPRODUCE
---
- name: Deploy Virtual Machine from template in content library
  hosts: localhost
  connection: local
  vars:
    proxyenv:
      VMWARE_PROXY_HOST: "10.252.33.216"
      VMWARE_PROXY_PORT: 80

  tasks:
  - name: Clone the template
    environment: "{{ proxyenv }}"
    community.vmware.vmware_content_deploy_ovf_template:
      name: "{{ vmName }}"
      hostname: "{{ vcenterHostname }}"
      username: "{{ vcenterUsername }}"
      password: "{{ vcenterPassword }}"
      ovf_template: "{{ vmTemplate }}"
      content_library: "{{ vmContentLibrary }}"
      datastore: "{{ vmDatastore }}"
      folder: "Test"
      datacenter: "{{ vmDatacenter }}"
      cluster: "{{ vmCluster }}"
      validate_certs: no
      resource_pool: "Resources"
      host: "{{ vmDeployHost }}"
EXPECTED RESULTS

Another module, vmware_guest in the same playbook accepts and uses the same variable, and connects to VCenter through the proxy without any issue. The below output is from a content library deploy not using a proxy.

PLAY [Deploy Virtual Machine from template in content library] *****************TASK [Clone the template] ******************************************************
changed: [localhost] => {"changed": true, "vm_deploy_info": {"msg": "Deployed Virtual Machine '20212512-test-01'.", "vm_id": "vm-111345"}}
ACTUAL RESULTS
TASK [Clone the template] ***************************************************************************************************************************************************************************************************************************************************************************************************
task path: /home/automation/deployments/5a28f3cc-b1c6-11eb-95a6-570aa14ce1b6-TR-IaaS-vmware-vm-create.yaml:35
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: ubuntu
<127.0.0.1> EXEC /bin/sh -c 'echo ~ubuntu && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/ubuntu/.ansible/tmp `"&& mkdir "` echo /home/ubuntu/.ansible/tmp/ansible-tmp-1620854952.9003043-940526-133039244110806 `" && echo ansible-tmp-1620854952.9003043-940526-133039244110806="` echo /home/ubuntu/.ansible/tmp/ansible-tmp-1620854952.9003043-940526-133039244110806 `" ) && sleep 0'
Using module file /home/ubuntu/.ansible/collections/ansible_collections/community/vmware/plugins/modules/vmware_content_deploy_ovf_template.py
<127.0.0.1> PUT /home/ubuntu/.ansible/tmp/ansible-local-940522zdg8_o1c/tmpwshhbccp TO /home/ubuntu/.ansible/tmp/ansible-tmp-1620854952.9003043-940526-133039244110806/AnsiballZ_vmware_content_deploy_ovf_template.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/ubuntu/.ansible/tmp/ansible-tmp-1620854952.9003043-940526-133039244110806/ /home/ubuntu/.ansible/tmp/ansible-tmp-1620854952.9003043-940526-133039244110806/AnsiballZ_vmware_content_deploy_ovf_template.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'HTTP_PROXY=<Proxy IP Censored>:80 HTTPS_PROXY=<Proxy IP Censored>:80 /usr/bin/python3 /home/ubuntu/.ansible/tmp/ansible-tmp-1620854952.9003043-940526-133039244110806/AnsiballZ_vmware_content_deploy_ovf_template.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/ubuntu/.ansible/tmp/ansible-tmp-1620854952.9003043-940526-133039244110806/ > /dev/null 2>&1 && sleep 0'
The full traceback is:
  File "/tmp/ansible_community.vmware.vmware_content_deploy_ovf_template_payload_rnmiwrqh/ansible_community.vmware.vmware_content_deploy_ovf_template_payload.zip/ansible_collections/community/vmware/plugins/module_utils/vmware.py", line 613, in connect_to_api
    service_instance = connect.SmartConnect(**connect_args)
  File "/home/ubuntu/.local/lib/python3.8/site-packages/pyVim/connect.py", line 842, in SmartConnect
    supportedVersion = __FindSupportedVersion(protocol,
  File "/home/ubuntu/.local/lib/python3.8/site-packages/pyVim/connect.py", line 720, in __FindSupportedVersion
    serviceVersionDescription = __GetServiceVersionDescription(protocol,
  File "/home/ubuntu/.local/lib/python3.8/site-packages/pyVim/connect.py", line 641, in __GetServiceVersionDescription
    tree = __GetElementTree(protocol, server, port,
  File "/home/ubuntu/.local/lib/python3.8/site-packages/pyVim/connect.py", line 608, in __GetElementTree
    conn.request("GET", path)
  File "/usr/lib/python3.8/http/client.py", line 1255, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/lib/python3.8/http/client.py", line 1301, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.8/http/client.py", line 1250, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.8/http/client.py", line 1010, in _send_output
    self.send(msg)
  File "/usr/lib/python3.8/http/client.py", line 950, in send
    self.connect()
  File "/usr/lib/python3.8/http/client.py", line 1417, in connect
    super().connect()
  File "/usr/lib/python3.8/http/client.py", line 921, in connect
    self.sock = self._create_connection(
  File "/usr/lib/python3.8/socket.py", line 808, in create_connection
    raise err
  File "/usr/lib/python3.8/socket.py", line 796, in create_connection
    sock.connect(sa)
fatal: [localhost]: FAILED! => {
    "changed": false,
    "invocation": {
        "module_args": {
            "cluster": "cluster",
            "content_library": "library",
            "datacenter": "datacenter",
            "datastore": "datastore",
            "folder": "Test",
            "host": "host.domain.com",
            "hostname": "hostname",
            "name": "20210507-test-05",
            "ovf_template": "mws_win_server2019_20210506_1744",
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "port": 443,
            "protocol": "https",
            "resource_pool": "Resources",
            "storage_provisioning": null,
            "username": "administrator@vsphere.local",
            "validate_certs": false
        }
    },
    "msg": "Unknown error while connecting to vCenter or ESXi API at <vcenter IP censored>:443 : [Errno 110] Connection timed out"
}
abikouo commented 3 years ago

Hi @nsbender Thanks for taking the time to report this issue. The vmware_content_deploy_ovf_template use the vmware.vapi.vsphere.client.create_vsphere_client to interact with the vsphere using a requests.Session object which is not update yet with proxies parameters. This will need a fix.

abikouo commented 3 years ago

@nsbender could you test the proxy connection using PR #848 ?

nsbender commented 3 years ago

@abikouo, we have tested the pull request version by specifying the proxy host and port using both VMWARE_PROXY_HOST/PORT and proxy_host/port and they both resulted in

The error was: AttributeError: 'dict' object has no attribute 'proxy_port' (Full error at bottom)

Here is the playbook:

  vars:

...

    proxyenv:
      VMWARE_PROXY_HOST: "10.252.33.216"
      VMWARE_PROXY_PORT: 80
      # proxy_host: "10.252.33.216"
      # proxy_port: 80

  tasks:
  - name: Clone the template
    environment: "{{ proxyenv }}"
    community.vmware.vmware_content_deploy_ovf_template:
      name: "{{ vmName }}"
      hostname: "{{ vcenterHostname }}"
      username: "{{ vcenterUsername }}"
      password: "{{ vcenterPassword }}"
      ovf_template: "{{ vmTemplate }}"
      content_library: "{{ vmContentLibrary }}"
      datastore: "{{ vmDatastore }}"
      folder: "Test"
      datacenter: "{{ vmDatacenter }}"
      cluster: "{{ vmCluster }}"
      validate_certs: no
      resource_pool: "Resources"
      host: "{{ vmDeployHost }}"
...

Full error:

TASK [Clone the template] ******************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AttributeError: 'dict' object has no attribute 'proxy_port'
fatal: [localhost]: FAILED! => {"changed": false, "module_stderr": "Traceback (most recent call last):\n  File \"/home/automation/.ansible/tmp/ansible-tmp-1621269108.5774417-2372561-169852255276665/AnsiballZ_vmware_content_deploy_ovf_template.py\", line 102, in <module>\n    _ansiballz_main()\n  File \"/home/automation/.ansible/tmp/ansible-tmp-1621269108.5774417-2372561-169852255276665/AnsiballZ_vmware_content_deploy_ovf_template.py\", line 94, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/home/automation/.ansible/tmp/ansible-tmp-1621269108.5774417-2372561-169852255276665/AnsiballZ_vmware_content_deploy_ovf_template.py\", line 40, in invoke_module\n    runpy.run_module(mod_name='ansible_collections.community.vmware.plugins.modules.vmware_content_deploy_ovf_template', init_globals=None, run_name='__main__', alter_sys=True)\n  File \"/usr/lib/python3.8/runpy.py\", line 207, in run_module\n    return _run_module_code(code, init_globals, run_name, mod_spec)\n  File \"/usr/lib/python3.8/runpy.py\", line 97, in _run_module_code\n    _run_code(code, mod_globals, init_globals,\n  File \"/usr/lib/python3.8/runpy.py\", line 87, in _run_code\n    exec(code, run_globals)\n  File \"/tmp/ansible_community.vmware.vmware_content_deploy_ovf_template_payload_3db6srey/ansible_community.vmware.vmware_content_deploy_ovf_template_payload.zip/ansible_collections/community/vmware/plugins/modules/vmware_content_deploy_ovf_template.py\", line 277, in <module>\n  File \"/tmp/ansible_community.vmware.vmware_content_deploy_ovf_template_payload_3db6srey/ansible_community.vmware.vmware_content_deploy_ovf_template_payload.zip/ansible_collections/community/vmware/plugins/modules/vmware_content_deploy_ovf_template.py\", line 265, in main\n  File \"/tmp/ansible_community.vmware.vmware_content_deploy_ovf_template_payload_3db6srey/ansible_community.vmware.vmware_content_deploy_ovf_template_payload.zip/ansible_collections/community/vmware/plugins/modules/vmware_content_deploy_ovf_template.py\", line 139, in __init__\n  File \"/tmp/ansible_community.vmware.vmware_content_deploy_ovf_template_payload_3db6srey/ansible_community.vmware.vmware_content_deploy_ovf_template_payload.zip/ansible_collections/community/vmware/plugins/module_utils/vmware_rest_client.py\", line 59, in __init__\n  File \"/tmp/ansible_community.vmware.vmware_content_deploy_ovf_template_payload_3db6srey/ansible_community.vmware.vmware_content_deploy_ovf_template_payload.zip/ansible_collections/community/vmware/plugins/module_utils/vmware_rest_client.py\", line 137, in connect_to_vsphere_client\nAttributeError: 'dict' object has no attribute 'proxy_port'\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}
nsbender commented 3 years ago

@abikouo I believe we fixed the issue. On line 137 of vmware_rest_client.py:

Should: if self.params.get('protocol') and self.params.get('proxy_host') and self.params.proxy_port:

be: self.params.get('protocol') and self.params.get('proxy_host') and self.params.get('proxy_port'): ?

We were able to get it working with the latter.

abikouo commented 3 years ago

@abikouo I believe we fixed the issue. On line 137 of vmware_rest_client.py:

Should: if self.params.get('protocol') and self.params.get('proxy_host') and self.params.proxy_port:

be: self.params.get('protocol') and self.params.get('proxy_host') and self.params.get('proxy_port'): ?

We were able to get it working with the latter.

yes you are right, I was in a rush

nsbender commented 3 years ago

When that pull request gets merged with this change, this issue will be resolved. Thanks for all your help!