This guide will show you how to utilize VMware Connection plugin to communicate and automate various tasks on VMware guest machines.
Software
pip install Pyvmomi
on the Ansible control node (as the OS packages are usually out of date and incompatible) if you are planning to use any existing VMware modules.Hardware
Access / Credentials
validate_certs
option, as this version is capable of changing the SSL verification behaviors.User can run playbooks against VMware virtual machines using vmware_tools
connection plugin.
In order work with vmware_tools
connection plugin, you will need to specify hostvars for the given virtual machine.
For example, if you want to run a playbook on a virtual machine called centos_7
located at /Asia-Datacenter1/prod/centos_7
in the given vCenter, you will need to specify hostvars as follows:
[centos7] host1 [centos7:vars] # vmware_tools related variables ansible_connection=vmware_tools ansible_vmware_host=10.65.201.128 ansible_vmware_user=administrator@vsphere.local ansible_vmware_password=Esxi@123$% ansible_vmware_validate_certs=no # Location of the virtual machine ansible_vmware_guest_path=Asia-Datacenter1/vm/prod/centos_7 # Credentials ansible_vmware_tools_user=root ansible_vmware_tools_password=Secret123
Here, we are providing vCenter details and credentials for the given virtual machine to run the playbook on. If your virtual machine path is Asia-Datacenter1/prod/centos_7
, you specify ansible_vmware_guest_path
as Asia-Datacenter1/vm/prod/centos_7
. Please take a note that /vm
is added in the virtual machine path, since this is a logical folder structure in the VMware inventory.
Let us now run following playbook,
--- - name: Example showing VMware Connection plugin hosts: centos7 tasks: - name: Gather information about temporary directory inside VM shell: ls /tmp
Since Ansible utilizes the vmware-tools
or openvm-tools
service capabilities running in the virtual machine to perform actions, in this use case it will be connecting directly to the guest machine.
For now, you will be entering credentials in plain text, but in a more advanced playbook this can be abstracted out and stored in a more secure fashion using ansible-vault or using Ansible Tower credentials.
Running this playbook can take some time, depending on your environment and network connectivity. When the run is complete you will see:
{ "changed": true, "cmd": "ls /tmp", "delta": "0:00:00.005440", "end": "2020-10-01 07:30:56.940813", "rc": 0, "start": "2020-10-01 07:30:56.935373", "stderr": "", "stderr_lines": [], "stdout": "ansible_command_payload_JzWiL9\niso", "stdout_lines": ["ansible_command_payload_JzWiL9", "iso", "vmware-root"] }
If your playbook fails:
/vm/
needs to be provided while specifying virtual machine location.
© 2012–2018 Michael DeHaan
© 2018–2021 Red Hat, Inc.
Licensed under the GNU General Public License version 3.
https://docs.ansible.com/ansible/latest/scenario_guides/vmware_scenarios/scenario_vmware_tools_connection.html