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: