vSphere templating with Packer

In recent times I started to utilise Devops Tools more and more in my daily work, as I try to optimise my workload. As my current company mostly uses private cloud (vSphere) and almost exclusively Windows, I am somewhat in a challenging situation – as nowadays everything is around the public cloud and Linux systems. Therefore I decided to not down my work here, maybe someone will find it useful, or worst case it will be a good way to remind future-me how this is done.

Setup

My setup currently looks something like this:

  • I have a Jenkins server deployed, this is wher I organize the pipelin
  • I have Packer installed on the same host (currently v.1.7.0)
  • I also have Powershell Core 7.1 (this is important, if you want paralellism support)

My repository with the code: fabricesemti80/Jenkins_Packer (github.com)

Process overview

To prepare this project, create a new pipeline in Jenkins.
I use Extended Choice Parameter plugin to select from the builds…

And the same to select the cluster

Also add the SCM link and credentials to the repo

The overview of the flow is as follows:

  • Jenkins first checks out the repository
  • In the folder of the repository the process (using PowerShell) there will be a new folder created for each build, separate for each selected cluster
  • In each build-folder there will be a .json file generated (it will be the combination of the *_builders.json, *_variables.json and *_provisioners.json files found in the JSON folder)
  • There will be also an “answerFiles” folder created within the build folders, with the answerfile insie (this will come from the sources/answerFiles folder, only the OS and the product key will be different (I use evaluation keys in this build); you will have to have PKEY2016 and PKEY2019 variables in Jenkins for this
  • Next, I ran packer validate-s (you need to have the VCENTER_PWD and the LOCAL_PWD variables available in the Jenkins variables. This will be wrapped in a Powershell script, and the validates – since they are pretty quick – will be done in serial manner. (I have 2 clusters and 4 editions, so it is only 8 quick validate tops)
  • The most important step next is the deployment. Here the same builder script will be called like in the previous step, but instead of “validate” it calls “build”, also I utilise pws-s paralell procesing feature
  • Finally when all build finished, I dispose of all the packer processes running on the host

In action

The process looks something like this in action

Fir

First we select the builds and clusters (obviously, if you want to have different names, builds, you are free to edit the code, although you need to edit these on quite a few places. I normally use vscode to edit all files in a folder).

Jenkins then will checkout the repo

This will be followed by the creation of folders for the selected build (in this case i selected 2016_core on the ‘bnw’ cluster).

Then comes the validation and the deployment

The deployment itself is also having a number of tasks:

  • first of course Packer builds a vSphere VM
  • Next it installs the Windows OS on this VM
  • As part of the OS install, it also installs VMWare Tools, however I found it that only gives network access and does not start the actual service. So I immediately after this install chocolatey and (re)install VMWare Tools. That usually works for me.
  • Once the OS is installed, provisioners kick in. I use Powershell for these mainly. I run a few local (on the VM) scripts.
  • ChocoInstall installs chocolatey itself. > Next I (re)install vmTools > I also configure WinRM > I deploy a few packages with chocolatey (if the server is desktop one: Edge and BGInfo) > I also configure BGInfo with some default values > I also prepare an autounattend.xml for Sysprep
  • Most importantly, after the PS provisioners, I use the Windows update Packer plugin to patch the servers. (How long this takes varies greatly; for 2019-s I have 2-3 months old ISO-s in the media library of vSphere, so half an hour; for 2016-s, since we do not really use them, the ISO-s are 2 years old and it takes 2 hours or so); this also involves a number of reboots

Eventually it should complete though.

Packer will turn the VM into a template, reusable for deployments (for example with Terraform) that I will probably document / show in another post.

Leave a Reply

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

Close Bitnami banner
Bitnami