Terraforming Your vSphere Environment

In part two of this series on getting started with Terraform in a vSphere environment, we will focus on initializing the Terraform folder and producing a plan of the changes our definition will make. Before we can execute our plan, we need to ensure that the required providers exist within our project structure.

To initiate the Terraform process, we open a command prompt and navigate into our `terraform/deploy_datacenter` folder. Once in the folder, we run the command `terraform init`. This command processes the files in the current folder, looking for references to providers. In our case, it will find a reference to the vSphere provider and download it directly from Hashicorp.

Once the provider is downloaded, we have everything we need to execute our first Terraform run and create a new datacenter in our vSphere inventory. However, it’s good practice to run a plan in advance to check what will happen before we apply the changes. To do this, we save the plan output so we can run the `apply` command later, passing in the plan file.

To generate the plan, we execute the command `terraform plan -var “datacenter=our_new_datacenter” -out=newDC.plan`. This tells Terraform to create a plan recording the changes that will take place, with the datacenter variable set to our_new_datacenter. The `-out` flag specifies the output file name as `newDC.plan`.

If we check the folder, we will now see the `newDC.plan` file. However, if we try to open this file in a text editor, we won’t be able to read it! Luckily, Terraform allows us to output the plan to JSON. To do this, we use the command `terraform show -json newDC.plan`.

This will output the plan as JSON, which we can redirect to a file (e.g., `terraform show -json newDC.plan > newDC.plan.json`) and then paste the contents into our favorite JSON viewer to make viewing easier.

Now that we have our plan file saved, we can apply the plan, creating our datacenter object. We will do this in part three of this series.

In conclusion, this blog post has covered the initializing of the Terraform folder and producing a plan of the changes our definition will make before applying them to create a new datacenter in our vSphere environment. In the next part of this series, we will apply the plan and create the datacenter object.

Leave a Reply