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 18.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 18.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 18.04
1- Select the language of the Ubuntu installer.
2- Select the keyboard layout for the Ubuntu installation.
3- Start the Ubuntu installation.
4- Configure the network card of the virtual machine with a temporary configuration.
5- Configure a proxy if you use one.
6- Choose the partition layout for the disk.
7- Choose the installation disk.
8- Configure a user for the virtual machine.
9- Reboot the virtual machine.
10- SSH to the new Ubuntu virtual machine.
$ ssh sguyennet@10.10.40.253
11- Upgrade the Ubuntu packages.
$ sudo apt-get update
$ sudo apt-get upgrade
Install the VMware tools
1- Download the VMware tools on your laptop (you have to create an account if you don't already have one).
2- Uncompress the archive.
$ tar xvzf VMware-Tools-core-10.3.5-10430147.tar.gz
3- Copy the VMware tools ISO to the template virtual machine.
$ scp vmtools/linux.iso sguyennet@10.10.40.253:/tmp
4- SSH to the template virtual machine.
$ ssh sguyennet@10.10.40.253
5- Mount the VMware tools ISO.
$ sudo mount -o loop /tmp/linux.iso /mnt
6- Extract the VMware tools installer.
$ tar xvzf /mnt/VMwareTools-10.3.5-10430147.tar.gz -C /tmp
7- Unmount the VMware tools ISO.
$ sudo umount /mnt
8- Remove the open-vm-tools package.
$ sudo apt-get remove --purge open-vm-tools
9- Install the VMware tools (leave all the options by default).
$ cd /tmp/vmware-tools-distrib
$ sudo ./vmware-install.pl
10- Reboot the machine.
$ sudo reboot
Clean the virtual machine configuration
1- Remove the temporary network configuration.
$ sudo rm /etc/netplan/50-cloud-init.yaml
2- Prevent cloud config from preserving the hostname.
$ sudo vim /etc/cloud/cloud.cfg
...
preserve_hostname: true
...
3- Power off the Ubuntu virtual machine.
$ sudo shutdown now
4- Edit the settings of the virtual machine.
5- Set the network card back to "VM Network".
6- Set the CD/DVD drive back to "Client Device".
7- Validate the modification.
Create the VMware vSphere template
1- Convert the virtual machine to a vSphere template.
https://blog.inkubate.io/ghosthttps:/start.fedoraproject.org/
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-18.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 18.04 template to deploy vSphere virtual machines with Terraform.