Repository: ansible-collections/community.vmware

Issue:
community.vmware.vmware_cfg_backup: urlopen error timed out on config restore
##### SUMMARY
When attempting a restore to a host the module fails with the below URL timed out failure, due to port 80 being blocked.
Similar issue to #1270 which was for backup, and a similar fix works

##### ISSUE TYPE
- Bug Report

##### COMPONENT NAME
`vmware_cfg_backup`

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes -->
```paste below
ansible [core 2.11.12] 
  config file = None
  configured module search path = ['/home/ansible/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/ansible/venvs/vmware-runner/lib64/python3.6/site-packages/ansible
  ansible collection location = /home/ansible/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/ansible/venvs/vmware-runner/bin/ansible
  python version = 3.6.8 (default, Oct 13 2020, 16:18:22) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
  jinja version = 3.0.3
  libyaml = True

```

##### COLLECTION VERSION
<!--- Paste verbatim output from "ansible-galaxy collection list <namespace>.<collection>"  between the quotes
for example: ansible-galaxy collection list community.general
-->
```paste below
# /home/ansible/.ansible/collections/ansible_collections
Collection       Version
---------------- -------
community.vmware 2.7.0
```

##### CONFIGURATION
<!--- Paste verbatim output from "ansible-config dump --only-changed" between quotes -->
```paste below

```

##### OS / ENVIRONMENT
Centos 7 (with venv for ansible)
Vmware ESXi 7.0.3

##### STEPS TO REPRODUCE
<!--- Describe exactly how to reproduce the problem, using a minimal test-case -->
This needs port 80 blocked to the host to show the failure.
I identified the issue via packet capture, and then found the related issue and fix.

<!--- Paste example playbooks or commands between quotes below -->
```yaml
- hosts: localhost
  vars:
    esxi_hostname: "192.168.88.154"
    esxi_username: "root"
    esxi_password: "password"
  tasks:
  - name: ESXI restore test
    community.vmware.vmware_cfg_backup:
      hostname: '{{esxi_hostname}}'
      username: '{{esxi_username}}'
      password: '{{esxi_password}}'
      validate_certs: no
      state: loaded
      src: ./configbackup.tgz

```

<!--- HINT: You can paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- Describe what you expected to happen when running the steps above -->
Module should restore the backup file to the host.

