Repository: ansible-collections/community.vmware

Issue:
vmware_guest create vm with hostname None
##### SUMMARY
From ansible 2.10.7 vmware_guest module can't set guest os hostname. Hostname looks like 'root@None'. So it seems that something became None in module.

##### ISSUE TYPE
- Bug Report

##### COMPONENT NAME
vmware_guest

##### ANSIBLE VERSION
ansible --version is also broken. Now we can't ran --version without passing some other args!!


##### OS / ENVIRONMENT
os x 11.1
ansible 2.10.7 (brew)
python 3.9


##### STEPS TO REPRODUCE
create vm with vmware_guest module

##### EXPECTED RESULTS
should see hostname like set in inventory

##### ACTUAL RESULTS
hostname inside vm would be None.


Discussion:
Files identified in the description:
* [`plugins/modules/vmware_guest.py`](https://github.com/['ansible-collections/amazon.aws', 'ansible-collections/community.aws', 'ansible-collections/community.vmware']/blob/main/plugins/modules/vmware_guest.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 @nerzhul @pdellaert @pgbidkar @warthog9
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
I am also seeing the same issue with a playbook like this:
```yaml
- hosts: localhost
  tasks:
    - name: Test vm provision
      community.vmware.vmware_guest:
        hostname: "{{ vmware_orchestration_login.hostname }}"
        username: "{{ vmware_orchestration_login.username }}"
        password: "{{ vmware_orchestration_login.password }}"
        validate_certs: "{{ vmware_orchestration_login.validate_certs }}"
        folder: "{{ vmware_folder }}"
        name: "test-host-name"
        state: poweredon
        template: "{{ vmware_template }}"
        cluster: "{{ vmware_orchestration_env.cluster }}"
        datacenter: "{{ vmware_orchestration_env.datacenter }}"
        disk:
          - size_gb: 50
        hardware:
          memory_mb: 32768
          num_cpus: 8
          scsi: paravirtual
        networks:
          - name: "{{ vmware_network_name }}"
            device_type: vmxnet3
        datastore: vsanDatastore
```

ansible --version
```
$ ansible --version
ansible 2.10.7rc1
  config file = ./ansible.cfg
  configured module search path = ['~/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = ~/.local/lib/python3.6/site-packages/ansible
  executable location = ~/.local/bin/ansible
  python version = 3.6.8 (default, Nov 16 2020, 16:55:22) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
```

It looks like the issue is https://github.com/ansible-collections/community.vmware/blob/902710c6490f075d53e5cb532ad98d4034f13227/plugins/modules/vmware_guest.py#L2379
```python
hostname = str(self.params['customization'].get('hostname', default_name.split('.')[0]))
```
It seems that now, ansible returns "None" from this rather than using the default_name variable.

As a workaround, I can set the customization.hostname variable in my playbook, and everything works fine, but it's not using the "name" variable as a default hostname correctly as it should do according to the docs.
@ollie1 Would you be willing to submit a PR for this?

Resolving patch:
diff --git a/plugins/modules/vmware_guest.py b/plugins/modules/vmware_guest.py
--- a/plugins/modules/vmware_guest.py
+++ b/plugins/modules/vmware_guest.py
@@ -2324,7 +2324,12 @@ def customize_vm(self, vm_obj):
                 default_name = vm_obj.name.replace(' ', '')
             punctuation = string.punctuation.replace('-', '')
             default_name = ''.join([c for c in default_name if c not in punctuation])
-            ident.userData.computerName.name = str(self.params['customization'].get('hostname', default_name[0:15]))
+
+            if self.params['customization']['hostname'] is not None:
+                ident.userData.computerName.name = self.params['customization']['hostname'][0:15]
+            else:
+                ident.userData.computerName.name = default_name[0:15]
+
             ident.userData.fullName = str(self.params['customization'].get('fullname', 'Administrator'))
             ident.userData.orgName = str(self.params['customization'].get('orgname', 'ACME'))
 
@@ -2385,7 +2390,12 @@ def customize_vm(self, vm_obj):
                 default_name = self.params['name']
             elif vm_obj:
                 default_name = vm_obj.name
-            hostname = str(self.params['customization'].get('hostname', default_name.split('.')[0]))
+
+            if self.params['customization']['hostname'] is not None:
+                hostname = self.params['customization']['hostname'].split('.')[0]
+            else:
+                hostname = default_name.split('.')[0]
+
             # Remove all characters except alphanumeric and minus which is allowed by RFC 952
             valid_hostname = re.sub(r"[^a-zA-Z0-9\-]", "", hostname)
             ident.hostName.name = valid_hostname


Test patch: