Recently someone sent out a request via Twitter on how to automatically update images. I suggested using Hashicorp Packer but not with the default vmware-iso builder but the one created by the jetbrains-infra team over at Github because it uses the vSphere API instead of a SSH backdoor like the official vmware-iso builder does.

Infrastructure as code is a big thing nowadays. So I thought it would be nice to enable you to release your inner DevOps engineer. And demonstrate how to implement this awesome tool.

First thing you need to do is download Packer for your platform. There are versions available for macOS, FreeBSD, Linux, OpenBSD, Solaris and Windows. The great thing about Packer is you don’t really need to install it. It’s just a single executable that you can extract to a location of your choice.

Next you need to grab the jetbrains-infra vmware-iso builder. Beware that the jetbrains-infra builder is available for macOS, Linux and Windows only. In my case I’m using Windows so I got the .exe and moved it to the location of my Packer executable. Once all this is in place you can start building your first VM. In order to build a VM you need the build instructions.

To get up-and-running fast I decided to use one of the examples; an Ubuntu VM. I copied over the example files, updated the username and password values and ran Packer for the first time. Unfortunately……. ERROR.

Turns out that this was simply a matter of RTFM; all objects that are not unique need to be defined explicitly. In my case this meant adding the following options to the example JSON file:

  • datacenter
  • cluster
  • resource_pool
  • datastore
  • network

Now the network property demanded some extra attention as the same portgroup name was used multiple times in my environment. If this is the case you can use the following format to define the network: “folder/portgroupname”. With this last issue sorted out I succesfully built my first VM with Packer.

I didn’t like the fact that your vSphere credentials are readable so I looked for a way around this. The easiest option was to use user variables in the template. I created a new JSON file with two variables: vcenter_username and vcenter_password. And I included those variables in my template (don’t worry I’ll display the final result on the bottom of this post). On all subsequent builds I used the -var-file parameter to include the variable file which containes my username and password. Having an option to use hashed credentials would be preferable but this was an easy fix.

So to summarize, if you want to start using Packer with vSphere you’ll need to do the following:

  • A copy of Packer
  • A copy of the jetbrains-infa vsphere-iso builder
  • A build template
  • A variable file containing vSphere credentials (optional)

When you put all these things together you can start building all your VM images from code. And from there who knows what cool things you can do? Maybe automatically convert the generated VMs to a template and put them in a content catalog. Subscribe to that catalog from all your vCenters, vCloud Director instances. The sky is the limit!

This is the final product, an optimized Ubuntu build template. Happy coding.

Share this if you found this interesting.

Leave a Reply

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