##### ACTUAL RESULTS
<!--- Describe what actually happened. If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes -->
```paste below
PLAY [localhost] *********************************************************************************************************************************************

TASK [ESXI restore test] *************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "module_stderr": "/tmp/ansible_community.vmware.vmware_cfg_backup_payload_hgs7dk1q/ansible_community.vmware.vmware_cfg_backup_payload.zip/ansible/module_utils/urls.py:176: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography and will be removed in a future release.\nTraceback (most recent call last):\n  File \"/usr/lib64/python3.6/urllib/request.py\", line 1349, in do_open\n    encode_chunked=req.has_header('Transfer-encoding'))\n  File \"/usr/lib64/python3.6/http/client.py\", line 1254, in request\n    self._send_request(method, url, body, headers, encode_chunked)\n  File \"/usr/lib64/python3.6/http/client.py\", line 1300, in _send_request\n    self.endheaders(body, encode_chunked=encode_chunked)\n  File \"/usr/lib64/python3.6/http/client.py\", line 1249, in endheaders\n    self._send_output(message_body, encode_chunked=encode_chunked)\n  File \"/usr/lib64/python3.6/http/client.py\", line 1036, in _send_output\n    self.send(msg)\n  File \"/usr/lib64/python3.6/http/client.py\", line 974, in send\n    self.connect()\n  File \"/usr/lib64/python3.6/http/client.py\", line 946, in connect\n    (self.host,self.port), self.timeout, self.source_address)\n  File \"/usr/lib64/python3.6/socket.py\", line 724, in create_connection\n    raise err\n  File \"/usr/lib64/python3.6/socket.py\", line 713, in create_connection\n    sock.connect(sa)\nsocket.timeout: timed out\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n  File \"/home/ansible/.ansible/tmp/ansible-tmp-1661998153.8253503-4467-134295241311897/AnsiballZ_vmware_cfg_backup.py\", line 100, in <module>\n    _ansiballz_main()\n  File \"/home/ansible/.ansible/tmp/ansible-tmp-1661998153.8253503-4467-134295241311897/AnsiballZ_vmware_cfg_backup.py\", line 92, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/home/ansible/.ansible/tmp/ansible-tmp-1661998153.8253503-4467-134295241311897/AnsiballZ_vmware_cfg_backup.py\", line 41, in invoke_module\n    run_name='__main__', alter_sys=True)\n  File \"/usr/lib64/python3.6/runpy.py\", line 205, in run_module\n    return _run_module_code(code, init_globals, run_name, mod_spec)\n  File \"/usr/lib64/python3.6/runpy.py\", line 96, in _run_module_code\n    mod_name, mod_spec, pkg_name, script_name)\n  File \"/usr/lib64/python3.6/runpy.py\", line 85, in _run_code\n    exec(code, run_globals)\n  File \"/tmp/ansible_community.vmware.vmware_cfg_backup_payload_hgs7dk1q/ansible_community.vmware.vmware_cfg_backup_payload.zip/ansible_collections/community/vmware/plugins/modules/vmware_cfg_backup.py\", line 231, in <module>\n  File \"/tmp/ansible_community.vmware.vmware_cfg_backup_payload_hgs7dk1q/ansible_community.vmware.vmware_cfg_backup_payload.zip/ansible_collections/community/vmware/plugins/modules/vmware_cfg_backup.py\", line 227, in main\n  File \"/tmp/ansible_community.vmware.vmware_cfg_backup_payload_hgs7dk1q/ansible_community.vmware.vmware_cfg_backup_payload.zip/ansible_collections/community/vmware/plugins/modules/vmware_cfg_backup.py\", line 130, in process_state\n  File \"/tmp/ansible_community.vmware.vmware_cfg_backup_payload_hgs7dk1q/ansible_community.vmware.vmware_cfg_backup_payload.zip/ansible_collections/community/vmware/plugins/modules/vmware_cfg_backup.py\", line 142, in load_configuration\n  File \"/tmp/ansible_community.vmware.vmware_cfg_backup_payload_hgs7dk1q/ansible_community.vmware.vmware_cfg_backup_payload.zip/ansible/module_utils/urls.py\", line 1541, in open_url\n  File \"/tmp/ansible_community.vmware.vmware_cfg_backup_payload_hgs7dk1q/ansible_community.vmware.vmware_cfg_backup_payload.zip/ansible/module_utils/urls.py\", line 1446, in open\n  File \"/usr/lib64/python3.6/urllib/request.py\", line 223, in urlopen\n    return opener.open(url, data, timeout)\n  File \"/usr/lib64/python3.6/urllib/request.py\", line 526, in open\n    response = self._open(req, data)\n  File \"/usr/lib64/python3.6/urllib/request.py\", line 544, in _open\n    '_open', req)\n  File \"/usr/lib64/python3.6/urllib/request.py\", line 504, in _call_chain\n    result = func(*args)\n  File \"/usr/lib64/python3.6/urllib/request.py\", line 1377, in http_open\n    return self.do_open(http.client.HTTPConnection, req)\n  File \"/usr/lib64/python3.6/urllib/request.py\", line 1351, in do_open\n    raise URLError(err)\nurllib.error.URLError: <urlopen error timed out>\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}

PLAY RECAP ***************************************************************************************************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

```



Discussion:

Resolving patch:
diff --git a/plugins/modules/vmware_cfg_backup.py b/plugins/modules/vmware_cfg_backup.py
--- a/plugins/modules/vmware_cfg_backup.py
+++ b/plugins/modules/vmware_cfg_backup.py
@@ -135,6 +135,8 @@ def load_configuration(self):
 
         url = self.host.configManager.firmwareSystem.QueryFirmwareConfigUploadURL()
         url = url.replace('*', self.cfg_hurl)
+        if self.module.params["port"] == 443:
+            url = url.replace("http:", "https:")
         # find manually the url if there is a redirect because urllib2 -per RFC- doesn't do automatic redirects for PUT requests
         try:
             open_url(url=url, method='HEAD', validate_certs=self.validate_certs)


Test patch: