Are you still creating vSphere images manually? Modernize your approach by adopting Infrastructure as Code (IaC) with HashiCorp Packer and Terraform. This blog post and accompanying video will guide you through the virtual machine settings, ISO image creation, and the entire Packer workflow.
For those who prefer a visual tutorial, here’s a video demonstration:
You can skip to the relevant chapters below:
- 00:00 – Introduction to VMware vSphere, HashiCorp Packer, and Terraform
- 01:32 – Packer and Creating Windows Templates
- 08:28 – vCenter View of Template Created
- 09:31 – Terraform: Building VMs from Templates
- 16:31 – vCenter View of VMs Created: Multiple Platforms
- 17:02 – Summary and Conclusion
As organizations transition to on-prem virtualized environments, there’s a growing emphasis on tools like HashiCorp Packer and Terraform. Our journey involves two key steps, which include Packer variables and Packer configuration:
- Creating a Windows Server 2019 Template: Generate a virtual machine image and save it as a Packer template in vCenter using Packer.
- Deploying VMs using Terraform: Use Terraform to provision virtual machine images based on the Packer-created template.
Subscribe to my newsletter to get FREE access to the Packer variables and Packer configuration files, including the JSON template for virtual machine settings.
The following is required to follow along:
- Install Packer (tested on Packer v1.6.6)
- Terraform (HashiCorp Configuration Language supported)
- Access to a vSphere instance (tested on vSphere v6.7)
Overview of Packer and Terraform
HashiCorp Packer is an open-source tool designed to automate the creation of identical machine images for multiple platforms from a single source configuration. With Packer, you can codify everything that is required to create a machine image, making the process highly reproducible and easy to share with team members. Whether you’re working with cloud environments or on-premises virtual machines, Packer helps to ensure consistency and speed up the deployment process. It defines all its settings, such as variables, provisioners, and post-processors, in a JSON-formatted template file, offering a programmatic way to create ready-to-go machine images.
Terraform, another stellar product from HashiCorp, takes the automation a step further. While Packer focuses on creating machine images, Terraform is designed for building, changing, and version controlling the infrastructure efficiently. It uses a declarative configuration language called HashiCorp Configuration Language (HCL), allowing you to describe the end state you want for your infrastructure. Terraform then works out how to achieve that end state, whether it involves setting up network configurations, spinning up virtual machines, or any other infrastructure-related task. This powerful tool works across multiple cloud providers and on-premises setups, helping businesses manage complex infrastructures in a unified manner.
Packer and Terraform with VMware vSphere
When it comes to VMware vSphere, the integration of Packer and Terraform offers unparalleled automation and efficiency. Packer can be configured to create virtual machine images tailored specifically for VMware vSphere. It captures all the software, virtual machine settings, and configurations into an ISO image, which can then be effortlessly deployed across your VMware environment. The outcome is a highly customizable, standardized, and automated approach to managing vSphere machine images. It eliminates manual effort, ensuring that you can rapidly roll out new instances or changes while adhering to organizational standards and best practices.
In a VMware vSphere context, Terraform’s role is equally important. Once Packer has created a standardized virtual machine image, Terraform can provision new virtual machines based on that image within the vSphere environment. By defining your requirements in the HashiCorp Configuration Language, you can deploy multiple, identical virtual machines without manual intervention, manage resource pools, and even configure networking settings, all directly from your Terraform scripts. The pairing of Packer for image creation and Terraform for infrastructure deployment within vSphere results in a powerful, automated, Infrastructure as Code (IaC) pipeline that significantly simplifies virtual machine management.
More on Packer
If you’re new to Packer, getting started is straightforward. You can visit the official Packer website to find the download link for the open-source tool. Whether you’re on Linux, Windows, or macOS, installation guides are available to assist you. In most cases, you’ll interact with Packer through the command line.
A typical Packer workflow involves defining a JSON or HCL file that outlines the source block, build block, and variable block.
- The source block specifies the base image you’ll be working with, be it from a cloud provider or a local vSphere ISO builder.
- The build block defines the provisioners and post-processors that will customize this image.
- The variable block allows you to parameterize your configurations, making it easy to reuse or share your code.
- You can then execute Packer builds using the `packer build` command, and Packer will generate golden images according to your specifications.
One of the exciting facets of HashiCorp Packer is its support for multiple operating systems, including various Linux distributions like Ubuntu, CentOS, and Rocky Linux. You can even create golden images for different cloud platforms in addition to VMware vSphere, making Packer extremely versatile in multi-cloud environments. When it comes to vSphere, Packer connects to the vSphere endpoint and typically runs as a root user, allowing it to execute necessary commands. You can insert an SSH key for secure communication or run a `sh script` to bootstrap your instances, enhancing both security and functionality.
For those looking to dive deeper into more advanced use-cases, HCP Packer, a cloud offering from HashiCorp, allows you to centralize and automate template creation in a scalable and collaborative way. The process begins the same way as it does with open-source Packer; you define your configuration and variables. HCP Packer also integrates seamlessly with Terraform Cloud, providing a complete Infrastructure as Code solution from image building with Packer to resource provisioning with Terraform. This holistic approach ensures that your template is ready for deployment across various platforms and services, thus demonstrating the power of integrating HashiCorp tools.
Below is our setup diagram.
Identical Machine Images and Virtual Machine Templates
Creating identical machine images using Packer creates a standardized, repeatable, and self-documented process. Virtual machine templates simplify the deployment across multiple platforms, ensuring uniformity and compliance.
Virtual Machines and Machine Images
To build a virtual machine, the first step is to create a machine image using Packer. This image is essentially a single source configuration that contains all the necessary files, environment variables, and scripts. By using the Packer build command in your command line, you initiate the image creation process. Once you have this machine image, you can easily clone it into virtual machines using Terraform, avoiding the manual process entirely.
HashiCorp Packer Section
Let’s start by discussing the Packer section of the provided GitHub repository. We would like to credit Guillermo Musumeci for his insightful Medium post and the corresponding GitHub repository. Guillermo’s post serves as a step-by-step guide on using Packer to build Windows Server templates for VMware vSphere. The GitHub repository contains all the necessary scripts and configuration files needed for the template creation process.
Within the packer/vsphere/iso/windows folder, you will find various subfolders. The scripts folder contains scripts used during the template creation process. The windows2016-base and windows2019-base folders contain the JSON files used by Packer to build the respective templates.
To begin the template creation process, you need to define variables at the top of the JSON file. These variables include the vSphere server details, username, and password. It is recommended to keep sensitive information separate and not include it in the GitHub repository.
Next, you define various parameters such as the data center name, cluster name, datastore name, network, folder for saving templates, VM or template name, CPU and memory specifications, disk provisioning, ISO path, and more. Additionally, you can choose to specify a Windows administrator password.
The most critical part is the builder section, where you provide details about the vCenter server, including the previously defined variables. Here, you also mention the required data center, cluster, datastore, and folder details.
The final section involves configuring the customization and adding any necessary scripts. The auto unattend xml file allows for the unattended installation of Windows, and the scripts folder contains additional scripts for different purposes.
To build the template, navigate to the windows2019-base folder in the command line and run the Packer command
packer build -var-file=variables.json win2019-base-thick.json
This will initiate the template creation process, which will take some time. Once completed, you will have a template stored in your vCenter.
The Terraform section of the repository contains the necessary files for creating a virtual machine using the previously created template. The main Terraform file is used to define the provider, which in this case is vSphere. It is recommended to pin the version of the provider used.
Similar to the Packer section, you need to define variables at the beginning of the file. These variables include the vSphere server details and template name. It is essential to keep sensitive information secure and not include it in public repositories.
Next, you define the resources for creating the virtual machine, including the name, CPU, memory, disks, and other specifications. It is crucial to choose the appropriate disk provisioning based on the template used.
The Terraform file also allows for customization of the virtual machine, such as changing the computer name and workgroup. You can also define the network interface configuration, IP address assignments, and customization timeout.
Once all the necessary configurations are in place, you can run the Terraform commands to plan and apply the changes. This will provision the virtual machine based on the template and configurations provided.
- Waiting for IP: If Packer gets stuck on this stage, check your DHCP server.
- vSphere Console: Open the vSphere web console to get insights into the VM creation process.
In this comprehensive guide, we’ve shown you:
- How to create a Windows Server 2019 image using Packer’s JSON file and HCL (HashiCorp Configuration Language) syntax, which automates the installation process for major operating systems.
- How to provision Windows Server 2019 VMs using Terraform by leveraging the virtual machine templates generated by Packer.
We’ve made this process repeatable, self-documented, and applicable to various Linux distributions as well. All source code and JSON files can be found in our HashiCorp repository.