VMware inventory plugin allows you to configure hostnames using the hostnames
configuration parameter.
In this scenario guide we will see how you configure hostnames from the given VMware guest in the inventory.
To use the VMware dynamic inventory plugins, you must install pyVmomi on your control node (the host running Ansible).
To include tag-related information for the virtual machines in your dynamic inventory, you also need the vSphere Automation SDK, which supports REST API features such as tagging and content libraries, on your control node. You can install the vSphere Automation SDK
following these instructions.
$ pip install pyvmomi
Starting in Ansible 2.10, the VMware dynamic inventory plugin is available in the community.vmware
collection included Ansible. To install the latest community.vmware
collection:
$ ansible-galaxy collection install community.vmware
To use this VMware dynamic inventory plugin:
ansible.cfg
file:[inventory] enable_plugins = community.vmware.vmware_vm_inventory
vmware.yml
or vmware.yaml
in your working directory.The vmware_vm_inventory
inventory plugin takes in the same authentication information as any other VMware modules does.
Here’s an example of a valid inventory file with custom hostname for the given VMware guest:
plugin: community.vmware.vmware_vm_inventory strict: False hostname: 10.65.223.31 username: [email protected] password: Esxi@123$% validate_certs: False with_tags: False hostnames: - config.name
Here, we have configured a custom hostname by setting the hostnames
parameter to config.name
. This will retrieve the config.name
property from the virtual machine and populate it in the inventory.
You can check all allowed properties for the given virtual machine at Using Virtual machine attributes in VMware dynamic inventory plugin.
Executing ansible-inventory --list -i <filename>.vmware.yml
creates a list of the virtual machines that are ready to be configured using Ansible.
You will notice that instead of default behavior of representing the hostname as config.name + _ + config.uuid
, the inventory hosts show value as config.name
.
{ "_meta": { "hostvars": { "template_001": { "config.name": "template_001", "guest.toolsRunningStatus": "guestToolsNotRunning", ... "guest.toolsStatus": "toolsNotInstalled", "name": "template_001" }, "vm_8046": { "config.name": "vm_8046", "guest.toolsRunningStatus": "guestToolsNotRunning", ... "guest.toolsStatus": "toolsNotInstalled", "name": "vm_8046" }, ... }
If the custom property specified in hostnames
fails:
strict: True
to get more information about the error.See also
The GitHub Page of pyVmomi
The issue tracker for the pyVmomi project
The GitHub Page of vSphere Automation SDK for Python
The issue tracker for vSphere Automation SDK for Python
Using Virtual machine attributes in VMware dynamic inventory plugin
An introduction to playbooks
Using Vault in playbooks
© 2012–2018 Michael DeHaan
© 2018–2019 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
https://docs.ansible.com/ansible/2.10/scenario_guides/vmware_scenarios/vmware_inventory_hostnames.html