Gradually starting our IaC work here on the site, let’s move on to another post.
Today’s post is about packer, a packer that aims to create identical VM images for multiple platforms (Azure, AWS, VMware, etc) from a single source model.
Knowing the purpose of the tool, we can imagine several possibilities. You can standardize VM templates and make templates available to a provisioner such as VMware vRealize Automation, Cloud provider, or Terraform. However, you will still continue to fix bugs or certain aspects that your environment requires, is part of the game.
GOING BACK IN TIME
You must remember the processes to create a VM in VMware vSphere. Let’s say Windows Server 2016, 2019 or 2022, it used to be like this:
Download the ISO;
Guest OS selection;
Choose storage adapter for ParaVirtual SCSI;
Choose network adapter for vmxnet3;
Attach Windows ISO file;
Choose host or cluster to receive the VM;
Choose datastore or datastore cluster to store the VM;
Select VM Customization Specifications file;
Detach the Windows ISO;
Install or update VMware tools;
Run Windows Update one or more times until there are no more updates to install;
Shut down the VM;
Convert it to a template;
These are some must-do activities. Every VMware administrator knows how to use Customization Specifications, so the list could be longer without it.
Before we start, be aware that there are two approaches to automating model creation in VMware vSphere, they are:
VMware-iso provider. This provider creates VMware VMs from an ISO file as a source.
vSphere-iso provider. This provider, originally created by JetBrains, integrated with the official Packer repository and released with Packer since version 1.5.2, builds VMs on VMware vSphere directly using the vSphere API.
Which one will we use in this post? vSphere-iso provider! That said, here we go!
vCenter: Check your vCenter version, it must be at least 6.5.
Download the OS version that we will be working on in this post: https://yum.oracle.com/oracle-linux-isos.html
We will only work with HCL files which is the motivation of this post and that the manufacturer adopted as default recently.
We have three files, the first one you can see now, a variable file: vsphere_secret.pkrvars.hcl. Note from a blog reader. Hide passwords. We will have a next post talking about vault. Wait 🙂
This file contains an administrator user of the vSphere environment and another user that will be created using the kickstart.cfg file that we will talk about later.
The second variable file named: vsphere.pkrvars.hcl.
Contains all variables available for creating a VM. However, I enabled only the most used and performance related ones.
And finally the last file, which contains the build, the OracleLinux-7.9.pkr.hcl. I will only put the most relevant images with the explanations.
The image above contains the part of the code with the structure of the variables and their type.
Another part of the code, the most important because from this stage onwards your operating system receives instructions necessary for its operation. Highlight for the configuration of Floppy and boot_command.
Final part of the code.
Here you can run different commands or if you want to call scripts, with the OS installed. I created an example called “echo Visit my blog https://virtualizandoaju.com/ :)”
Now we move on to the final phase of execution. Prepare the Red Hat kickstart.cfg response file. Consult the official documentation if you have never used it. You can see a part of the code below:
It’s a response file. In it I have all the necessary configurations for the installation of the OS and post installation configurations. It is very important that this file is well configured.
It’s time to run the command: packer build -var-file .vsphere_segredo.pkrvars.hcl -var-file .vsphere.pkrvars.hcl .OracleLinux-7.9.pkr.hcl
That’s it folks! Thank you for showcasing my work. My complete code is available on github.
“One of the best options for advancing, protecting yourself and progressing in life is to become self-taught. Think about it!”