Deploy a VMware vSphere Virtual Machine with Terraform
words - read.

Deploy a VMware vSphere Virtual Machine with Terraform

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.


HashiCorp Terraform Logo

Preriquisites

For this lab we will see how to deploy a VMware vSphere virtual machine with Terraform, therefore we obviously need a working VMware vSphere environment (vCenter and ESXi). We will use a Linux client machine (Ubuntu 16.04), but the installation of Terraform on another platform should be straightforward.

Installation of Terraform

1- Download the latest Terraform archive for Linux 64-bit on the Linux client machine.

$ wget https://releases.hashicorp.com/terraform/0.11.7/terraform_0.11.7_linux_amd64.zip

2- Install Unzip.

$ sudo apt-get install unzip

3- Unzip the Terraform archive.

$ unzip terraform_0.11.7_linux_amd64.zip

4- Copy the Terraform binary to /usr/local/bin.

$ sudo cp terraform /usr/local/bin

5- Modify the ownership of the Terraform binary.

$ sudo chown root:root /usr/local/bin/terraform

Create the vSphere Ubuntu 16.04 template

1- Download the ISO of Ubuntu 16.04 LTS.

2- Upload the Ubuntu 16.04 image to one of your vSphere datastores.

Create vShpere Template Ubuntu

Create vShpere Template Ubuntu

3- Wait for the Ubuntu 16.04 ISO to upload.

Create vShpere Template Ubuntu

4- Create a new VMware vSphere virtual machine.

Create vShpere Template Ubuntu

Create vShpere Template Ubuntu

5- Enter "ubuntu-16.04-template" as the name of the virtual machine.

Create vShpere Template Ubuntu

6- Select one of your clusters or ESXi hosts to deploy the virtual machine.

Create vShpere Template Ubuntu

7- Select one of your datastores to deploy the virtual machine.

Create vShpere Template Ubuntu

8- Select the vSphere compatibility for the virtual machine.

Create vShpere Template Ubuntu

9- Select "Linux" as guest OS family and "Ubuntu Linux (64-bit) as guest OS version.

Create vShpere Template Ubuntu

10- Change the virtual machine disk provisioning to "Thin provision".

Create vShpere Template Ubuntu

11- Change the SCSI controller type to "VMware Paravirtual". Ubuntu supports PV SCSI, therefore you will gain in performance with this driver.

Create vShpere Template Ubuntu

12- Disconnect the network card.

Create vShpere Template Ubuntu

13- Change the CD drive to "Datastore ISO File".

Create vShpere Template Ubuntu

14- Select the Ubuntu 16.04 image that we uploaded earlier.

Create vShpere Template Ubuntu

15- Connect the CD drive.

Create vShpere Template Ubuntu

16- Validate the customization of the virtual machine.

Create vShpere Template Ubuntu

17- Complete the creation of the virtual machine.

Create vShpere Template Ubuntu

18- Power on the new virtual machine.

Create vShpere Template Ubuntu

19- Launch the virtual machine console.

Create vShpere Template Ubuntu

20- Select the language you would like to use for the Ubuntu 16.04 installer.

Create vShpere Template Ubuntu

21- Start the installation of Ubuntu 16.04.

Create vShpere Template Ubuntu

22- Choose your language.

Create vShpere Template Ubuntu

23- Select the location of your server.

Create vShpere Template Ubuntu

24- Skip the automatic detection of the keyboard.

Create vShpere Template Ubuntu

25- Select the language of your keyboard.

Create vShpere Template Ubuntu

26- Select the layout of your keyboard.

Create vShpere Template Ubuntu

27- The DHCP configuration of the network will fail. This is expected as we disconnected the network card of the virtual machine earlier.

Create vShpere Template Ubuntu

28- Don't configure the network.

Create vShpere Template Ubuntu

29- Leave the default hostname.

Create vShpere Template Ubuntu

30- Enter your name.

Create vShpere Template Ubuntu

31- Enter your username.

