Terraform and vSphere - mk2
This article focuses on improving on our mk1 vm deployment project.
Deploying vSphere VMs with Terraform - mk2
In our previous article we went through a basic deployment of a vsphere vm, cloned from an ubuntu 20 server template.
Today I will be looking at how we can improve it and make it more production ready. For the full code example, please see the github link here: vsphere-vm-mk2
File organization
Putting all of your code in a main.tf
is acceptable when getting started or writing our vSphere
mk1 example. However, In most situations, you are better off following a file structure like this:
|
|
main.tf
- calls your modules, locals, and data sources to create all resources.
variables.tf
- contains the declared variables used in your main.tf
file.
outputs.tf
- Contains all of the outputs from the resources created by your main.tf file.
versions.tf
- Contains all of the version requirements for Terraform and the providers used.
terraform.tfvars
- Contains all inputs that we put in variables declared in the variables.tf
file.
Adding Variables
Variables are the next step in making our mk2 revision of our vSphere deployment.
The sections below contain the data we will use in the main.tf
file.
The significant difference with this code block compared to our mk1 is removing hard-coded data
values; for example, name
and num_cpus
have been defined as variables; var.vm_name
and var.num_cpus
.
I’ve left disk label
as a hard-coded value since I plan on having this setting the same on
all of my VMs. However, you can choose to convert it to a variable as well.
|
|
Now that we have our variables placed, we need to declare them in a variables.tf
file.
Declaring Variables
We will need to declare the variables that we are using in our main.tf
to be populated.
There are three main types of variables I will be using in our variables.tf
file.
Type Contraints
These variable types are known in terraform as type keywords
string
- String values are always treated as text, even if they contain only numbers.
number
- number variables are variables that must take a number value 0, 1, 2,3,…)
bool
- Boolean variables contain one of two possible values, true
or false
Type Constructors
Type constructors work like functions, that take 0 or more values and give back a new value, such as a collection of data.
list
example of declaring variables:
|
|
Outputs
You may already be familiar with outputs since we used them in our mk1 file. The difference here is
that we take them out of the main.tf
file and place them in a separate outputs.tf
file. Doing so
will allow us to consolidate our outputs code into one place.
Below is an example of the outputs.tf
file I will be using:
There are many different outputs that can be given from a deployment. For simplicity, I am only specifying IP address and hostname.
|
|
TFVARS
WARNING
Make sure NOT to upload a populated
tfvars
file into any publicly available repository, or any place you don’t want secret information listed. Thetfvars
file I am hosting in this project has been sanitized for the purposes of the article.
.tfvars
files are known as “variable definitions files,” which gives us a convenient method for
inputting data into our variables.
tfvars
files can be specified directly during a deployment or automatically loaded depending on
how they are named. for more insight into tfvars
, please see the documentation listed here:
For the full example of the tfvars
file I will be using:
tfvars file
An excerpt of the tfvars
file I will be using is listed below:
|
|
Closing thoughts
From here, you can see we are starting to get some more organization around our code. Moving forward, we will look at modules and how you can take your newly constructed mk2 vSphere project and transform it into a consumable module.