Repository: ansible-collections/community.vmware

Issue:
community.vmware.vmware_cluster_vsan module fails with a newly created vSphere 7.0U2 cluster
##### SUMMARY
After deploying a new vSphere 7.0 Update 2 cluster, I try to use the vmware_cluster_vsan module to enable vSAN and automatically claim the storage, and the module fails.  This failure also occurs even when attempt to enable vSAN by itself.  Latest vSAN SDK v7.0U1 (Latest) is downloaded, and vsanmgmtObjects.py and vsanapiutils.py files were placed in the Python include path.

The following two configurations were tested, and both failed with the same message:
1) Cache Disk: Flash, CapacityDisk: Flash
2) Cache Disk: Flash, CapacityDisk: HDD

I have not tried running this against any other version of vSphere.

vSAN was not enabled on the cluster before the playbook was executed, as it's expected that the module would enable vsan, and when "vsan_auto_claim_storage" is specified, automatically claim the drives into a storage group.

The SDDC.Lab environment is a nested environment, but vSAN works without issues when manually configured.

One final comment.  It would be really nice if this module had the ability to specify the vSAN Datastore name via an optional "datastore_name" variable, so that we could have the newly created datastore be named something other than "vsanDatastore".  Of course, if "datastore_name" is not included, it would default to "vsanDatastore".


##### ISSUE TYPE
- Bug Report

##### COMPONENT NAME
community.vmware.vmware_cluster_vsan

##### ANSIBLE VERSION
```
adminuser@NetLab-Ansible:~/git/SDDC.Lab$ ansible --version
ansible 2.10.7
  config file = /home/adminuser/git/SDDC.Lab/ansible.cfg
  configured module search path = ['/home/adminuser/git/SDDC.Lab/library']
  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
```
adminuser@NetLab-Ansible:~/git/SDDC.Lab$ ansible-config dump --only-changed
DEFAULT_CALLBACK_WHITELIST(/home/adminuser/git/SDDC.Lab/ansible.cfg) = ['profile_tasks']
DEFAULT_GATHERING(/home/adminuser/git/SDDC.Lab/ansible.cfg) = explicit
DEFAULT_HOST_LIST(/home/adminuser/git/SDDC.Lab/ansible.cfg) = ['/home/adminuser/git/SDDC.Lab/hosts/hosts']
DEFAULT_JINJA2_EXTENSIONS(/home/adminuser/git/SDDC.Lab/ansible.cfg) = jinja2.ext.do,jinja2.ext.i18n
DEFAULT_MODULE_PATH(/home/adminuser/git/SDDC.Lab/ansible.cfg) = ['/home/adminuser/git/SDDC.Lab/library']
DEFAULT_MODULE_UTILS_PATH(/home/adminuser/git/SDDC.Lab/ansible.cfg) = ['/home/adminuser/git/SDDC.Lab/module_utils']
INTERPRETER_PYTHON(/home/adminuser/git/SDDC.Lab/ansible.cfg) = /usr/bin/python3
LOCALHOST_WARNING(/home/adminuser/git/SDDC.Lab/ansible.cfg) = False
```

##### OS / ENVIRONMENT
Ansible:
Linux Ubuntu 20.04.2 Desktop

vCenter Server:
v7.02 Update 2

ESXi:
```
[root@Pod-200-ComputeA-1:~] esxcli system version get
   Product: VMware ESXi
   Version: 7.0.2
   Build: Releasebuild-17630552
   Update: 2
   Patch: 0
```

##### STEPS TO REPRODUCE
Either of the two following plays will demonstrate the same failure condition:

```yaml
    - name: Enable vSAN
      community.vmware.vmware_cluster_vsan:
        hostname: pod-200-vcenter.sddc.lab
        username: administrator@vsphere.local
        password: VMware1!
        validate_certs: false
        datacenter_name: Pod-200-DataCenter
        cluster_name: Compute-A
        enable_vsan: True
      delegate_to: localhost

    - name: Enable vSAN and claim storage automatically
      community.vmware.vmware_cluster_vsan:
        hostname: pod-200-vcenter.sddc.lab
        username: administrator@vsphere.local
        password: VMware1!
        validate_certs: false
        datacenter_name: Pod-200-DataCenter
        cluster_name: Compute-A
        enable_vsan: True
        vsan_auto_claim_storage: True
      delegate_to: localhost

