Skip to content

Terraform with VMware

Introduction to VMware Provider

The VMware provider is used to interact with the resources supported by VMware vSphere. It allows you to manage virtual machines, datastores, networks, and other VMware resources.

Provider Configuration

provider "vsphere" {
  user           = var.vsphere_user
  password       = var.vsphere_password
  vsphere_server = var.vsphere_server

  # If you have a self-signed cert
  allow_unverified_ssl = true
}

Authentication

Typically, you'll use username and password authentication with VMware vSphere. It's recommended to use variables for sensitive information.

Common VMware Resources

  1. Virtual Machine
resource "vsphere_virtual_machine" "vm" {
  name             = "terraform-test"
  resource_pool_id = data.vsphere_resource_pool.pool.id
  datastore_id     = data.vsphere_datastore.datastore.id

  num_cpus = 2
  memory   = 1024
  guest_id = "other3xLinux64Guest"

  network_interface {
    network_id = data.vsphere_network.network.id
  }

  disk {
    label = "disk0"
    size  = 20
  }
}
  1. Folder
resource "vsphere_folder" "folder" {
  path          = "terraform-test-folder"
  type          = "vm"
  datacenter_id = data.vsphere_datacenter.dc.id
}
  1. vSphere Distributed Virtual Switch
resource "vsphere_distributed_virtual_switch" "dvs" {
  name          = "terraform-test-dvs"
  datacenter_id = data.vsphere_datacenter.dc.id

  uplinks = ["uplink1", "uplink2", "uplink3", "uplink4"]

  active_uplinks  = ["uplink1", "uplink2"]
  standby_uplinks = ["uplink3", "uplink4"]
}

Best Practices

  1. Use data sources to reference existing vSphere resources
  2. Implement proper error handling and retries for vSphere operations
  3. Use Terraform workspaces for managing multiple vSphere environments
  4. Utilize VMware-specific modules from the Terraform Registry

Advanced Topics

  1. Working with vSphere clusters and resource pools
  2. Managing vSphere networking and distributed switches
  3. Implementing VM templates and cloning
  4. Integrating with VMware NSX-T for advanced networking

Always refer to the official Terraform VMware provider documentation for the most up-to-date information on available resources and their arguments.