Create multiple VMs on VMware vSphere using Terraform

Terraform, developed by HashiCorp, is a versatile Infrastructure as Code (IaC) solution that empowers automation of diverse infrastructure operations. Written in Go and open-source, Terraform offers cloud-agnostic provisioning capabilities. It excels in constructing, modifying, and maintaining infrastructure with safety and efficiency in mind. Terraform streamlines multi-cloud management by enabling a unified workflow across various cloud platforms. This means that you can employ Terraform to manage infrastructure hosted on public cloud providers like Amazon Web Services, Microsoft Azure, and Google Cloud Platform, as well as on private cloud environments like VMware vSphere, OpenStack, or CloudStack.

Prerequisites:

  • Installed Terraform tool. The installation instructions for Terraform are available on the following link.
  • vSphere provider for Terraform.
  • VMware vSphere infrastructure.
  • A VMware vCenter user with enough privileges for creating and administering VMs.

Step 1: Set up your Terraform environment

  • If you haven’t already installed Terraform, download and install it from the official website (https://www.terraform.io/).
  • Make sure you have SSH access to your ESXi host.

Step 2: Create a working directory

Create a directory on your local machine where you’ll store your Terraform configuration files.

mkdir terraform-esxi-vms
cd terraform-esxi-vms

Step 3: Define your Terraform configuration

In this step, you’ll create a Terraform configuration file (e.g., main.tf) to define your VMs. Here’s a basic example:

provider “vsphere” {
user = “your-username”
password = “your-password”
vsphere_server = “your-esxi-server”
}
resource “vsphere_virtual_machine” “example_vm” {
count = 2
name = “vm-${count.index}”
resource_pool_id = data.vsphere_resource_pool.example.id
datastore_id = data.vsphere_datastore.example.id
host_system_id = data.vsphere_host.example.id
num_cpus = 2
memory = 2048
guest_id = “ubuntu64Guest”
network_interface {
network_id = data.vsphere_network.example.id
}
}
data “vsphere_datastore” “example” {
name = “your-datastore”
}
data “vsphere_resource_pool” “example” {
name = “your-resource-pool”
}
data “vsphere_host” “example” {
name = “your-esxi-host”
}
data “vsphere_network” “example” {
name = “your-network”
}

In the initial section of the configuration, we establish the Terraform provider for vSphere. This provider is responsible for configuring our interaction with the vSphere infrastructure, such as the vSphere vCenter. Each resource type in Terraform is made available through a specific provider, which serves as a plugin within Terraform, delivering a set of resource types related to a particular cloud or on-premises infrastructure platform. Typically, each provider caters to the management of resources on a specific infrastructure platform. While Terraform and its providers are distinct entities, when you initialize a Terraform project, Terraform can automatically handle the installation of most providers, ensuring that they are readily available for use within your working directory.

In the second section of the configuration, data sources are declared to define VMware vSphere resources, including datacenter, cluster, host, datastore, network portgroup, and VM template. These data sources serve the purpose of retrieving information about external resources in the vSphere environment and employing this data to configure your Terraform resources.

The following section elaborates on the “vsphere_virtual_machine” resource, where we specify the hardware, network, and disk configurations for our virtual machines (VMs). Resources play a crucial role in Terraform as they enable the definition of one or more infrastructure resource entities, such as Virtual Private Clouds (VPCs), virtual machines, or DNS records. A resource block serves to explicitly declare a specific infrastructure entity that the author intends to create, along with the assignment of various property values.

In this particular example, as we are utilizing a VM template, this template will be duplicated and customized using a Terraform “clone” block. Within the “clone” block, we can tailor aspects such as the VM’s hostname, domain name, and network interfaces to suit our requirements.

Step 4: Initialize and apply Terraform

In your working directory, run the following Terraform commands to initialize the project and apply the configuration:

terraform init
terraform apply

Terraform will prompt you to confirm the planned changes. Type “yes” to proceed.

Step 5: Verify VM creation

Once the Terraform apply process is complete, you should have multiple VMs created on your ESXi host. You can check this by logging in to your ESXi host or using the vSphere client.

Step 6: Destroy VMs (optional)

If you need to destroy the VMs created by Terraform, you can use the following command:

terraform destroy

Again, Terraform will prompt you to confirm the destruction of the resources. Type “yes” to proceed.

That’s it! You’ve successfully created multiple VMs on your ESXi host using Terraform. This is a basic example, and you can customize it further to meet your specific requirements, such as specifying the VM’s operating system, IP addresses, and more.

Conclusion

Terraform proves to be an excellent solution for streamlining vSphere management automation. There are numerous benefits associated with leveraging Terraform, including its user-friendly nature, remarkable flexibility, reusability, and the seamless ability to perform full-stack deployments across various cloud platforms and on-premises environments. Setting up Terraform is straightforward, and it ensures rapid and consistent infrastructure provisioning. Additionally, Terraform employs a ‘state file’ to preserve the current state of the infrastructure, which proves invaluable for tracking and reviewing any subsequent modifications made to the system’s infrastructure.

Recent Articles

spot_img

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here

Stay on op - Ge the daily news in your inbox