Basic Terraform Installation and VMware vSphere Automation

0

One of the great tools in automation and DevOps today is Hashicorp’s Terraform.  Terraform is a great way to provide powerful automation in most any environment spanning on-premise infrastructure to public cloud environments.  One of the many advantages of utilizing Terraform for automation is the simplicity in which you can get up and running with Terraform automating your environments.  I had made it my goal this year to get my hands dirty with various automation tools and Hashicorp’s Terraform is next on my list of tools to get familiar with.  I can tell you, coming from an infrastructure background that viewing infrastructure as code is a mind shift that allows great possibilities in how infrastructure is created and maintained.  Terraform provides a great way to easily get your feet wet with infrastructure automation.  Getting a start with automation using Terraform is extremely easy and simple to setup and we can also begin automating providers such as VMware vSphere in no time.  Let’s take a look at basic Terraform installation and VMware vSphere Automation.

Hashicorp Terraform Overview and Terminology

Let’s take a step back for a moment and discuss what Hashicorp Terraform really is.  Hashicorp Terraform allows creating, changing, and improving upon infrastructure that allows teams to treat infrastructure creation and changes as code that can be reviewed and versioned as such.

Terraform is a declarative language that allows you to tell the system what you want the end infrastructure to look like and Terraform will create or change it to fit that declared state. In Terraform terminology, it describes different types of infrastructure platforms as providers. It can interact with platforms housed in AWS, Azure, Google Cloud, VMware, and many others. The language that Terraform is written in is called Hashicorp Configuration Language or HCL. The files that it understands are created with the extension .tf on the end.

Terraform has the concept of the following:

  • Provider – The platform you are going to manage
  • Resource – The type of object you want to create, manage, etc (servers, databases, and load balancers.
  • Plan – Let’s Terraform know what you are doing before making any changes and also is a great sanity check to actually performing an action
  • Apply – Actually applies the creation or changes to an environment.

Basic Terraform Installation and VMware vSphere Automation

For the purposes of this walkthrough, I simply have an Ubuntu 16.04 LTS server that I have quickly imaged up in the home lab environment.  To level set, be sure to run all the latest updates on the server before getting started running any applications, etc.

Running-Ubuntu-updates-before-installing-Terraform Basic Terraform Installation and VMware vSphere Automation

Running Ubuntu updates before installing Terraform

To install Terraform, we have to think of the word “install” very loosely in this context here.  There isn’t really an “installation” process with Terraform.  You simply download the terraform executable from Hashicorp to your Linux installation and you are good to go!  It is that simple.  I think that is part of the huge success of Terraform as an automation tool.

To obtain the Terraform executable, simply visit the Hashicorp site and grab the executable for your specific platform:

As of the time of this writing, Terraform can currently be downloaded for the following platforms:

  • macOS
  • FreeBSD
  • Linux
  • OpenBSD
  • Solaris
  • Windows
Creating-a-terraform-folder-to-house-the-terraform-executable Basic Terraform Installation and VMware vSphere Automation

Creating a terraform folder to house the terraform executable

Copy-the-terraform-executable-inside-the-folder-you-created Basic Terraform Installation and VMware vSphere Automation

Copy the terraform executable inside the folder you created

Setting up Terraform Files for VMware vSphere Virtual Machine Creation

In the following VMware vSphere example, I will be creating a single virtual machine by cloning an existing VMware vSphere virtual machine template.  Let’s walkthrough the files created for a quick VMware vSphere automation demo.  Disregard the “old” folder as I had used this to copy files before creating new files for labbing.  The files below:

  • build.tf – This is a file that I created to house the actual provider information and the declarative state for Terraform to perform the actual build.
  • terraform – This is the terraform executable
  • variables.tf – This file contains our variables specific to the VMware vCenter environment.
Setting-up-Terraform-files-for-vSphere-automation Basic Terraform Installation and VMware vSphere Automation

Setting up Terraform files for vSphere automation

The provider information is configured like the following to connect to a VMware vSphere environment:

Inside the variables.tf file, our variables look like this:

Now the rest of the build.tf file contains the following after the provider information.  Most of the code blocks are pretty self explanatory.  We are feeding Terraform most of the pertinent vSphere information from vCenter.  This includes datacenter name, virtual machine network, the source virtual machine template, the name of the virtual machine clone.  Also in the customization spec, we can tell it what the computer name is going to be for Windows as well as the domain name to join, along with credentials to make that happen.  As you see below, we can also assign the IP address, gateway, etc.

Just to set the stage, after we have the files in place, we will want to do the following:

  • Run terraform plan – This tests the settings and is basically like running a “whatif” command.  This is very handy as it helps to identify errors before applying the declared state.
  • Run terraform apply – This actually runs the actions against the provider and resources.

Running Terraform VMware vSphere Virtual Machine Automation

There are a few things that you may run into if you are like me the first go around.  Terraform needs to initialize the needed plugins for vSphere, etc.  As you can see, after I had my files in place, I simply tried to run the terraform plan command and saw the following error of sorts.

Terraform-plan-error-due-to-plugins-not-being-initialized Basic Terraform Installation and VMware vSphere Automation

Terraform plan error due to plugins not being initialized

As directed, I simply ran the terraform init command which pulls down the needed provider plugins.

Running-Terraform-init-to-initialize-the-needed-plugins Basic Terraform Installation and VMware vSphere Automation

Running Terraform init to initialize the needed plugins

Now, a second try of the terraform plan command is successful.  Terraform will spit out the mock results of the intended creation or configuration.

Running-the-Terraform-plan-option-to-test-settings Basic Terraform Installation and VMware vSphere Automation

Running the Terraform plan option to test settings

Below is the rest of the output of the terraform plan command.

Results-of-running-Terraform-plan Basic Terraform Installation and VMware vSphere Automation

Results of running Terraform plan

Now, let’s actually run this.  Below, I am running the terraform apply command.  You will have to type yes to answer the “Do you want to perform these actions?” question.

Running-Terraform-Apply Basic Terraform Installation and VMware vSphere Automation

Running Terraform Apply

It now looks like we have actions happening.  As you can see, I have the terraform window overlaid on top of the vCenter web client.  We can see terraform working/waiting on the resulting actions in vCenter.

Terraform-apply-kicks-off-the-vSphere-virtual-machine-clone-from-template-job Basic Terraform Installation and VMware vSphere Automation

Terraform apply kicks off the vSphere virtual machine clone from template job

An error – In my first attempt, the clone was successful, but the customization was not.  In my case this was an issue with VMware Tools.

Terraform-customization-spec-error-encountered-virtual-machine-is-deleted Basic Terraform Installation and VMware vSphere Automation

Terraform customization spec error encountered virtual machine is deleted

After correcting the VMware Tools issue, the operation completed successfully.  The virtual machine was created as expected and the customization was ran successfully also.

Terraform-apply-successful-run-virtual-machine-is-created-and-customized Basic Terraform Installation and VMware vSphere Automation

Terraform apply successful run virtual machine is created and customized

Takeaways

Hopefully this Basic Terraform Installation and VMware vSphere Automation demo shows just how easy it is to setup a simple automation test of cloning a VMware vSphere virtual machine template and then customizing the resulting virtual machine.  Terraform is extremely efficient, lightweight, easy to stand up, and versatile.  Stay tuned for more in depth posts covering this powerful automation tool.  More to come!