```


##### EXPECTED RESULTS
I was expecting the vSAN cluster to have the vSAN Service enabled, configured with default values, and in the case of the 2nd play, have all of the drives automatically claimed into a storage group.  The module should be able to handle both Flash/HDD and Flash/Flash configurations.


##### ACTUAL RESULTS
When the plays are executed, they fail with the following exception error:
**"msg": "Failed to update cluster due to generic exception 'VMwareCluster' object has no attribute 'vsanClusterConfigSystem'"**

```paste below
adminuser@NetLab-Ansible:~/git/SDDC.Lab$ 
adminuser@NetLab-Ansible:~/git/SDDC.Lab$ ansible-playbook playbooks/configureVsan-test.yml -vvvv
ansible-playbook 2.10.7
  config file = /home/adminuser/git/SDDC.Lab/ansible.cfg
  configured module search path = ['/home/adminuser/git/SDDC.Lab/library']
  ansible python module location = /usr/local/lib/python3.8/dist-packages/ansible
  executable location = /usr/local/bin/ansible-playbook
  python version = 3.8.5 (default, Jan 27 2021, 15:41:15) [GCC 9.3.0]
Using /home/adminuser/git/SDDC.Lab/ansible.cfg as config file
setting up inventory plugins
host_list declined parsing /home/adminuser/git/SDDC.Lab/hosts/hosts as it did not pass its verify_file() method
script declined parsing /home/adminuser/git/SDDC.Lab/hosts/hosts as it did not pass its verify_file() method
auto declined parsing /home/adminuser/git/SDDC.Lab/hosts/hosts as it did not pass its verify_file() method
Set default localhost to localhost
Parsed /home/adminuser/git/SDDC.Lab/hosts/hosts inventory source with ini plugin
Loading collection community.vmware from /home/adminuser/.ansible/collections/ansible_collections/community/vmware
Loading callback plugin default of type stdout, v2.0 from /usr/local/lib/python3.8/dist-packages/ansible/plugins/callback/default.py
redirecting (type: callback) ansible.builtin.profile_tasks to ansible.posix.profile_tasks
Loading collection ansible.posix from /usr/local/lib/python3.8/dist-packages/ansible_collections/ansible/posix
Skipping callback 'default', as we already have a stdout callback.
Skipping callback 'minimal', as we already have a stdout callback.
Skipping callback 'oneline', as we already have a stdout callback.
Loading callback plugin ansible.posix.profile_tasks of type aggregate, v2.0 from /usr/local/lib/python3.8/dist-packages/ansible_collections/ansible/posix/plugins/callback/profile_tasks.py

PLAYBOOK: configureVsan-test.yml **********************************************************************************************************************************************************************************************************************************************************
Positional arguments: playbooks/configureVsan-test.yml
verbosity: 4
connection: smart
timeout: 10
become_method: sudo
tags: ('all',)
inventory: ('/home/adminuser/git/SDDC.Lab/hosts/hosts',)
forks: 5
1 plays in playbooks/configureVsan-test.yml

PLAY [localhost] **************************************************************************************************************************************************************************************************************************************************************************
META: ran handlers

TASK [Enable vSAN] ************************************************************************************************************************************************************************************************************************************************************************
task path: /home/adminuser/git/SDDC.Lab/playbooks/configureVsan-test.yml:10
Friday 19 March 2021  23:33:31 -0700 (0:00:00.047)       0:00:00.047 ********** 
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: adminuser
<localhost> EXEC /bin/sh -c 'echo ~adminuser && sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/adminuser/.ansible/tmp `"&& mkdir "` echo /home/adminuser/.ansible/tmp/ansible-tmp-1616222011.4441807-127817-80833664684210 `" && echo ansible-tmp-1616222011.4441807-127817-80833664684210="` echo /home/adminuser/.ansible/tmp/ansible-tmp-1616222011.4441807-127817-80833664684210 `" ) && sleep 0'
Using module file /home/adminuser/.ansible/collections/ansible_collections/community/vmware/plugins/modules/vmware_cluster_vsan.py
<localhost> PUT /home/adminuser/.ansible/tmp/ansible-local-127813rstjmplb/tmpxbmpjd5j TO /home/adminuser/.ansible/tmp/ansible-tmp-1616222011.4441807-127817-80833664684210/AnsiballZ_vmware_cluster_vsan.py
<localhost> EXEC /bin/sh -c 'chmod u+x /home/adminuser/.ansible/tmp/ansible-tmp-1616222011.4441807-127817-80833664684210/ /home/adminuser/.ansible/tmp/ansible-tmp-1616222011.4441807-127817-80833664684210/AnsiballZ_vmware_cluster_vsan.py && sleep 0'
<localhost> EXEC /bin/sh -c '/usr/bin/python3 /home/adminuser/.ansible/tmp/ansible-tmp-1616222011.4441807-127817-80833664684210/AnsiballZ_vmware_cluster_vsan.py && sleep 0'
<localhost> EXEC /bin/sh -c 'rm -f -r /home/adminuser/.ansible/tmp/ansible-tmp-1616222011.4441807-127817-80833664684210/ > /dev/null 2>&1 && sleep 0'
The full traceback is:
  File "/tmp/ansible_community.vmware.vmware_cluster_vsan_payload_u9hb59h0/ansible_community.vmware.vmware_cluster_vsan_payload.zip/ansible_collections/community/vmware/plugins/modules/vmware_cluster_vsan.py", line 236, in configure_vsan
