Sergey Nivens - Fotolia


Use Terraform, VMware vSphere to enable declarative configuration

Go beyond traditional vSphere management with Terraform, which enables infrastructure configuration as code. Use commands to set the providers, data centers and compute clusters.

You can combine Terraform and VMware vSphere to enable a declarative configuration that makes infrastructure easy to set up and maintain.

Terraform is a HashiCorp tool that enables the abstraction of infrastructure configurations into code that resembles an easy JavaScript Object Notation language. It can create a declarative definition of what the infrastructure should look like and, because it's in code, it can save the configurations in version control. This ensures predictable maintenance for the infrastructure.

Terraform and VMware vSphere present a compelling context for this functionality. Terraform can provide benefits when managing a vSphere environment, particularly in regard to core vSphere components.

Typically, VMware administrators manage vSphere manually by logging into a management interface and clicking through to configure the environment. During this process, it's easy to forget the details of these configuration decisions. It's even easier to forget the network and storage settings to ensure standardized configurations across the same hosts.

There are many tools available for vSphere management automation, but Terraform is the most relevant for defining infrastructure configuration and maintaining those configurations in version control. The most crucial part of maintaining effective automation is minimizing configuration drift.

The main source of automation complexity originates from the different blocks of logic that must be accounted for in order to configure the vSphere infrastructure to particular specifications. Similarly complex is figuring out what to do when the infrastructure no longer resembles the original automated configuration.

With Terraform, VMware vSphere admins can declare what the infrastructure should look like and maintain that state. Terraform abstracts the complexities and logic involved to ensure that the infrastructure matches what is in the code itself. Running Terraform can also help you visualize any changes made to the code.

Terraform and VMware vSphere configuration commands

The provider is the first thing you must configure in Terraform. The provider is responsible for the API interactions with a product that expose the resources available for consumption. This example will show you how to set up a provider with Terraform for VMware vSphere.

There are only a few parameters you need to configure to set up a vSphere provider.

Parameters for a vSphere provider
Figure A. Set up a vSphere provider.

The code in Figure A tells Terraform that this example will be using the vSphere provider and includes the username, password and server. Terraform also makes it easy to communicate with a stand-alone, licensed ESXi host or a vCenter server.

After setting up the provider, define the data center you want to create. For simplicity, this example assumes there's an existing vCenter.

Creating a vSphere data center is as easy as creating a provider.

Data center creation code
Figure B. Create a vSphere data center.

The short bit of code in Figure B will tell Terraform to create a new vSphere data center named our_example_datacenter. Note that the code telling Terraform that the vSphere data center is a resource means it will create one. It's also possible to consume an existing data center by changing resource to data. Terraform then consumes that existing data center.

For this example, assume that the data center configuration is complete and there are three stand-alone hosts already added to vCenter that are ready for consumption. This example will show you how to consume existing resources to add additional configurations to the infrastructure.

Code for additional configurations
Figure C. Use the existing resources to add additional configurations.

The example in Figure C instructs Terraform to consume example_datacenter and informs it of three hosts -- esxi1, esxi2 and esxi3 -- with which to create a new vSphere compute cluster named terraform-example-cluster. Additionally, this enables Distributed Resource Scheduler (DRS), sets the DRS automation level to fully automated and enables High Availability.

All of the code in Figure C is useful, but it isn't complete without an available data store and networking.

Data store configuration instructions
Figure D. Create a data store and networking system.

The code in Figure D instructs Terraform to create a new Network File System (NFS) data store named terraform-example by connecting to the NFS server with /export/terraform-example and attaching it to all three of the hosts.

This also instructs Terraform to create a standard vSwitch named vSwitchTerraformExample using vmnic0 and vmnic1 on each host, and sets the active and standby network interface cards accordingly. Terraform creates a vSphere port group on each host named PGTerraformExample using the vSwitch created in this example.

To configure these settings, apply the Terraform plan command to visualize the configuration and then execute the Terraform apply command to actually administer the configuration.

Terraform provides numerous capabilities at a high level of abstraction, which makes it easy to manage vSphere environments. Together, Terraform and VMware vSphere form a potent configuration management tool set.

Dig Deeper on VMware ESXi, vSphere and vCenter

Virtual Desktop
Data Center
Cloud Computing