Create a virtual machine in ESXi 8 using Ansible

Meher Askri
5 min readAug 2, 2024

--

Hello and welcome .In this article, we will discuss automating the creation of virtual machines (VMs) on an ESXi server. But before we dive into that, let’s take a quick look at the VMware vSphere environment and its basic components. To keep things simple, we’ll only cover the essential components and avoid discussing optional vSphere components.

What is VMware Vsphere ?

Vmware vsphere is a suite of products designed to virtualize enterprise data centers and build private clouds ( As the leader in the virtualization market, about 80% of organizations that have their own on-premise data center use VMware solutions ).

The core components of vSphere are : vCenter server and an ESXI server

vCenter is a centralized management platform that provides control and management of multiple ESXi hosts, (typically a cluster) . On the other hand , ESXI server is the hypervisor known as A bare-metal hypervisor on which virtual machines run .

Now that you understand the global picture , let’s move on to the practical part .

Prepare ESXI Host

I’m going to demonstrate those steps on my ESXI host which is a standalone host ( not managed by vCenter ) , However, the steps are the same for a vSphere cluster, where multiple ESXi hosts are managed by vCenter.

Okay ,the first step is to enable the ssh connection , Because by default ESXI does not accept any SSH connection for security reasons , so we need to enable the service and open the port .

From the DCUI console , press F2 , navigate to the troubleshooting option and select enable SSH , (the second option) and hit enter .

Now, open the ESXI host client web interface ,login and navigate to the manage tab, from there go to service and search for TSM-SSH . You should see that it is up and running ( If you don’t have access to the DCU , you can enable it from here ) .

Setup Ansible Configuration and inventory

Now let’s move to the second part :

To work with vmware we need to install the python3-pyVmomi “ , which is the vmware vsphere API (python library). You can install it via the python package manager pip or via the linux package manager, open the terminal and type : dnf install python3-pyvmomi -y ”

Next let’s create an “ ansible.cfg “ file and an “ inventory “ file , again I used the minimal and the very basic way to connect to the ESXI host .

I wanted to keep things as simple as possible, which is why I’m logging in using the root user. However, in most cases, it’s recommended to create a separate ESXi user, assign the appropriate permissions, and copy the SSH public key for secure password-less access. Alternatively, we can use Ansible Vault to encrypt the password file ( I’ll show how to do this another day 😊😊 ) .

Now let’s try to ping the ESXI from the ansible control node using ad hoc commands (For now, we can ignore the warning message about the missing python interpreter. We will fix it later ).

And as you can see, the ping was successful .

Ansible Playbook to create a VM

Now that everything is ready, we can start creating the playbook to create a simple VM.

As you can see, I have already set the “ ansible_python “ interpreter to version 3.6 because I am using Ansible 2.13, which by default uses Python 3.9. However, the “ python-pyvmomi “ library I installed is not compatible with Python 3.9, so I had to change the interpreter version to ensure compatibility.

Okay, let me quickly explain this simple playbook. This playbook will create an empty virtual machine on the specific datastore ,which in my case is datastore1 (a VMFS datastore) , The guest OS will be CentOS with 2GB of RAM and 2 vCPUs.

Additionally, It will ensure that the virtual machine is connected to the correct port group, which is equivalent to a VLAN in the Vmware world (I’ll explain the networking part in more detail another day).

Okay, let’s run the playbook and see what happens and let’s check the tasks tab in the ESXi host client at the same time.

As you can see, the VM has been successfully created with CentOS as the guest OS and named “test” .

This is the power of automation. What I showed you is just the basics, but we can even automate the installation of the operating system, the migration of VMs and a lot more.

Just, imagine how much faster the process could be when dealing with hundreds of virtual machines.”

Thank you for reading my article. If you found it useful, please like and share. If you have any questions, don’t hesitate to ask me , See you in another one .

--

--

Meher Askri
Meher Askri

Written by Meher Askri

Linux System Engineer || RHCA

No responses yet