fatal: [localhost]: FAILED! => {
    "changed": false,
    "invocation": {
        "module_args": {
            "advanced_options": null,
            "cluster_name": "Compute-A",
            "datacenter": "Pod-200-DataCenter",
            "datacenter_name": "Pod-200-DataCenter",
            "enable_vsan": true,
            "hostname": "pod-200-vcenter.sddc.lab",
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "port": 443,
            "proxy_host": null,
            "proxy_port": null,
            "username": "administrator@vsphere.local",
            "validate_certs": false,
            "vsan_auto_claim_storage": false
        }
    },
    "msg": "Failed to update cluster due to generic exception 'VMwareCluster' object has no attribute 'vsanClusterConfigSystem'"
}

PLAY RECAP ********************************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

Friday 19 March 2021  23:33:32 -0700 (0:00:00.747)       0:00:00.795 ********** 
=============================================================================== 
Enable vSAN ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 0.75s
/home/adminuser/git/SDDC.Lab/playbooks/configureVsan-test.yml:10 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
adminuser@NetLab-Ansible:~/git/SDDC.Lab$ 
```



Discussion:
Files identified in the description:
* [`plugins/modules/vmware_cluster_vsan.py`](https://github.com/['ansible-collections/amazon.aws', 'ansible-collections/community.aws', 'ansible-collections/community.vmware']/blob/main/plugins/modules/vmware_cluster_vsan.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @Akasurde @Tomorrow9 @goneri @lparkes @mariolenz @pgbidkar @warthog9
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
Files identified in the description:
None

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
@luischanu Thanks for reporting this issue. 

@mariolenz I think this is due to https://github.com/ansible-collections/community.vmware/pull/289/files#diff-b0727b9b194a50c77d9e7fe10435a35a072a90f0934c612b9fbaec111d4f0a7cR237. Would you be able to tackle this issue? Thanks in advance.

Resolving patch:
diff --git a/plugins/modules/vmware_cluster_vsan.py b/plugins/modules/vmware_cluster_vsan.py
--- a/plugins/modules/vmware_cluster_vsan.py
+++ b/plugins/modules/vmware_cluster_vsan.py
@@ -162,11 +162,12 @@ def __init__(self, module):
 
         if module.params['advanced_options'] is not None:
             self.advanced_options = module.params['advanced_options']
-            client_stub = self.si._GetStub()
-            ssl_context = client_stub.schemeArgs.get('context')
-            apiVersion = vsanapiutils.GetLatestVmodlVersion(module.params['hostname'])
-            vcMos = vsanapiutils.GetVsanVcMos(client_stub, context=ssl_context, version=apiVersion)
-            self.vsanClusterConfigSystem = vcMos['vsan-cluster-config-system']
+
+        client_stub = self.si._GetStub()
+        ssl_context = client_stub.schemeArgs.get('context')
+        apiVersion = vsanapiutils.GetLatestVmodlVersion(module.params['hostname'])
+        vcMos = vsanapiutils.GetVsanVcMos(client_stub, context=ssl_context, version=apiVersion)
+        self.vsanClusterConfigSystem = vcMos['vsan-cluster-config-system']
 
     def check_vsan_config_diff(self):
         """


Test patch: