The VMware vSphere Provider enables Terraform to manage infrastructure within a VMware vSphere environment, including virtual machines, networks, datastores, and resource pools. It is widely used for automating on-premises infrastructure in enterprise environments.
Key Features:
Example Use Case: Creating a Virtual Machine
Here’s how to create a virtual machine in a VMware vSphere environment using Terraform:
provider "vsphere" {
user = "user"
password = "password"
vsphere_server = "vsphere.example.com"
allow_unverified_ssl = true
}
data "vsphere_datacenter" "dc" {
name = "Datacenter"
}
data "vsphere_compute_cluster" "cluster" {
name = "Cluster"
datacenter_id = data.vsphere_datacenter.dc.id
}
data "vsphere_network" "network" {
name = "VM Network"
datacenter_id = data.vsphere_datacenter.dc.id
}
data "vsphere_datastore" "datastore" {
name = "Datastore"
datacenter_id = data.vsphere_datacenter.dc.id
}
resource "vsphere_virtual_machine" "vm" {
name = "example-vm"
resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
datastore_id = data.vsphere_datastore.datastore.id
num_cpus = 2
memory = 4096
guest_id = "otherGuest64"
network_interface {
network_id = data.vsphere_network.network.id
adapter_type = "vmxnet3"
}
disk {
label = "disk0"
size = 20
thin_provisioned = true
}
}
What’s Happening Here?
provider
block connects Terraform to the vSphere environment.data
resources retrieve information about the vSphere datacenter, cluster, network, and datastore.vsphere_virtual_machine
resource creates a virtual machine with 2 CPUs, 4 GB of memory, and a 20 GB thin-provisioned disk.vmxnet3
adapter type.Advanced Tip:
Clone a VM from an existing template for faster provisioning:
resource "vsphere_virtual_machine" "cloned_vm" {
name = "cloned-vm"
resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
datastore_id = data.vsphere_datastore.datastore.id
clone {
template_uuid = "template-uuid"
customize {
linux_options {
host_name = "cloned-vm"
domain = "example.com"
}
network_interface {
ipv4_address = "192.168.1.100"
ipv4_netmask = 24
}
ipv4_gateway = "192.168.1.1"
}
}
}
This configuration clones a VM from a template and customizes its hostname, domain, and network settings.