Building a Windows Server 2022 OVA with Packer (Updated)

As promised in my previous post about enabling SSH as a communicator on Windows in the automated Packer build I am now releasing this automated build and writing about it.

Update 20th January 2022

I updated the project with a breaking change, so according to Semantic Versioning the new version is 2.0.0.

I changed the build file from JSON to the new Packer HCL2, did some updates and clarficiations to the readme and removed the requirement for VMware’s ovftool to create the OVA in the Gitlab-CI build.

I updated the article below to reflect these changes.

Overview

This build uses Microsoft’s eval ISO image of Windows Server 2022, runs an unattended installation of it in a brand new virtual machine, runs some commands to turn the eval into a full blown Windows installation with KMS key, enable the SSH communication, etc. and then hands over to Packer to run some more customization on the system before sysprepping it and extracting an OVA file from the building vSphere ESX server.

This OVA can be used for easy deployment of new virtual machines in your lab and test environment. This is not meant to be used in production, but of course you can take inspiration for your own production build.

Requirements

Before even thinking about using this project be aware of the following requirements:

  • I am using the vsphere-iso builder. This requires a VMware vCenter from version 6.5 and that means a vSphere license.
  • I am using a local installation of Gitlab-CE and Gitlab-CI for my build automation. If you want to build automated you can do the same but manual builds from the CLI are possible. In my config I run Pipelines only when started manually from the Web UI and not on every commit.
  • I am using the Microsoft Windows Server 2022 Datacenter KMS key for the automated build – this will require a Microsoft key management server with proper volume license keys in your environment. Of course you can change the build to stay in eval mode or use your own key in the unattended file.

Get the code

I published the code on both GitHub and GitLab. I am running this on my own lab in GitLab using Gitlab-CI for automated builds. For now I don’t see a possibility for public builds because of licensing restrictions (VMware and Microsoft) and of course no chance to release a built OVA image of Microsoft’s OS.

Get started

I have written an extensive README but still want to go step by step on how to get your first build and some useful things to modify if you like. I’ll use Gitlab as the repository but you can adapt to above’s Github repository where necessary.

First off, install Packer on your dev machine. If you’re on Windows I do recommend using Chocolatey for this.

Next clone the git repository and change into the folder.

git clone https://gitlab.com/StefanZ8n/packer-ws2022.git

Next prepare a Packer variable file myvarfile.json to specify the needed variables for the build. Check out the Packer documentation on User Variables for the different ways of specifying these variables. I am using environment variables in my automated build, so you could also use local environment variables to input values to Packer. Checkout the project’s README for more info on this.

{   
    "vcenter_server": "vc.demo.local",
    "vcenter_user": "myser@vsphere.local",
    "vcenter_password": "Passw0rd.",
    "vcenter_datacenter": "Nested",
    "esx_host": "buildhost.demo.local",
    "esx_user": "root",
    "esx_password": "Passw0rd."
}

Next you already can run the Packer build after initializing the packer plugins

packer init -upgrade ws2022.pkr.hcl
packer build -timestamp-ui -force -var-file=myvarfile.json ws2022.pkr.hcl

The build will take some time and then output you an OVF template of your new WS2022 installation in the build directory.

Have fun running some builds, adapting and improving this. I’ll be happy to see some comments and feedback on this project.

One Reply to “Building a Windows Server 2022 OVA with Packer (Updated)”

  1. You might be able to get this building without needing a local install of gitlab server, gitlab-emulator works on windows and should work with this (It’s my pet project)

Leave a Reply

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