Terraform is an automation tool created by HashiCorp. It focuses on deploying cloud infrastructure in an automatic fashion. It supports a lot of cloud providers like AWS, DigitalOcean, Google Cloud Platform, OpenStack, VMware vSphere, and more. Terraform is developed in GoLang, which makes the installation really easy to perform, and allows for a lot of operating systems to be supported.

In this lab, we will see how to create a Ubuntu 16.04 Terraform template for VMware vSphere.
Create a VMware vSphere virtual machine
1- Create a new virtual machine.
2- Choose a name for your virtual machine template.
3- Select a temporary compute resource for the virtual machine.
4- Select a datastore for the virtual machine.
5- Select the vSphere compatibility for the virtual machine.
6- Select "Ubuntu Linux (64-bit)" as guest OS for the virtual machine.
7- Change the type of the SCSI controller to "VMware Paravirtual".
8- Choose a temporary network for the virtual machine.
9- Add the Ubuntu 16.04 ISO to the CD/DVD drive of the virtual machine.
10- Connect the CD/DVD drive at boot.
11- Finish the hardware customization of the virtual machine.
12- Validate the creation of the virtual machine.
13- Power on the virtual machine.
14- Launch the vSphere web console.
Install Ubuntu 16.04
1- Select the language of the Ubuntu installer.
2- Start the Ubuntu installation.
3- Choose the language of your Ubuntu installation.
4- Select the country were your Ubuntu virtual machine is located.
5- Select the keyboard layout for the Ubuntu installation.
6- Configure the network card of the virtual machine with a temporary configuration.
7- Configure a user for the virtual machine.
8- Don't encrypt the home directory of the virtual machine.
9- Validate the timezone of the virtual machine.
10- Choose a partitioning method for the installation disk.
11- Choose the installation disk.
12- Configure a proxy if you use one.
13- Choose to install the security updates automatically.
14- Choose to install the "OpenSSH server".
15- Install GRUB.
16- Reboot the virtual machine.
17- SSH to the new Ubuntu virtual machine.
$ ssh sguyennet@10.10.40.253
18- Upgrade the Ubuntu packages.
$ sudo apt-get update
$ sudo apt-get upgrade
Clean the virtual machine configuration
1- Remove the following lines from the /etc/network/interfaces network configuration.
# The primary network interface
auto ens192
iface ens192 inet static
address 10.10.40.253
netmask 255.255.255.0
network 10.10.40.0
broadcast 10.10.40.255
gateway 10.10.40.1
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 10.10.40.1
dns-search inkubate.io
2- Power off the Ubuntu virtual machine.
$ sudo shutdown now
3- Edit the settings of the virtual machine.
4- Set the network card back to "VM Network".
5- Set the CD/DVD drive back to "Client Device".
6- Validate the modification.
Create the VMware vSphere template
1- Convert the virtual machine to a vSphere template.
Test the VMware vSphere template
Install Terraform
This is an example on how to install Terraform on your Linux desktop. Terraform is also available for Mac OSX and Windows.
1- Download Terraform.
$ wget https://releases.hashicorp.com/terraform/0.11.14/terraform_0.11.14_linux_amd64.zip
2- Unzip the archive.
$ unzip -e terraform_0.11.14_linux_amd64.zip
3- Copy the binary into your path.
$ sudo cp terraform /usr/bin
Launch a new virtual machine based on the template
1- Clone the Terraform script repository.
$ git clone https://github.com/sguyennet/terraform-vsphere-standalone.git
2- Initialise Terraform.
$ cd terraform-vsphere-standalone
$ terraform init
3- Configure the deployment (modify accordingly).
$ vim terraform.tfvars
#===============================================================================
# VMware vSphere configuration
#===============================================================================
# vCenter IP or FQDN #
vsphere_vcenter = "vcsa.inkubate.io"
# vSphere username used to deploy the infrastructure #
vsphere_user = "administrator@vsphere.local"
# Skip the verification of the vCenter SSL certificate (true/false) #
vsphere_unverified_ssl = "true"
# vSphere datacenter name where the infrastructure will be deployed #
vsphere_datacenter = "inkubate-lab"
# vSphere cluster name where the infrastructure will be deployed #
vsphere_cluster = "Compute-01"
#===============================================================================
# Virtual machine parameters
#===============================================================================
# The name of the virtual machine #
vm_name = "ubuntu-standalone"
# The datastore name used to store the files of the virtual machine #
vm_datastore = "Datastore-02"
# The vSphere network name used by the virtual machine #
vm_network = "pg-inkubate-production-static"
# The netmask used to configure the network card of the virtual machine (example: 24) #
vm_netmask = "24"
# The network gateway used by the virtual machine #
vm_gateway = "10.10.40.1"
# The DNS server used by the virtual machine #
vm_dns = "10.10.40.1"
# The domain name used by the virtual machine #
vm_domain = "inkubate.io"
# The vSphere template the virtual machine is based on #
vm_template = "ubuntu-16.04-terraform-template"
# Use linked clone (true/false)
vm_linked_clone = "false"
# The number of vCPU allocated to the virtual machine #
vm_cpu = "1"
# The amount of RAM allocated to the virtual machine #
vm_ram = "1024"
# The IP address of the virtual machine #
vm_ip = "10.10.40.254"
4- Deploy the virtual machine.
$ terraform apply
5- Test to access the virtual machine.
$ ssh sguyennet@10.10.40.254
6- Destroy the virtual machine.
$ terraform destroy
Congratulations! You can now use this Ubuntu 16.04 template to deploy vSphere virtual machines with Terraform.