Skip to content
Luca Dell'Oca Principal Cloud Architect @Veeam
Virtual To The Core Virtual To The Core

Virtualization blog, the italian way.

  • Media
  • About me
Virtual To The Core
Virtual To The Core

Virtualization blog, the italian way.

My Automated Lab project: #4 Deploy the VMs from templates using Terraform

Luca Dell'Oca, October 10, 2023October 6, 2023
After I have created my templates with Packer in the previous posts, I can now use them every time as a quick source for creating my own virtual machines. Thanks to Terraform, I’ll be able to deploy each new vm in a matter of minutes, and also to customize each of them as I need.

Install Terraform

First, let’s install Terraform if we don’t have it:
sudo apt-get install jq unzip -y

# pull latest using github api
export TERRA_VERSION=$(curl -sL https://api.github.com/repos/hashicorp/terraform/releases/latest | jq -r ".tag_name" | cut -c2-)

# download
wget https://releases.hashicorp.com/terraform/${TERRA_VERSION}/terraform_${TERRA_VERSION}_linux_amd64.zip

# unzip
unzip terraform_${TERRA_VERSION}_linux_amd64.zip

# set permissions and move into path
chmod +x terraform
sudo mv terraform /usr/local/bin

# validate
terraform version

Configure the Terraform plan for my first Windows VM

Technically, Terraform only needs one file in its own .tf format to work. But for better manageability, people tend to split information into multiple files. So did I.
base.tf has all the definitions of the variables to connect to the VMware vSphere environment.
variables.tf has all the definitions of the variables:
variable "vsphere_user" {}
variable "vsphere_password" {}
variable "vsphere_server" {}
variable "vsphere_dc_name" {}
variable "vsphere_datastore" {}
variable "vsphere_compute_cluster" {}
variable "vsphere_portgroup_name" {}
variable "vsphere_folder" {}
variable "vsphere_template_name" {}
variable "winadmin_password" {}
variable "server_name" {}
variable "server_IP" {}
variable "netmask" {}
variable "def_gw" {}
variable "dns_server" {}
variable "server_cpu_num" {}
variable "server_mem" {} 
variable "firmware" {}
variables.auto.tfvars has all the parameters for the variables we defined in the other files. This is where I configure the VM as I want it to be:
# Which Windows administrator password to set during vm customization
winadmin_password = "Veeam123!"
# Firmware of template
firmware = "bios"
# VBR Server settings
server_name = "vbrserver" 
server_IP = "172.27.217.62" 
server_cpu_num = 4 
server_mem = 6144
# Common network params
netmask = "24"
def_gw = "172.27.217.1"
dns_server = "172.27.217.21"
Last but not least, I have vbr.tf, that is the “plan” file. Here, there are multiple sections that are better explained in a Terraform course, but in short it creates a resource of type “vsphere_virtual_machine”, defines its name, location in vSphere, network, disk, the template to use as a source. Have a look at the complete file in my Github IAC repository.

Execute the Terraform plan

When the files are ready, I initialize Terraform:
terraform init
then I let it check the plan to validate it:
terraform validate
and if all is ok, I execute the plan:
terraform apply

Terraform will show us what it is about to modify:

In this case, it will create a list of new resources, as you can see from the green + symbol. I write yes in the answer and I wait for the new VM to be created and configured. The duration of the operation will be mainly dependant on the speed of the underlying storage where the VM is cloned, but in any case it will not take a lot of time:
3m30s were used just for the clone operation, plus 4 minutes for the configuration of the VM, which is now up and running and ready to be used:

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to email a link to a friend (Opens in new window) Email
  • Click to share on Tumblr (Opens in new window) Tumblr
  • Click to share on Pinterest (Opens in new window) Pinterest
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to share on Pocket (Opens in new window) Pocket
Tech automationcodeiacinfrastructurelabservertemplateterraformvspherewindows

Post navigation

Previous post
Next post

Search

Sponsors

Latest Posts

  • Migrate WSL (Windows Subsystem for Linux) to a new computer
  • Pass keystrokes to a pfSense virtual machine to install it automatically
  • Automatically deploy pfSense with Terraform and Ansible
  • My Automated Lab project: #6 Create a S3 Bucket with Terraform
  • My Automated Lab project: #5 Deploy a Linux vSphere VM with Terraform and custom disks
©2025 Virtual To The Core | WordPress Theme by SuperbThemes
We use cookies to ensure that we give you the best experience on our website, and to collect anonymous data regarding navigations stats using 3rd party plugins; they all adhere to the EU Privacy Laws. If you continue to use this site we will assume that you are ok with it.OkNoPrivacy Policy