Using Terraform to deploy VM with an additional disk in vSphere

Some time ago I wrote a post, which explains how to deploy VM in vSphere using a Terraform. It’s something like a universal Terraform configuration for vSphere. You don’t need to manually specify any variable. The most vars are based on the variable mappings. Today I want to show you another code – this time you will see how to create a VM with an additional disk. In theory, it doesn’t need many changes, but I am not confined just to create a disk. I also want to make it usable by the Operating System and ensure that disk will be mounted at the every system boot. Of course, all this stuff is powered by Terraform!

Short reminder

As you know (or not), I have made a project which contains the code for deploying single VM in the vSphere provider. All you have to do is change variables with your values, and then specify Resource Pool, Datastore, IP addresses and domain for Guest OS customization. It is not a difficult code because it allows you to create only a simple VM. One disk, one NIC, works only with a Linux VMs. If you haven’t seen that yet, let check my old post and also my GitHub repository.

Now, I will show you how to create the same simple VM with an additional disk. It’s not the rocket science, but you will see more than only Terraform code changes. In this case, I also use a bash script which mounts the created device.

Deploying VM with an additional disk

As I said before, it’s the same code, but with an additional disk section in the code, and an additional script. The section describing disk looks that:

There are only three parameters – the size of the newly created disk, its name (based on the vSphere disks name rules) and the disk type. I use “thin” disks everywhere I can, but I know that somebody may want to choose another type.

In theory, it would be enough, but we want to automate everything. Not only the deploying stage. And here we have an additional disk, but it is not mounted! Of course one can use Ansible or other automation tool for mounting, but I would prefer do it with Terraform. Since Terraform allows to run code remotely, why shouldn’t I use this opportunity?

Run code remotely during the deploy

Terraform uses provisioners for remote or local code execution. It helps during the creation or destruction your infrastructure – you can, for example, save some data to the local file or run some code on the newly created VM. In my case, I use three different provisioners:

The first one allows me to connect with my newly created Virtual Machine. It could be done with ssh connection and winrm. I’ve chosen ssh with the password authentication because it’s the simplest way of working with the vSphere and Terraform. If it was AWS I would use SSH key pair, and the provisioners would be different than now (I would use user-data for example).

The second one is used to copy files between the local Terraform host and remote, after its creation. In this case, I use it to copy my script into the Virtual Machine.

The third one serves me to execute my script on the remote host. In general, you can use this provisioner for the initial configuration, cluster bootstrap, install any software packages and more. Here it runs the script which mounts my newly created and attached disk.

The full content of the additional code:

Technical notes

I use the connection provisioner “globally”, for the whole Virtual Machine ( a resource from the Terraform point of view). If you check Terraform Documentation, especially “connection” section, you see that this provisioner can be used as a part of an another provisioner too.

Take a look at the “remote-exec” code section. You can see a line in which the script permissions are being modified. It is possible with the “in line” argument. It allows running the multiple commands one by one. Also, it is (for now) the only way to pass the argument to the executed script.

The script uses the simplest way of creating disk – LVM with a single VG and LV. In that case, the VM with an additional disk is created as the universal machine, with the simple device’s architecture, easy to understand for every SysAdmin. I have based on the script created by Edward Viaene towards his Terraform course needs. As his script isn’t complex, but it covers the whole necessities, the script from my repo is almost the same. It’s just a few commands, which don’t need to be different. To be honest, it’s hard to call it “script”. 🙂 The whole commands:

Huh, very simple, isn’t it? Of course, you can change it for your necessities. Other names, mount points, another filesystem etc. It’s just the example based on the another example.

Where can I get this fantastic code?

Of course, you can find it on my GitHub account. Just click on the image below and it will redirect you to my repository.

VM with an additional disk - repository

If you have any questions don’t hesitate to leave a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *