Repository: ansible-collections/community.vmware

Issue:
Allow encrypted variables in dynamic inventory configuraiton
<!--- 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 -->
Support ansible vault variables for username and password
##### ISSUE TYPE
- Feature Idea

##### COMPONENT NAME
<!--- Write the short name of the module, plugin, task or feature below, use your best guess if unsure -->
vmware_vm_inventory
##### ADDITIONAL INFORMATION
<!--- Describe how the feature would be used, why it is needed and what it would solve -->
Currently, you can not use encrypted variables in the inventory file. You receive the following error.

I was attempting to encrypt just the username and password for the inventory, but that was unable to function. If you encrypt the entire contents of the file, it does work okay.

vmwaretest ansible -m ansible.windows.win_whoami _domain_controllers -l dtw
[WARNING]:  * Failed to parse /vmwaretest/inventories/production/{{ vcenter }}.vmware.yml with
ansible_collections.community.vmware.plugins.inventory.vmware_vm_inventory plugin: Unknown error while connecting to vCenter or ESXi API at {{ vcenter }}:443: For "userName" expected type str, but got AnsibleVaultEncryptedUnicode
[WARNING]: Unable to parse /vmwaretest/inventories/production/{{ vcenter }}.vmware.yml as an inventory source
[WARNING]: Could not match supplied host pattern, ignoring: dtw
<!--- Paste example playbooks or commands between quotes below -->
```yaml
-- NA --
```

<!--- HINT: You can also paste gist.github.com links for larger files -->



Discussion:
@usscarter Thanks for reporting this issue. `password` is supported as encrypted variables - https://github.com/ansible-collections/community.vmware/blob/bbd98e5e182b6b965f52eedc4a112370872d55eb/plugins/inventory/vmware_vm_inventory.py#L721
Well, that is good to know atleast. 

We also prefer to encrypt the username field as we use a service account so that would also be preferable in the future as well 👍 
ok. On it.
cc @Tomorrow9 @goneri @pgbidkar @warthog9
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/plugins/inventory/vmware_host_inventory.py b/plugins/inventory/vmware_host_inventory.py
--- a/plugins/inventory/vmware_host_inventory.py
+++ b/plugins/inventory/vmware_host_inventory.py
@@ -35,13 +35,17 @@
               - name: VMWARE_HOST
               - name: VMWARE_SERVER
         username:
-            description: Name of vSphere user.
+            description:
+            - Name of vSphere user.
+            - Accepts vault encrypted variable.
             required: True
             env:
               - name: VMWARE_USER
               - name: VMWARE_USERNAME
         password:
-            description: Password of vSphere user.
+            description:
+            - Password of vSphere user.
+            - Accepts vault encrypted variable.
             required: True
             env:
               - name: VMWARE_PASSWORD
@@ -202,14 +206,18 @@ def parse(self, inventory, loader, path, cache=True):
         # set _options from config data
         self._consume_options(config_data)
 
+        username = self.get_option("username")
         password = self.get_option("password")
 
+        if isinstance(username, AnsibleVaultEncryptedUnicode):
+            username = username.data
+
         if isinstance(password, AnsibleVaultEncryptedUnicode):
             password = password.data
 
         self.pyv = BaseVMwareInventory(
             hostname=self.get_option("hostname"),
-            username=self.get_option("username"),
+            username=username,
             password=password,
             port=self.get_option("port"),
             with_tags=self.get_option("with_tags"),
diff --git a/plugins/inventory/vmware_vm_inventory.py b/plugins/inventory/vmware_vm_inventory.py
--- a/plugins/inventory/vmware_vm_inventory.py
+++ b/plugins/inventory/vmware_vm_inventory.py
@@ -33,13 +33,17 @@
               - name: VMWARE_HOST
               - name: VMWARE_SERVER
         username:
-            description: Name of vSphere user.
+            description:
+            - Name of vSphere user.
+            - Accepts vault encrypted variable.
             required: True
             env:
               - name: VMWARE_USER
               - name: VMWARE_USERNAME
         password:
-            description: Password of vSphere user.
+            description:
+            - Password of vSphere user.
+            - Accepts vault encrypted variable.
             required: True
             env:
               - name: VMWARE_PASSWORD
@@ -716,14 +720,18 @@ def parse(self, inventory, loader, path, cache=True):
         # set _options from config data
         self._consume_options(config_data)
 
+        username = self.get_option('username')
         password = self.get_option('password')
 
+        if isinstance(username, AnsibleVaultEncryptedUnicode):
+            username = username.data
+
         if isinstance(password, AnsibleVaultEncryptedUnicode):
             password = password.data
 
         self.pyv = BaseVMwareInventory(
             hostname=self.get_option('hostname'),
-            username=self.get_option('username'),
+            username=username,
             password=password,
             port=self.get_option('port'),
             with_tags=self.get_option('with_tags'),


Test patch: