You can use Ansible to automate some tasks in your VMware infrastructure. You can automatically deploy and configure ESXi hosts, manage network settings, start, stop, or delete VMs, deploy virtual machines from templates, install updates on ESXi hosts, etc. This post explains how you can get started with using Ansible to manage your VMware environment.
Ansible has a large number of modules for managing VMware infrastructure and most of them are based on pyVmomi (a Python SDK that allows you to connect to VMware vSphere API). Install pyVmomi with pip manager:
$ sudo pip install pyvmomi
To allow the use of VMware dynamic inventory plugin, enable the vmware_vm_inventory
in ansible.cfg:
[inventory] enable_plugins = vmware_vm_inventory, host_list, script, auto, yaml, ini, toml vmware_vars plugin: vmware_vm_inventory strict: False hostname: 192.168.13.70 username: [email protected] password: pas1swsew43 validate_certs: False with_tags: True
Then you can run the inventory command and get a list of virtual machines on your vCenter or ESXi server.
$ ansible-inventory --list -i /etc/ansible/vmware/vmware.yml
You can get a hierarchical list of VMs:
$ ansible-inventory --graph -i /etc/ansible/vmware/vmware.yml
Let’s add the list of your ESXi hosts to /etc/ansible/hosts
:
[all_esxi_hosts] esxi1 ansible_host=192.168.31.20 esxi2 ansible_host=192.168.31.21 esxi3 ansible_host=192.168.31.22
Set the variable values for this group of hosts:
[all_esxi_hosts:vars] ansible_user=root ansible_python_interpreter=/bin/python ansible_ssh_pass=P1ssw0rd6
You can then verify that all ESXi hosts are accessible from the Ansible host over the network:
$ ansible all_esxi_hosts -m ping
You can interactively prompt for the password if you have not specified it in the hosts file:
$ ansible all_esxi_hosts -m ping --ask-pass
With Ansible, you can run any command against all of your ESXi hosts. Ansible ad-hoc command mode is used to execute the command on the remote ESXi. For example, you might want to know the ESXi version number of all your hosts:
$ ansible all_esxi_hosts -m shell -a "vmware -vl"
In this way, you can run any shell command on any of the hosts (SSH must be enabled on ESXi).
Let’s create a simple Ansible playbook vm_start.yml to power on a specific virtual machine in VMware vCenter. This playbook uses connection credentials from vmware_vars.yml
.
- name: start vm hosts: localhost become: false gather_facts: false collections: - community.vmware pre_tasks: - include_vars: vmware_vars.yml tasks: - name: power on vmware_guest_powerstate: hostname: "{{ hostname }}" username: "{{ username }}" password: "{{ password }}" name: munfs01 validate_certs: "{{ validate_certs }}" state: powered-on
Run the playbook:
$ ansible-playbook /etc/ansible/vmware/vm_start.yml
Open the vSphere Client and verify that your VM has started successfully.
The next playbook allows you to enable the ntpd service on a specific ESXi host. Install the community.vmware module before using this Playbook:
$ ansible-galaxy collection install community.vmware
Now create a playbook (we’ve specified the connection parameters directly in the Playbook to make this easier to understand.):
--- - hosts: localhost vars: vcenter_hostname: "192.168.31.20" vcenter_user: "[email protected]" vcenter_password: "passwprd123" esxi_hostname: "192.168.31.50" esxi_username: "root" esxi_password: "es-122023" tasks: - name: Start Service on esxi host in vcenter community.vmware.vmware_host_service_manager: hostname: "{{ vcenter_hostname }}" username: "{{ vcenter_user }}" password: "{{ vcenter_password }}" esxi_hostname: "{{ esxi_hostname }}" service_name: ntpd state: present #present #absent service_policy: off validate_certs: no delegate_to: localhost
Run your playbook:
$ ansible-playbook /etc/ansible/vmware/vm_esxi_start_service.yml
In this post, we have covered the basics of using Ansible to manage VMware ESXi, vCenter, and VMs in a vSphere environment.