Reduce Your Virtual Machine Build Time Using Hashicorp Packer: [Part 3 - Environment Setup]

automation packer Dec 28, 2023
hashicorp packer part 3

Introduction:


In Part 2 of this series, we reviewed the different aspects of a Packer template file.  In this post, we'll begin creating the HCL template file for the Windows Server 2022 golden image.  Here's what you'll have by the end:

  • Install tools we'll use to create the HCL file and associated scripts.
  • VMware Workstation Pro - Hypervisor used for the virtual lab.
  • Download Windows Server 2022 evaluation edition iso.
  • Create the folder structure for our project.  We'll use several scripts to build and configure the golden image.
  • Variables file created variables.pkvars.hcl file.
  • Template file started (we'll build this out over the next few posts) - Win2022.pkr.hcl

Step 1: Tools Installation


In Part 1, we installed Hashicorp Packer and Chocolatey on Windows 11.  Although you can write the *.hcl files using Notepad, I prefer Visual Studio Code as there is a Packer plugin to help with IntelliSense.  We'll use Chocolatey again here to install VS Code.

  1. Open Powershell as an administrator.
  2. Run the following command to install VS Code - choco install vscode -y 
  3. Install VMware Workstation Pro.  You can use the trial version for up to 30 days.  It's currently $199 to purchase. Packer also works with other hypervisors, but I'll use VMware Workstation Pro throughout this series.  Run the following command in the same Powershell window - choco install vmwareworkstation -y 
  4. Restart your computer.

  5.  After the restart, open VS Code.  We need to install the Powershell and Packer extensions.  Click on extensions on the left menu, search for "packer" and click install.
  6. Next, search for Powershell and install the extension.
  7. Create a folder named "AutomatedSandboxFramework."  This will be where all files we use to build the golden image will reside along with the golden image themselves.
  8. Last tool - Install Windows Terminal if it isn't already.  Instructions for how to do that can be found here.

Step 2: Folder setup and ISO download


You'll need at least 20 GBs of space available.  The image we build won't use much, but if you plan to create virtual machines from this image, you'll need additional space.  

  1. Create a folder named "AutomatedSandboxFramework" on a drive that has free space.  
  2. Within this folder, create another folder named "ISO."  We'll store the ISO here.
  3. Download the Windows Server 2022 evaluation ISO using the link below.  Once downloaded, move the file to the ISO folder created above.
    1.  https://software-download.microsoft.com/download/sg/20348.169.210806-2348.fe_release_svc_refresh_SERVER_EVAL_x64FRE_en-us.iso

  4. Next, create another folder named "Win2022" within the AutomatedSandboxFramework folder.
  5. Create another folder, within Win2022, named scripts.  Your folder structure should look like the screenshot below.
  6. Open Visual Studio Code and proceed to step 3.  

Step 3: Create the empty HCL template and variable files


  1. In VS Code, open the AutomatedSandboxFramework folder.
  2. Click new file and name the file Win2022.pkr.hcl.
  3. Create one more file and name it variables.pkrvars.hcl.

Step 4: Add packer, source, and build blocks to HCL template.


Next, add the following blocks to Win2022.pkr.hcl template.  Your file should look like the screenshot below when finished.

Next, open Windows Terminal and change your directory to where you saved Win2022.pkr.hcl.  Then run "packer init win2022.pkr.hcl"  This command will download and install the required plugins: VMware and Vagrant.

Conclusion:


You should now have the structure and tools installed to create your golden images.  Refer to [Part 2] as a refresher on the blocks we added to the HCL template file.  In the next post, we'll create the scripts to configure our image and the autounattend.xml answer file.  Check out the guide below for more information on setting up your sandbox.

Thanks for stopping by.

 

Whenever you're ready, there is one way I can help you gain hands-on experience:Ā 

Automated Sandbox Fundamentals: I teach how to build a virtual lab using automation in this course. Learn how toĀ create golden images, using both Windows and Linux, to easily spin up and add additional machines to your sandbox.Ā  It's packed with 8 modules and the scripts you'll need to build your environment.Ā  Start small, andĀ scale as needed by easily changing the configuration file included with the course.

Give Me the Details