Create vShpere Template Ubuntu

32- Enter a password for your user.

Create vShpere Template Ubuntu

33- Re-enter the same password.

Create vShpere Template Ubuntu

34- Choose if you would like to encrypt your disk or not.

Create vShpere Template Ubuntu

35- Choose to use the entire disk and to set up LVM.

Create vShpere Template Ubuntu

36- Select the disk to partition.

Create vShpere Template Ubuntu

37- Write the change to the disk.

Create vShpere Template Ubuntu

38- Choose the size of the volume group.

Create vShpere Template Ubuntu

39- Write the change to the disk.

Create vShpere Template Ubuntu

40- Install the security updates automatically.

Create vShpere Template Ubuntu

41- Choose to install the standard system utilities and OpenSSH server.

Create vShpere Template Ubuntu

42- Install GRUB on the master boot record.

Create vShpere Template Ubuntu

43- Reboot the server.

Create vShpere Template Ubuntu

44- Power off the virtual machine.

Create vShpere Template Ubuntu

45- Edit the settings of the virtual machine.

Create vShpere Template Ubuntu

46- Connect the network card.

Create vShpere Template Ubuntu

47- Disconnect the CD drive.

Create vShpere Template Ubuntu

48- Configure the CD drive to "Client Device".

Create vShpere Template Ubuntu

49- Validate the virtual machine configuration.

Create vShpere Template Ubuntu

50- Convert the virtual machine to a template.

Create vShpere Template Ubuntu

51- Create a new virtual machine and template folder.

Create vShpere Template Ubuntu

52- Enter "template" as folder name.

Create vShpere Template Ubuntu

53- Move the "ubuntu-16.04-template" template to the "template" folder.

Create vShpere Template Ubuntu

Create and configure the Terraform script

1- Clone the terraform-vSphere-ubuntu project on the Linux client machine on which we installed Terraform earlier.

$ git clone https://github.com/sguyennet/terraform-vsphere-ubuntu.git

2- Enter the terraform-vsphere-ubuntu directory.

$ cd terraform-vsphere-ubuntu

3- Edit the terraform.tfvars file and modify it with your VMware vSphere infrastructure information.

$ vim terraform.tfvars

Here is an example:

# vCenter connection
vsphere_vcenter = "vcsa.inkubate.io"
vsphere_user = "administrator@vsphere.local"
vsphere_password = "**"
vsphere_unverified_ssl = "true"
# VM specifications
vsphere_datacenter = "inkubate-lab"
vsphere_vm_folder = "terraform-ubuntu-01"
vsphere_vm_name = "ubuntu-01"
vsphere_vm_template = "template/ubuntu-16.04-template"
vsphere_cluster = "Compute-01"
vsphere_vcpu_number = "1"
vsphere_memory_size = "1024"
vsphere_datastore = "Datastore-01"
vsphere_port_group = "VM Network"
vsphere_ipv4_address = "10.10.20.250"
vsphere_ipv4_netmask = "24"
vsphere_ipv4_gateway = "10.10.20.1"
vsphere_dns_servers = "10.10.20.1"
vsphere_domain = "inkubate.io"
vsphere_time_zone = "UTC"

Execute the deployment

1- Download the Terraform vSphere provider.

$ terraform init

2- Verify which modifications to the VMware vSphere infrastructure are going to be done.

$ terraform plan

3- Push the modifications to the VMware vSphere infrastructure.

$ terraform apply

You should now be able to SSH to your new Ubuntu 16.04 virtual machine.

Update the deployment

1- Edit the terraform.tfvars configuration file.

$ vim terraform.tfvars

Example: To add a vCPU to the virtual machine, just bump the value of the vsphere_vcpu_number parameter.

2- Verify which modifications to the VMware vSphere infrastructure are going to be done.

$ terraform plan

3- Push the modifications to the VMware vSphere infrastructure.

$ terraform apply

Delete the deployment

$ terraform destroy

Comments

comments powered by Disqus