Repository: ansible-collections/community.vmware

Issue:
vmware_content_deploy_ovf_template fails requiring resource_pool_id
<!--- Verify first that your issue is not already reported on GitHub -->
<!--- Also test if the latest release and devel branch are affected too -->
<!--- Complete *all* sections as described, this form is processed automatically -->

##### SUMMARY
Attempting to create a VM using the vmware_content_deploy_ovf_template module. It's failing with:

```File \"/usr/local/lib/python3.7/site-packages/vmware/vapi/bindings/converter.py\", line 257, in _visit_vapi_struct\n    raise CoreException(msg)\nvmware.vapi.exception.CoreException: Field resource_pool_id missing from Structure com.vmware.vcenter.ovf.library_item.deployment_target
```
##### ISSUE TYPE
- Bug Report

##### COMPONENT NAME
vmware_content_deploy_ovf_template module

##### ANSIBLE VERSION
```paste below
ansible 2.9.7
  config file = /Users/ppiper/workspace/ansible/ansible.cfg
  configured module search path = ['/Users/ppiper/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.7/site-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.7.7 (default, Mar 10 2020, 15:43:33) [Clang 11.0.0 (clang-1100.0.33.17)]
```

##### CONFIGURATION
<!--- Paste verbatim output from "ansible-config dump --only-changed" between quotes -->
```paste below
DEFAULT_FORKS(/Users/ppiper/workspace/ansible/ansible.cfg) = 10
DEFAULT_HOST_LIST(/Users/ppiper/workspace/ansible/ansible.cfg) = ['/Users/ppiper/workspace/ansible/inventories/production/hosts']
DEFAULT_REMOTE_USER(/Users/ppiper/workspace/ansible/ansible.cfg) = ansible
HOST_KEY_CHECKING(/Users/ppiper/workspace/ansible/ansible.cfg) = False
INTERPRETER_PYTHON(/Users/ppiper/workspace/ansible/ansible.cfg) = auto
```

##### OS / ENVIRONMENT
```
  System Version:	macOS 10.15.4 (19E287)
  Kernel Version:	Darwin 19.4.0
  Model Name:	Mac mini
  Model Identifier:	Macmini8,1
  Processor Name:	6-Core Intel Core i7
  Processor Speed:	3.2 GHz
  Number of Processors:	1
  Total Number of Cores:	6
  L2 Cache (per Core):	256 KB
  L3 Cache:	12 MB
  Hyper-Threading Technology:	Enabled
  Memory:	64 GB
  Boot ROM Version:	1037.100.362.0.0 (iBridge: 17.16.14281.0.0,0)
```

##### STEPS TO REPRODUCE
<!--- Describe exactly how to reproduce the problem, using a minimal test-case -->
```
ansible-playbook test.yml -l test-centos -vvv
```

<!--- Paste example playbooks or commands between quotes below -->
```yaml
---
# This playbook creates a single virtual machine from vcenter using vmware_guest module
- hosts: all
  serial: 1
  connection: local
  gather_facts: no
  tasks:
    - name: Create a VM from a template
      community.vmware.vmware_content_deploy_ovf_template:
        hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_user }}"
        password: "{{ vcenter_password }}"
        validate_certs: no
        name: "{{ inventory_hostname }}"
        ovf_template: "{{ vmware_template }}"
        datacenter: "{{ vmware_datacenter }}"
        datastore: "{{ esxi_hostname }}-datastore"
        host: "{{ esxi_hostname }}.ddiguru.net"
        folder: "{{ vmware_folder }}"
      delegate_to: localhost
      register: deploy

    - name: Show Debug results
      debug:
        msg: "{{ deploy }}"
      tags:
        - always
      when: debug == 'yes'
```

NOTE: when i created a Resource Pool called "test_rp" and added that to the above config. it works. 

##### EXPECTED RESULTS
```TASK [Create a VM from a template] ***************************************************************************************************************************************************
task path: /Users/ppiper/workspace/ansible/test.yml:52
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: ppiper
<localhost> EXEC /bin/sh -c 'echo ~ppiper && sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /Users/ppiper/.ansible/tmp `"&& mkdir /Users/ppiper/.ansible/tmp/ansible-tmp-1588623536.459615-35952-273111745314636 && echo ansible-tmp-1588623536.459615-35952-273111745314636="` echo /Users/ppiper/.ansible/tmp/ansible-tmp-1588623536.459615-35952-273111745314636 `" ) && sleep 0'
Using module file /Users/ppiper/.ansible/collections/ansible_collections/community/vmware/plugins/modules/vmware_content_deploy_ovf_template.py
<localhost> PUT /Users/ppiper/.ansible/tmp/ansible-local-35947j491acn4/tmp1iw3kd_8 TO /Users/ppiper/.ansible/tmp/ansible-tmp-1588623536.459615-35952-273111745314636/AnsiballZ_vmware_content_deploy_ovf_template.py
<localhost> EXEC /bin/sh -c 'chmod u+x /Users/ppiper/.ansible/tmp/ansible-tmp-1588623536.459615-35952-273111745314636/ /Users/ppiper/.ansible/tmp/ansible-tmp-1588623536.459615-35952-273111745314636/AnsiballZ_vmware_content_deploy_ovf_template.py && sleep 0'
<localhost> EXEC /bin/sh -c '/usr/local/bin/python3 /Users/ppiper/.ansible/tmp/ansible-tmp-1588623536.459615-35952-273111745314636/AnsiballZ_vmware_content_deploy_ovf_template.py && sleep 0'
<localhost> EXEC /bin/sh -c 'rm -f -r /Users/ppiper/.ansible/tmp/ansible-tmp-1588623536.459615-35952-273111745314636/ > /dev/null 2>&1 && sleep 0'
changed: [filebeat02 -> localhost] => {
    "changed": true,
    "invocation": {
        "module_args": {
            "cluster": null,
            "datacenter": "PC",
            "datastore": "esxi2-datastore",
            "folder": "vm",
            "host": "esxi2.ddiguru.net",
            "hostname": "vcenter.ddiguru.net",
            "name": "filebeat02",
            "ovf_template": "centos-8.1",
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "protocol": "https",
            "resource_pool": "test_rp",
            "username": "ppiper@vsphere.local",
            "validate_certs": false
        }
    },
    "vm_deploy_info": {
        "msg": "Deployed Virtual Machine 'filebeat02'.",
        "vm_id": "vm-2529"
    }
}
```
NOTE: I SHOULD NOT have to supply the resource_pool "test_rp" to create the VM from OVF content library template. 

##### ACTUAL RESULTS

```paste below
ap test.yml -l filebeat02 -vvv
ansible-playbook 2.9.7
  config file = /Users/ppiper/workspace/ansible/ansible.cfg
  configured module search path = ['/Users/ppiper/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.7/site-packages/ansible
  executable location = /usr/local/bin/ansible-playbook
  python version = 3.7.7 (default, Mar 10 2020, 15:43:33) [Clang 11.0.0 (clang-1100.0.33.17)]
Using /Users/ppiper/workspace/ansible/ansible.cfg as config file
host_list declined parsing /Users/ppiper/workspace/ansible/inventories/production/hosts as it did not pass its verify_file() method
script declined parsing /Users/ppiper/workspace/ansible/inventories/production/hosts as it did not pass its verify_file() method
auto declined parsing /Users/ppiper/workspace/ansible/inventories/production/hosts as it did not pass its verify_file() method
Parsed /Users/ppiper/workspace/ansible/inventories/production/hosts inventory source with ini plugin

PLAYBOOK: test.yml *******************************************************************************************************************************************************************
1 plays in test.yml
Enter Template [centos-8.1]:
Enter vlan number [40]:

PLAY [all] ***************************************************************************************************************************************************************************
META: ran handlers

TASK [Create a VM from a template] ***************************************************************************************************************************************************
task path: /Users/ppiper/workspace/ansible/test.yml:52
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: ppiper
<localhost> EXEC /bin/sh -c 'echo ~ppiper && sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /Users/ppiper/.ansible/tmp `"&& mkdir /Users/ppiper/.ansible/tmp/ansible-tmp-1588623093.0298822-35766-66306551630703 && echo ansible-tmp-1588623093.0298822-35766-66306551630703="` echo /Users/ppiper/.ansible/tmp/ansible-tmp-1588623093.0298822-35766-66306551630703 `" ) && sleep 0'
Using module file /Users/ppiper/.ansible/collections/ansible_collections/community/vmware/plugins/modules/vmware_content_deploy_ovf_template.py
<localhost> PUT /Users/ppiper/.ansible/tmp/ansible-local-357616k4gnoqi/tmp5hy3c78z TO /Users/ppiper/.ansible/tmp/ansible-tmp-1588623093.0298822-35766-66306551630703/AnsiballZ_vmware_content_deploy_ovf_template.py
<localhost> EXEC /bin/sh -c 'chmod u+x /Users/ppiper/.ansible/tmp/ansible-tmp-1588623093.0298822-35766-66306551630703/ /Users/ppiper/.ansible/tmp/ansible-tmp-1588623093.0298822-35766-66306551630703/AnsiballZ_vmware_content_deploy_ovf_template.py && sleep 0'
<localhost> EXEC /bin/sh -c '/usr/local/bin/python3 /Users/ppiper/.ansible/tmp/ansible-tmp-1588623093.0298822-35766-66306551630703/AnsiballZ_vmware_content_deploy_ovf_template.py && sleep 0'
<localhost> EXEC /bin/sh -c 'rm -f -r /Users/ppiper/.ansible/tmp/ansible-tmp-1588623093.0298822-35766-66306551630703/ > /dev/null 2>&1 && sleep 0'
fatal: [filebeat02 -> localhost]: FAILED! => {
    "changed": false,
    "module_stderr": "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py:986: InsecureRequestWarning: Unverified HTTPS request is being made to host 'vcenter.ddiguru.net'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings\n  InsecureRequestWarning,\n/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py:986: InsecureRequestWarning: Unverified HTTPS request is being made to host 'vcenter.ddiguru.net'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings\n  InsecureRequestWarning,\n/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py:986: InsecureRequestWarning: Unverified HTTPS request is being made to host 'vcenter.ddiguru.net'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings\n  InsecureRequestWarning,\n/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py:986: InsecureRequestWarning: Unverified HTTPS request is being made to host 'vcenter.ddiguru.net'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings\n  InsecureRequestWarning,\n/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py:986: InsecureRequestWarning: Unverified HTTPS request is being made to host 'vcenter.ddiguru.net'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings\n  InsecureRequestWarning,\n/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py:986: InsecureRequestWarning: Unverified HTTPS request is being made to host 'vcenter.ddiguru.net'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings\n  InsecureRequestWarning,\n/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py:986: InsecureRequestWarning: Unverified HTTPS request is being made to host 'vcenter.ddiguru.net'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings\n  InsecureRequestWarning,\n/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py:986: InsecureRequestWarning: Unverified HTTPS request is being made to host 'vcenter.ddiguru.net'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings\n  InsecureRequestWarning,\n/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py:986: InsecureRequestWarning: Unverified HTTPS request is being made to host 'vcenter.ddiguru.net'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings\n  InsecureRequestWarning,\nTraceback (most recent call last):\n  File \"/usr/local/lib/python3.7/site-packages/vmware/vapi/bindings/converter.py\", line 251, in _visit_vapi_struct\n    raise AttributeError\nAttributeError\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n  File \"/Users/ppiper/.ansible/tmp/ansible-tmp-1588623093.0298822-35766-66306551630703/AnsiballZ_vmware_content_deploy_ovf_template.py\", line 102, in <module>\n    _ansiballz_main()\n  File \"/Users/ppiper/.ansible/tmp/ansible-tmp-1588623093.0298822-35766-66306551630703/AnsiballZ_vmware_content_deploy_ovf_template.py\", line 94, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/Users/ppiper/.ansible/tmp/ansible-tmp-1588623093.0298822-35766-66306551630703/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/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py\", line 205, in run_module\n    return _run_module_code(code, init_globals, run_name, mod_spec)\n  File \"/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py\", line 96, in _run_module_code\n    mod_name, mod_spec, pkg_name, script_name)\n  File \"/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/runpy.py\", line 85, in _run_code\n    exec(code, run_globals)\n  File \"/var/folders/zs/v62z4ydn6f757kkthdr67z_r0000gn/T/ansible_community.vmware.vmware_content_deploy_ovf_template_payload_ffs446d1/ansible_community.vmware.vmware_content_deploy_ovf_template_payload.zip/ansible_collections/community/vmware/plugins/modules/vmware_content_deploy_ovf_template.py\", line 235, in <module>\n  File \"/var/folders/zs/v62z4ydn6f757kkthdr67z_r0000gn/T/ansible_community.vmware.vmware_content_deploy_ovf_template_payload_ffs446d1/ansible_community.vmware.vmware_content_deploy_ovf_template_payload.zip/ansible_collections/community/vmware/plugins/modules/vmware_content_deploy_ovf_template.py\", line 231, in main\n  File \"/var/folders/zs/v62z4ydn6f757kkthdr67z_r0000gn/T/ansible_community.vmware.vmware_content_deploy_ovf_template_payload_ffs446d1/ansible_community.vmware.vmware_content_deploy_ovf_template_payload.zip/ansible_collections/community/vmware/plugins/modules/vmware_content_deploy_ovf_template.py\", line 169, in deploy_vm_from_ovf_template\n  File \"/usr/local/lib/python3.7/site-packages/com/vmware/vcenter/ovf_client.py\", line 2985, in filter\n    'target': target,\n  File \"/usr/local/lib/python3.7/site-packages/vmware/vapi/bindings/stub.py\", line 345, in _invoke\n    return self._api_interface.native_invoke(ctx, _method_name, kwargs)\n  File \"/usr/local/lib/python3.7/site-packages/vmware/vapi/bindings/stub.py\", line 230, in native_invoke\n    self._rest_converter_mode)\n  File \"/usr/local/lib/python3.7/site-packages/vmware/vapi/bindings/converter.py\", line 1117, in convert_to_vapi\n    binding_type.accept(visitor)\n  File \"/usr/local/lib/python3.7/site-packages/vmware/vapi/bindings/type.py\", line 40, in accept\n    visitor.visit(self)\n  File \"/usr/local/lib/python3.7/site-packages/vmware/vapi/lib/visitor.py\", line 43, in visit\n    return method(value)\n  File \"/usr/local/lib/python3.7/site-packages/vmware/vapi/bindings/converter.py\", line 327, in visit_struct\n    self._visit_python_dict(typ)\n  File \"/usr/local/lib/python3.7/site-packages/vmware/vapi/bindings/converter.py\", line 306, in _visit_python_dict\n    self.visit(field_type)\n  File \"/usr/local/lib/python3.7/site-packages/vmware/vapi/lib/visitor.py\", line 43, in visit\n    return method(value)\n  File \"/usr/local/lib/python3.7/site-packages/vmware/vapi/bindings/converter.py\", line 434, in visit_reference\n    self.visit(typ.resolved_type)\n  File \"/usr/local/lib/python3.7/site-packages/vmware/vapi/lib/visitor.py\", line 43, in visit\n    return method(value)\n  File \"/usr/local/lib/python3.7/site-packages/vmware/vapi/bindings/converter.py\", line 319, in visit_struct\n    self._visit_vapi_struct(typ)\n  File \"/usr/local/lib/python3.7/site-packages/vmware/vapi/bindings/converter.py\", line 257, in _visit_vapi_struct\n    raise CoreException(msg)\nvmware.vapi.exception.CoreException: Field resource_pool_id missing from Structure com.vmware.vcenter.ovf.library_item.deployment_target\n/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py:986: InsecureRequestWarning: Unverified HTTPS request is being made to host 'vcenter.ddiguru.net'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings\n  InsecureRequestWarning,\n",
    "module_stdout": "",
    "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
    "rc": 1
}
```



Discussion:
Current Python modules installed include:
```
Package                            Version
---------------------------------- ------------------------
ansible                            2.9.7
asn1crypto                         1.3.0
certifi                            2020.4.5.1
cffi                               1.14.0
chardet                            3.0.4
click                              7.1.2
colorama                           0.4.3
cryptography                       2.9.2
dnspython                          1.16.0
docopt                             0.6.2
et-xmlfile                         1.0.1
Faker                              4.0.3
ibx                                0.0.post0.dev17+g2bee154
idna                               2.9
iptools                            0.7.0
jdcal                              1.4.1
Jinja2                             2.11.2
lxml                               4.5.0
MarkupSafe                         1.1.1
mysql                              0.0.2
mysql-connector                    2.2.9
mysqlclient                        1.4.6
netaddr                            0.7.19
nsx-policy-python-sdk              2.5.1.0.1.15419398
nsx-python-sdk                     2.5.1.0.1.15419398
nsx-vmc-aws-integration-python-sdk 2.5.1.0.1.15419398
nsx-vmc-policy-python-sdk          2.5.1.0.1.15419398
openpyxl                           3.0.3
passlib                            1.7.2
pexpect                            4.8.0
pip                                20.1
prompt-toolkit                     3.0.5
protobuf                           3.11.4
ptyprocess                         0.6.0
pycodestyle                        2.5.0
pycparser                          2.20
pyflakes                           2.2.0
Pygments                           2.6.1
pyOpenSSL                          19.1.0
python-dateutil                    2.8.1
python-dotenv                      0.13.0
python-nmcli                       0.1.1
pyvmomi                            7.0
PyYAML                             5.3.1
requests                           2.23.0
setuptools                         46.1.3
six                                1.14.0
suds-jurko                         0.6
termcolor                          1.1.0
text-unidecode                     1.3
tqdm                               4.46.0
urllib3                            1.25.9
vapi-client-bindings               3.2.0
vapi-common-client                 2.14.0
vapi-runtime                       2.14.0
vmc-client-bindings                1.23.0
vmc-draas-client-bindings          1.3.0
vSphere-Automation-SDK             1.25.0
wcwidth                            0.1.9
wheel                              0.34.2
```
cc @pgbidkar @anusha94 
cc @ngp-star 
is there a solution to the problem? @ultral @anusha94 @pgbidkar 
cc @Tomorrow9 @goneri @lparkes @warthog9
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/plugins/modules/vmware_content_deploy_ovf_template.py b/plugins/modules/vmware_content_deploy_ovf_template.py
--- a/plugins/modules/vmware_content_deploy_ovf_template.py
+++ b/plugins/modules/vmware_content_deploy_ovf_template.py
@@ -52,14 +52,14 @@
       required: True
     host:
       description:
-      - Name of the ESX Host in datacenter in which to place deployed VM.
+      - Name of the ESX Host in datacenter in which to place deployed VM. The host has to be a member of the cluster that contains the resource pool.
       type: str
       required: True
     resource_pool:
       description:
       - Name of the resourcepool in datacenter in which to place deployed VM.
       type: str
-      required: False
+      required: True
     cluster:
       description:
       - Name of the cluster in datacenter in which to place deployed VM.
@@ -165,11 +165,9 @@ def deploy_vm_from_ovf_template(self):
         if not self.host_id:
             self.module.fail_json(msg="Failed to find the Host %s" % self.host)
         # Find the resourcepool by the given resourcepool name
-        self.resourcepool_id = None
-        if self.resourcepool:
-            self.resourcepool_id = self.get_resource_pool_by_name(self.datacenter, self.resourcepool)
-            if not self.resourcepool_id:
-                self.module.fail_json(msg="Failed to find the resource_pool %s" % self.resourcepool)
+        self.resourcepool_id = self.get_resource_pool_by_name(self.datacenter, self.resourcepool)
+        if not self.resourcepool_id:
+            self.module.fail_json(msg="Failed to find the resource_pool %s" % self.resourcepool)
         # Find the Cluster by the given Cluster name
         self.cluster_id = None
         if self.cluster:
@@ -230,7 +228,7 @@ def main():
         datastore=dict(type='str', required=True),
         folder=dict(type='str', required=True),
         host=dict(type='str', required=True),
-        resource_pool=dict(type='str', required=False),
+        resource_pool=dict(type='str', required=True),
         cluster=dict(type='str', required=False),
         storage_provisioning=dict(type='str',
                                   required=False,


Test patch: