Terraform is an automation tool created by HashiCorp. It focuses on deploying cloud infrastructure in an automatic fashion. It supports many 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 Red Hat RHEL 7 Terraform template for VMware vSphere.
Creating 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 "Red Hat Enterprise Linux 7 (64-bit)" as guest OS for the virtual machine.
7- Choose a temporary network for the virtual machine.
8- Add the RHEL 7 ISO to the CD/DVD drive of the virtual machine.
9- Connect the CD/DVD drive at boot.
10- Complete the hardware customization of the virtual machine.
11- Validate the creation of the virtual machine.
12- Power on the virtual machine.
13- Launch the vSphere web console.
Installing RHEL 7
1- Launch the RHEL installer.
2- Select the language of the installer.
3- Select the installation disk.
4- Configure the network card of the virtual machine with a temporary configuration.
5- Start the installation.
6- Configure the root password.
7- Configure an administrator user.
8- Reboot the virtual machine.
9- SSH to the new RHEL virtual machine.
$ ssh sguyennet@10.10.40.253
10- Register your new RHEL machine to access the Red Hat repositories.
$ sudo subscription-manager register --username [username] --password [password] --auto-attach
note: If you don't already have a Red Hat account, you can create a Red Hat developer account for free and register up to 16 RHEL servers.
11- Upgrade the RHEL packages.
$ sudo yum upgrade
12- Install Perl.
$ sudo yum install perl
13- Install open-vm-tools
$ sudo yum install open-vm-tools
14- Reboot the virtual machine.
$ sudo reboot
Cleaning the virtual machine configuration
1- SSH to the new RHEL virtual machine.
$ ssh sguyennet@10.10.40.253
2- Unregister your RHEL machine.
$ sudo subscription-manager unregister
3- Remove the temporary network configuration.
$ sudo rm /etc/sysconfig/network-scripts/ifcfg-ens192
3- Power off the RHEL virtual machine.
$ sudo shutdown now
4- Edit the settings of the virtual machine.
5- Set back the network card to "VM Network".
6- Set back the CD/DVD drive to "Client Device".
7- Validate the modification.
Creating the VMware vSphere template
1- Convert the virtual machine to a vSphere template.
Testing the VMware vSphere template
Installing 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 to your path.
$ sudo cp terraform /usr/bin
Launching 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 = "rhel-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 = "rhel-7-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 RHEL 7 template to deploy vSphere virtual machines with Terraform.