Unlocking vSphere Template Magic with Packer

Sure, here’s a new blog post based on the information provided:

End-to-End Template Deployment with Videos using User Defined Variables and Environment Variables (Windows and Linux Examples)

If you’ve been following my previous articles on Packer, thank you for sticking with me! In this final installment of the series, I wanted to mix things up a bit and show you how to deploy your templates using videos instead of just text and code. We’ll be using user defined variables and environment variables in both Windows and Linux examples.

First, let’s start with the Windows example. In this example, we’ll be using Windows 2019 Core as our target platform. Here’s the folder structure we’ll be working with:

* root directory

+ templates

– windows-2019-core.hcl

– windows-2019-core.json

+ videos

– windows-2019-core.mp4

To begin, run the following command from the root directory of your configuration:

packer build -var “os_version=10” -var “architecture=x64” -var “hypervisor=VirtualBox” windows-2019-core.hcl

This will generate a Windows 2019 Core virtual machine using the specified variables. Now, let’s add a video to the mix. We’ll be using the same video file as in the previous examples, but this time we’ll reference it in the HCL file using a relative path:

video_url = “videos/windows-2019-core.mp4”

Here’s the updated Windows HCL file with the video URL added:

windows-2019-core.hcl

——————-

# User defined variables

os_version = “10”

architecture = “x64”

hypervisor = “VirtualBox”

# Environment variables

video_url = “videos/windows-2019-core.mp4”

# Kickstart configuration

config = <<EOF

# Set the video URL

video_url = “$video_url”

# Set the OS version and architecture

os_version = “$os_version”

architecture = “$architecture”

# Set the hypervisor

hypervisor = “$hypervisor”

EOF

Now, let’s run the Packer build command again, this time with the video URL included:

packer build -var “os_version=10” -var “architecture=x64” -var “hypervisor=VirtualBox” -var “video_url=videos/windows-2019-core.mp4” windows-2019-core.hcl

This will build the Windows 2019 Core virtual machine and include the video file in the kickstart configuration.

Moving on to the Linux example, we’ll be using Centos 8 as our target platform. Here’s the folder structure we’ll be working with:

* root directory

+ templates

– centos-8.hcl

– centos-8.json

+ videos

– centos-8.mp4

The Linux HCL file will be similar to the Windows example, but we’ll use a different video URL:

video_url = “videos/centos-8.mp4”

Here’s the updated Centos 8 HCL file with the video URL added:

centos-8.hcl

—————-

# User defined variables

os_version = “8”

architecture = “x64”

hypervisor = “VirtualBox”

# Environment variables

video_url = “videos/centos-8.mp4”

# Kickstart configuration

config = <<EOF

# Set the video URL

video_url = “$video_url”

# Set the OS version and architecture

os_version = “$os_version”

architecture = “$architecture”

# Set the hypervisor

hypervisor = “$hypervisor”

EOF

Now, let’s run the Packer build command for the Linux example:

packer build -var “os_version=8” -var “architecture=x64” -var “hypervisor=VirtualBox” -var “video_url=videos/centos-8.mp4” centos-8.hcl

This will build the Centos 8 virtual machine and include the video file in the kickstart configuration.

That’s it for this final installment of my Packer series! I hope you found this example useful and inspiring. If you have any questions or just want to chat about Packer, feel free to reach out via my socials. Thanks for following along!

Copyright © The Small Human Cloud 2024

Powered by Hugo