This is part two of a series aimed at getting you going with Terraform in a vSphere environment. The series will assume you understand the concepts of IaC and Terraform, at a basic level. In part one, we setup Terraform and created a basic definition to create a virtual datacenter. In part two we will initialise the Terraform folder and produce 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. Out project structure is dictated by our folder structure. Therefore, we open a command prompt and navigate into our \terraform\deploy_datacenter folder. Once in the folder we run terraform init. This command will process the files in the current folder, looking for references to providers. In our case, it will find a reference to the vSphere provider and so will commence downloading it directly from Hashicorp. Once downloaded, you will find a new subfolder under deploy_datacenter called .terraform. In here, you will find the relevant provider, in our case a 64bit windows executable file which is the vSphere provider.
We now have everything that we need to execute out first Terraform run and create a new datacenter in our vSphere inventory. We could go right ahead and do this, but it is good practice to run a plan in advance so we can check what we happen. We will save the plan output so we can run the apply command later, passing in the plan file. This will ensure that when we create our datacenter object (whether it is in 5 minutes or 5 days!) we know exactly what we are getting. We will execute the following command
terraform plan -var “datacenter=our_new_datacenter” -out=newDC.plan
As you can see, we pass the plan flag to Terraform to tell it that we want to generate a plan recording the changes that will take place. The -var “datacenter=our_new_datacenter” sets our datacenter variable to our_new_datacenter. Finally the -out=newDC.plan tells Terraform that we wish to save the plan output to a file (called newDC.plan). We can then apply this plan later on.
If you check the folder you will now see that we have a newDC.plan out file. If you try and open this in a text editor you will find you cannot read it! It would be very useful if you could see the plan contents in plain english wouldn’t it? Luckily, Terraform will allow us to output a plan to JSON. Use the terraform show -json newDC.plan command to do this.
Of course, you can redirect this output to a file (terraform show -json newDC.plan > newDC.plan.json)and then paste the contents into your favourite 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.