Repository: ansible-collections/community.vmware

Issue:
vmware_content_deploy_ovf_template does not respet resource_pool parameter
<!--- 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
<!--- Explain the problem briefly below -->
When creating a VM from an OVF template in a Content Library, no matter what resource_pool you try to configure ( either it does exist or not )  the VM get created in the top level resource pool ( if such thing exist ).
##### ISSUE TYPE
- Bug Report

##### COMPONENT NAME
<!--- Write the short name of the module, plugin, task or feature below, use your best guess if unsure -->
vmware_content_deploy_ovf_template
##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes -->
```paste below
ansible --version
ansible 2.9.20
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/pigi/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib64/python3.9/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.9.10 (main, Jan 16 2022, 02:41:55) [GCC 11.2.0]

```

##### 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

```

##### CONFIGURATION
<!--- Paste verbatim output from "ansible-config dump --only-changed" between quotes -->
```paste below

```

##### OS / ENVIRONMENT
<!--- Provide all relevant information below, e.g. target OS versions, network device firmware, etc. -->


##### STEPS TO REPRODUCE
<!--- Describe exactly how to reproduce the problem, using a minimal test-case -->
Have a vmware environment with some resource pools.
Create a Content Library and upload an ovf
Try to create a VM from this ovf in a specific resource pool

<!--- Paste example playbooks or commands between quotes below -->
```yaml
- name: CreateBootStrap
  user: root
  hosts: localhost
  #become: true
  gather_facts: false
  vars:
# Define your username and password here that you want to create on target hosts.
    vcenter_hostname: vcsa67
    vcenter_username: pigi
    vcenter_password: Passw0rd
    OCP_template: rhcos-vmware.x86_64
    OCP_Library: Lib_CL_RHCOS
    Cluster: "Compute LAB Cluster"
    DataStore: Compute_03-Sistemi
    folderVM: "Pigi_folder_vm"
    DataCenter: "LAB Datacenter"
    NameVM:  "Test_for_OCP"
    ResPool: "OCP_TST"

   - name: create  machine from template. 
      community.vmware.vmware_content_deploy_ovf_template:
        cluster: '{{ Cluster }}'
        hostname: '{{ vcenter_hostname }}'
        username: '{{ vcenter_username }}'
        password: '{{ vcenter_password }}'
        template: '{{ OCP_template }}'
        content_library:  '{{ OCP_Library }}'
        datastore: '{{ DataStore }}'
        folder: '{{ folderVM }}'
        datacenter: '{{ DataCenter }}'
        name: '{{ NameVM }}'
        resource_pool: '{{ ResPool }}'
        storage_provisioning: thin
        validate_certs: no 
      delegate_to: localhost
```

<!--- HINT: You can paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- Describe what you expected to happen when running the steps above -->
I expepct that the VM would be created in the OCP_TST resource pool

##### ACTUAL RESULTS
<!--- Describe what actually happened. If possible run with extra verbosity (-vvvv) -->
The VM get created in the correct cluster but outside the resource pool

<!--- Paste verbatim command output between quotes -->
```paste below

```



Discussion:
This bug is still available in the latest version of vmware ansible collections.
Its taking the `resource_pool` value but there is no code to handle it.

Issue seems to be in https://github.com/ansible-collections/community.vmware/blob/5326613d98fe55cd2298ea7a361b2d5778f0a2f9/plugins/modules/vmware_content_deploy_template.py#L283 line. Its trying to get the resource pool details using `get_resource_pool_by_name` function. But that function itself is not available. Hence no activity is happening at that lebel.

- My installed version of collection:

```
$ ansible-galaxy collection list community.vmware

