Repository: ansible-collections/community.vmware

Issue:
vsphere_file: Add support for vSphere credentials via environment variables 
<!--- Verify first that your feature was not already discussed on GitHub -->
<!--- Complete *all* sections as described, this form is processed automatically -->

##### SUMMARY
<!--- Describe the new feature/improvement briefly below -->
Almost all modules support vSphere credentials via environment variables like `VMWARE_HOST`, `VMWARE_USER` and `VMWARE_PASSWORD`. I prefer to use these environment variables, because they work for the **community.vmware** and **vmware.vmware_rest** collections at the same time.

Unfortunately, in contrast to almost all other modules, `vsphere_file` requires the arguments `host`, `password` and `username` and does not use the above mentioned environment variables.

It would be great to also be able to pass vSphere credentials via environment variables to `vsphere_file`.

##### ISSUE TYPE
- Feature Idea

##### COMPONENT NAME
<!--- Write the short name of the module, plugin, task or feature below, use your best guess if unsure -->
vsphere_file

##### ADDITIONAL INFORMATION
<!--- Describe how the feature would be used, why it is needed and what it would solve -->
The below playbook should not fail with `missing required arguments: host, password, username`, but currently does.

<!--- Paste example playbooks or commands between quotes below -->
```yaml
- name: Use vsphere_file
  hosts: vcenter
  gather_facts: false
  connection: local
  environment:
    VMWARE_HOST: '{{ ansible_host }}'
    VMWARE_USER: '{{ ansible_user }}'
    VMWARE_PASSWORD: '{{ ansible_password }}'
    VMWARE_VALIDATE_CERTS: true
  tasks:
    - name: Check if file exists
      community.vmware.vsphere_file:
        datacenter: Datacenter
        datastore: datastore0
        path: test/test.txt
        state: file
      register: file_check
      ignore_errors: yes
```

<!--- HINT: You can also paste gist.github.com links for larger files -->



Discussion:
Great idea. However, I looked at the module itself. It's quite old (2017) and probably needs a lot more reworking.
> It's quite old (2017) and probably needs a lot more reworking.

Not the only one IMHO :smirk:

@mbx64 I have an idea how to implement this, although I'm not 100% sure. But TBH there are more pressing issues at the moment. Maybe I can find the time to have a look at this after we've released 4.1.0.

Resolving patch:
diff --git a/plugins/modules/vsphere_file.py b/plugins/modules/vsphere_file.py
--- a/plugins/modules/vsphere_file.py
+++ b/plugins/modules/vsphere_file.py
@@ -18,22 +18,6 @@
 author:
 - Dag Wieers (@dagwieers)
 options:
-  host:
-    description:
-    - The vCenter server on which the datastore is available.
-    type: str
-    required: true
-    aliases: [ hostname ]
-  username:
-    description:
-    - The user name to authenticate on the vCenter server.
-    type: str
-    required: true
-  password:
-    description:
-    - The password to authenticate on the vCenter server.
-    type: str
-    required: true
   datacenter:
     description:
     - The datacenter on the vCenter server that holds the datastore.
@@ -50,12 +34,6 @@
     type: str
     required: true
     aliases: [ dest ]
-  validate_certs:
-    description:
-    - If V(false), SSL certificates will not be validated. This should only be
-      set to V(false) when no other option exists.
-    type: bool
-    default: true
   timeout:
     description:
     - The timeout in seconds for the upload to the datastore.
@@ -73,6 +51,8 @@
     default: file
 notes:
 - The vSphere folder API does not allow to remove directory objects.
+extends_documentation_fragment:
+- community.vmware.vmware.documentation
 '''
 
 EXAMPLES = r'''
@@ -134,6 +114,7 @@
 from ansible.module_utils.six.moves.urllib.parse import quote, urlencode
 from ansible.module_utils.urls import open_url
 from ansible.module_utils._text import to_native
+from ansible_collections.community.vmware.plugins.module_utils.vmware import vmware_argument_spec
 
 
 def vmware_path(datastore, datacenter, path):
@@ -152,22 +133,22 @@ def vmware_path(datastore, datacenter, path):
 
 def main():
 
+    argument_spec = vmware_argument_spec()
+
+    argument_spec.update(dict(
+        datacenter=dict(type='str', required=True),
+        datastore=dict(type='str', required=True),
+        path=dict(type='str', required=True, aliases=['dest']),
+        state=dict(type='str', default='file', choices=['absent', 'directory', 'file', 'touch']),
+        timeout=dict(type='int', default=10),
+    ))
+
     module = AnsibleModule(
-        argument_spec=dict(
-            host=dict(type='str', required=True, aliases=['hostname']),
-            username=dict(type='str', required=True),
-            password=dict(type='str', required=True, no_log=True),
-            datacenter=dict(type='str', required=True),
-            datastore=dict(type='str', required=True),
-            path=dict(type='str', required=True, aliases=['dest']),
-            state=dict(type='str', default='file', choices=['absent', 'directory', 'file', 'touch']),
-            timeout=dict(type='int', default=10),
-            validate_certs=dict(type='bool', default=True),
-        ),
+        argument_spec=argument_spec,
         supports_check_mode=True,
     )
 
-    host = module.params.get('host')
+    host = module.params.get('hostname')
     username = module.params.get('username')
     password = module.params.get('password')
     datacenter = module.params.get('datacenter')


Test patch: