How do I automate VM provisioning using PowerCLI or Terraform?

Automating VM provisioning with PowerCLI or Terraform cuts manual work and reduces errors. Here are step-by-step instructions for both approaches.


Option 1: Automate VM Provisioning with PowerCLI

PowerCLI is VMware’s command-line tool for managing and automating vSphere environments.

Prerequisites

  1. Install PowerCLI on your system.
  2. Open PowerShell and run:
    powershell
    Install-Module -Name VMware.PowerCLI -Scope CurrentUser
  3. Verify you have permissions to provision VMs in your vSphere environment.
  4. Ensure VMware Tools are installed on the VMs if you need guest OS interaction.

Steps to Automate VM Provisioning

  1. Connect to vCenter:
    powershell
    Connect-VIServer -Server <vCenterServer> -User <Username> -Password <Password>

  2. Define VM specifications:
    powershell
    $vmName = "TestVM"
    $vmHost = "ESXiHostName"
    $datastore = "DatastoreName"
    $network = "VMNetwork"
    $guestOS = "windows2019Server64Guest" # Adjust for your OS version
    $cpu = 2
    $memoryMB = 4096
    $diskSizeGB = 40

    # Create VM
    New-VM -Name $vmName -VMHost $vmHost -Datastore $datastore -NetworkName $network -GuestId $guestOS -NumCpu $cpu -MemoryMB $memoryMB -DiskGB $diskSizeGB

  3. Customize the guest OS (optional):
    powershell
    Invoke-VMScript -VM $vmName -ScriptText "ipconfig /all" -GuestUser "Administrator" -GuestPassword "Password"

  4. Run the script: Save as a .ps1 file and execute in PowerShell.

  5. Disconnect from vCenter:
    powershell
    Disconnect-VIServer -Server <vCenterServer> -Confirm:$false


Option 2: Automate VM Provisioning with Terraform

Terraform is an Infrastructure as Code (IaC) tool that supports VMware vSphere through the terraform-provider-vsphere.

Prerequisites

  1. Install Terraform: download from terraform.io.
  2. Add the vSphere provider to your Terraform configuration.
  3. Configure vSphere credentials and permissions.
  4. Initialize the provider:
    bash
    terraform init

Steps to Automate VM Provisioning

  1. Create a Terraform configuration file: Define VM provisioning details in a .tf file. Example:
    provider "vsphere" {
      user           = "username"
      password       = "password"
      server         = "vcenter_server"
    
      # Disable SSL verification (lab only)
      allow_unverified_ssl = true
    }
    
    data "vsphere_datacenter" "dc" {
      name = "DatacenterName"
    }
    
    data "vsphere_compute_cluster" "cluster" {
      name          = "ClusterName"
      datacenter_id = data.vsphere_datacenter.dc.id
    }
    
    data "vsphere_network" "network" {
      name          = "VMNetwork"
      datacenter_id = data.vsphere_datacenter.dc.id
    }
    
    data "vsphere_datastore" "datastore" {
      name          = "DatastoreName"
      datacenter_id = data.vsphere_datacenter.dc.id
    }
    
    resource "vsphere_virtual_machine" "vm" {
      name             = "TerraformVM"
      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 = "windows2019Server64Guest"
    
      network_interface {
        network_id   = data.vsphere_network.network.id
        adapter_type = "vmxnet3"
      }
    
      disk {
        label            = "disk0"
        size             = 40
        eagerly_scrub    = false
        thin_provisioned = true
      }
    
      clone {
        template_uuid = "template-uuid"  # Use a pre-existing VM template
      }
    }
  2. Initialize Terraform:
    bash
    terraform init
  3. Validate configuration:
    bash
    terraform validate
  4. Provision the VM:
    bash
    terraform apply

    Review the execution plan and type yes to confirm.
  5. Monitor progress: Terraform communicates with vSphere and provisions the VM. On completion, it outputs the new VM details.

Comparison: PowerCLI vs Terraform

Feature PowerCLI Terraform
Ease of use Simple for scripting VMware tasks Requires IaC expertise
Scalability Best for single VM provisioning Best for managing large-scale environments
Reusability Reusable scripts, less structured Highly reusable, modular configurations
Version control Limited Full support via Git for IaC files
Integration Limited beyond VMware Integrates with multi-cloud environments

Choose PowerCLI for quick scripting tasks. Choose Terraform for scalable, reusable infrastructure automation.

Liked this briefing? Get it in your inbox

Every morning: the stories that make tech leaders act, with official clips and primary sources. Free, 5-minute read.

Free. One click to unsubscribe. We never share your email. Privacy

How do I automate VM provisioning using PowerCLI or Terraform?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to top