# /usr/local/lib/python3.8/dist-packages/ansible_collections
Collection       Version
---------------- -------
community.vmware 1.18.0
```

Can we get this fixed ?
@Udayendu This issue is about the `vmware_content_deploy_ovf_template` module. But you're linking to the `vmware_content_deploy_template`:
 
> Issue seems to be in
> 
> https://github.com/ansible-collections/community.vmware/blob/5326613d98fe55cd2298ea7a361b2d5778f0a2f9/plugins/modules/vmware_content_deploy_template.py#L283

Do you run into this issue with `vmware_content_deploy_ovf_template`, with `vmware_content_deploy_template` or with both?

> Its trying to get the resource pool details using `get_resource_pool_by_name` function. But that function itself is not available. Hence no activity is happening at that lebel.

I think you're wrong there. You see, if this method wouldn't be available I think the module would crash. And, anyway, the method _is_ defined in `VmwareRestClient` from where `VmwareContentDeployTemplate` inherits it:

https://github.com/ansible-collections/community.vmware/blob/1f295a8dbf7745e051c1e7f2fccae79829a6c6cb/plugins/module_utils/vmware_rest_client.py#L370-L375
@Pigi-102 It looks like the module searches for the resource pool only if both `cluster` and `host` are defined:

https://github.com/ansible-collections/community.vmware/blob/1f295a8dbf7745e051c1e7f2fccae79829a6c6cb/plugins/modules/vmware_content_deploy_ovf_template.py#L262-L264

Could you please try to deploy to a specific host in the cluster? I'd like to see what happens in this case. It would help me to fix this.
@n3pjk Could you have a look at this? You've been the last one to work on this module, so I thought you might be interested.
Sure thing! I don't use resource pools since they're evil, but I'll have a look.

Sent from my Verizon, Samsung Galaxy smartphone
Get Outlook for Android<https://aka.ms/AAb9ysg>
________________________________
From: Mario Lenz ***@***.***>
Sent: Saturday, June 11, 2022 11:30:22 AM
To: ansible-collections/community.vmware ***@***.***>
Cc: Paul Knight ***@***.***>; Mention ***@***.***>
Subject: Re: [ansible-collections/community.vmware] vmware_content_deploy_ovf_template does not respet resource_pool parameter (Issue #1290)


@n3pjk<https://github.com/n3pjk> Could you have a look at this? You've been the last one to work on this module, so I thought you might be interested.

—
Reply to this email directly, view it on GitHub<https://github.com/ansible-collections/community.vmware/issues/1290#issuecomment-1152951458>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AA2LV6YAXZMZ5HAOO6EB5V3VOSWI5ANCNFSM5TQOIZMQ>.
You are receiving this because you were mentioned.Message ID: ***@***.***>

> @Pigi-102 It looks like the module searches for the resource pool only if both `cluster` and `host` are defined:
> 
> https://github.com/ansible-collections/community.vmware/blob/1f295a8dbf7745e051c1e7f2fccae79829a6c6cb/plugins/modules/vmware_content_deploy_ovf_template.py#L262-L264
> 
> Could you please try to deploy to a specific host in the cluster? I'd like to see what happens in this case. It would help me to fix this.

Hello,
  unfortunally at the moment I don't have access to an infrastructure to execute the test.
In case I will gain a new access I will try it.
Sorry.


@mariolenz,

Issue is with both the modules. As this bug was already opened, so I though I should include it here.
But if you want I can open another bug for `vmware_content_deploy_template` and map it here as patch should go to both the tools separately.


> Issue is with both the modules. As this bug was already opened, so I though I should include it here. But if you want I can open another bug for `vmware_content_deploy_template` and map it here as patch should go to both the tools separately.

@Udayendu I don't think that's necessary. Let's track both issues here. It's possibly the same basic problem because `vmware_content_deploy_template` also searches for the resource pool only if both `cluster` and `host` are defined

https://github.com/ansible-collections/community.vmware/blob/1f295a8dbf7745e051c1e7f2fccae79829a6c6cb/plugins/modules/vmware_content_deploy_template.py#L281-L283

Could you please specify both `host` and `cluster` in addition to `resource_pool`? If this works, I think we've already found the problem.
> > Issue is with both the modules. As this bug was already opened, so I though I should include it here. But if you want I can open another bug for `vmware_content_deploy_template` and map it here as patch should go to both the tools separately.
> 
> @Udayendu I don't think that's necessary. Let's track both issues here. It's possibly the same basic problem because `vmware_content_deploy_template` also searches for the resource pool only if both `cluster` and `host` are defined
> 
> https://github.com/ansible-collections/community.vmware/blob/1f295a8dbf7745e051c1e7f2fccae79829a6c6cb/plugins/modules/vmware_content_deploy_template.py#L281-L283
> 
> Could you please specify both `host` and `cluster` in addition to `resource_pool`? If this works, I think we've already found the problem.

So far I was just using the cluster & resource_pool. Never tried to use the host as cluster supposed to do that selection. But I will try and update you by Monday.
@mariolenz 
I have completed the testing and here is my observation:
- Its able to deploy the vm under resource pool if cluster, resource pool and esxi hosts are selected at a time.
- ESXi server should be 6.7 and higher. Initial I tried on couple of ESXi 6.5 hosts and it failed with a compatibility error message.


I see the problem. In both modules, cluster and host are in the if clause. I think removing them from the clause should correct the problem. I will submit a PR later today.

Sent from my Verizon, Samsung Galaxy smartphone
Get Outlook for Android<https://aka.ms/AAb9ysg>
________________________________
From: Udayendu Kar ***@***.***>
Sent: Monday, June 20, 2022 1:57:29 AM
To: ansible-collections/community.vmware ***@***.***>
Cc: Paul Knight ***@***.***>; Mention ***@***.***>
Subject: Re: [ansible-collections/community.vmware] vmware_content_deploy_ovf_template does not respet resource_pool parameter (Issue #1290)


@mariolenz<https://github.com/mariolenz>
I have completed the testing and here is my observation:

  *   Its able to deploy the vm under resource pool if cluster, resource pool and esxi hosts are selected at a time.
  *   ESXi server should be 6.7 and higher. Initial I tried on couple of ESXi 6.5 hosts and it failed with a compatibility error message.

—
Reply to this email directly, view it on GitHub<https://github.com/ansible-collections/community.vmware/issues/1290#issuecomment-1160005247>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AA2LV67FVMCNNNBQ5OE3YT3VQAB4TANCNFSM5TQOIZMQ>.
You are receiving this because you were mentioned.Message ID: ***@***.***>


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
@@ -260,7 +260,7 @@ def deploy_vm_from_ovf_template(self):
             self._resourcepool_id = cluster_obj.resource_pool
 
         # Find the resourcepool by the given resourcepool name
-        if self.resourcepool and self.cluster and self.host:
+        if self.resourcepool:
             self._resourcepool_id = self.get_resource_pool_by_name(self.datacenter, self.resourcepool, self.cluster, self.host)
             if not self._resourcepool_id:
                 self._fail(msg="Failed to find the resource_pool %s" % self.resourcepool)
diff --git a/plugins/modules/vmware_content_deploy_template.py b/plugins/modules/vmware_content_deploy_template.py
--- a/plugins/modules/vmware_content_deploy_template.py
+++ b/plugins/modules/vmware_content_deploy_template.py
@@ -279,7 +279,7 @@ def deploy_vm_from_template(self, power_on=False):
             self._resourcepool_id = cluster_obj.resource_pool
 
         # Find the resourcepool by the given resourcepool name
-        if self.resourcepool and self.cluster and self.host:
+        if self.resourcepool:
             self._resourcepool_id = self.get_resource_pool_by_name(self.datacenter, self.resourcepool, self.cluster, self.host)
             if not self._resourcepool_id:
                 self._fail(msg="Failed to find the resource_pool %s" % self.resourcepool)


Test patch: