Hereβs an engaging Hugo Markdown guide that documents the process in a fun and structured way! π
π Automating vSphere Snapshots with Terraform #
Ever wished you could take VM snapshots in vSphere without clicking through the UI? Welcome to automation heaven! π With Terraform, weβll set up a repeatable, scalable way to snapshot multiple VMs in one command.
This guide will walk you through:
β
Setting up Terraform
β
Creating configuration files
β
Running the automation
β
Taking snapshots like a boss π
π οΈ Prerequisites #
Before we begin, make sure you have:
- β Terraform installed β Download Here
- β Access to vSphere (including your vCenter credentials)
- β A Debian 12 system (or similar Linux distro)
- β List of VMs you want to snapshot
π Step 1: Create Your Terraform Project #
First, create a directory to store your Terraform files:
mkdir -p ~/vm/terraform/snapshots && cd ~/vm/terraform/snapshots
π Step 2: Create the Configuration Files #
Terraform uses several config files. Letβs create them one by one.
1οΈβ£ main.tf - The Core Configuration #
Create a file called main.tf and add the following:
provider "vsphere" {
vsphere_server = var.vsphere_server
user = var.vsphere_username
password = var.vsphere_password
allow_unverified_ssl = var.vsphere_insecure
}
data "vsphere_datacenter" "datacenter" {
name = var.vsphere_datacenter
}
data "vsphere_virtual_machine" "vms" {
for_each = toset(var.vm_names)
name = each.key
datacenter_id = data.vsphere_datacenter.datacenter.id
}
resource "vsphere_virtual_machine_snapshot" "snapshots" {
for_each = data.vsphere_virtual_machine.vms
virtual_machine_uuid = each.value.id
snapshot_name = format("snapshot-%s-%s", each.key, timestamp())
description = "Scheduled snapshot via Terraform"
memory = true
quiesce = true
}
π₯ What this does:
- Connects to vSphere
- Finds the datacenter and VMs
- Creates a snapshot for each VM
2οΈβ£ variables.tf - Making It Configurable #
Create a file called variables.tf and add:
variable "vsphere_server" {}
variable "vsphere_username" {}
variable "vsphere_password" {}
variable "vsphere_insecure" {
type = bool
default = true
}
variable "vsphere_datacenter" {}
variable "vm_names" {
type = list(string)
default = [
"VM1",
"VM2",
"VM3
]
}
π₯ Why this rocks:
- Stores all VM names in one place for easy changes
3οΈβ£ terraform.tfvars - Securely Store Credentials #
Create a terraform.tfvars file to store your credentials (DO NOT COMMIT THIS FILE! π¨).
vsphere_server = "your-vcenter-server"
vsphere_username = "your-username"
vsphere_password = "your-password"
vsphere_insecure = true
vsphere_datacenter = "Your-Datacenter-Name"
π₯ Why this is important:
- Keeps sensitive info separate
- Terraform loads this automatically
π Step 3: Run the Automation #
Now, letβs put everything together and run Terraform!
1οΈβ£ Initialize Terraform #
terraform init
π‘ This downloads the vSphere provider and prepares Terraform.
2οΈβ£ Check the Execution Plan #
terraform plan
π‘ This shows what Terraform will do before making changes.
3οΈβ£ Apply the Configuration #
terraform apply -auto-approve
π‘ This creates snapshots for all listed VMs! π
π― Step 4: Verify the Snapshots #
To make sure everything worked, check your vSphere UI:
- Log in to vSphere Web Client
- Navigate to one of the VMs in the list
- Click Snapshots β You should see the newly created snapshots! π
π₯ Bonus: Automate with a Cron Job #
Want to run this on a schedule? Use a cron job!
- Edit your crontab:
crontab -e - Add this line to run Terraform every night at 2 AM:
0 2 * * * cd ~/vm/terraform/snapshots && terraform apply -auto-approve
π‘ Now your snapshots run automatically! π€
β Wrapping Up #
π Congratulations! Youβve successfully:
β
Set up Terraform for vSphere snapshots
β
Configured everything dynamically
β
Automated the process like a pro
π‘ Next Steps:
- Try restoring a snapshot from vSphere UI
- Explore Terraform state management
- Set up email notifications for snapshot success/failure