In my previous post I’ve created a Ubuntu template into VMware vSphere using Packer. Time to build now a Microsoft Windows template.
NOTE: You can grab all the files that I’m presenting here from my Github repo:
Some parameters in the public files have been anonymized, please customize the values to suit your needs.
File structure
Some configuration files are similar to the ones I used for Ubuntu Linux. So I will highlight here just the differences. These are the files that I’m using:
vsphere.pkrvars.hcl
This is the file where I store all the information needed to connect to the vSphere environment.
win2019.pkrvars.hcl
In this file I describe the virtual machine I want to create. Let’s have a look at the content:
# HTTP Settings http_directory = "http" # Virtual Machine Settings vm_name = "win2019" vm_guest_os_type = "windows2019srv_64Guest" vm_version = 17 vm_firmware = "bios" vm_cdrom_type = "sata" vm_cpu_sockets = 1 vm_cpu_cores = 2 vm_mem_size = 4096 vm_disk_size = 40960 thin_provision = true disk_eagerly_scrub = false vm_disk_controller_type = ["lsilogic-sas"] vm_network_card = "vmxnet3" vm_boot_wait = "5s" winadmin_username = "Administrator" winadmin_password = "******" # ISO Objects os_iso_path = "[vsanDatastore] contentlib-5ed427e9-8cf4-442a-8218-14a8d3cc9dc3/403f36cb-0df1-4712-a50f-87b398259f75/Windows_Server_2019_083310ca-7b8f-4883-bed2-5079485abd42.iso" vmtools_iso_path = "[vsanDatastore] contentlib-5ed427e9-8cf4-442a-8218-14a8d3cc9dc3/117f18e4-30e3-4ce0-b6bf-8e27a0166cd0/windows_aba4fd51-b25d-453f-b943-f59a6e8d5af8.iso"
win2019.pkr.hcl
In this file I defined all the variables, and instruct Packer to read the values that are defined in win2019.pkrvars.hcl.
I just want to highlight a couple of sections of the file. first, the removable devices:
cdrom_type = var.vm_cdrom_type iso_paths = ["${var.os_iso_path}", "${var.vmtools_iso_path}"] floppy_files = ["autounattend.xml", "scripts/install-vm-tools.ps1", "scripts/disable-network-discovery.cmd", "scripts/disable-server-manager.ps1", "scripts/enable-rdp.cmd", "scripts/enable-winrm.ps1", "scripts/set-temp.ps1"]
Packer will mount two ISO files, using the sources defined in two variables of the other file:
-
os_iso_path is the Windows Server 2019 Installation disk
-
vmtools_iso_path is the vmtools ISO disk, grabbed from vSphere itself and placed here for easier usage
This setup will build a vm with two cd-rom drives, D and E respectively, and use the content of those disks to install the Operating System first and the VMware tools later.
There will also be a floppy disk. I know it sounds like a stone age thing, but it’s the best solution to pass all the installation files I want to use for the customization of the Windows Server once it’s installed.
autounattend.xml is the classic answer file for an automatic installation of Windows. Inside it, I defined the language I want to use, which version of Windows Server I want (Standard or Datacenter), timezone, and I instruct the unattended procedure to execute the other files in the “scripts” subfolder. The name of these files are pretty self-explanatory:
-
install-vm-tools.ps1
-
disable-network-discovery.cmd
-
disable-server-manager.ps1
-
enable-rdp.cmd
-
enable-winrm.ps1
-
set-temp.ps1
You can obviously pass other scripts to the installation process, and possibly automate even more tasks. The two important ones for me are the installation of VMware tools and the enablement of WinRM, which will be then used by Ansible.
Build the template
Once the files are ready, I start the build process:
packer build -force -on-error=ask -var-file win2019.pkrvars.hcl -var-file vsphere.pkrvars.hcl win2019.pkr.hcl
As in my previous post, it takes around 16-17 minutes in my lab to build the template, that I can then use to create as many virtual machines as I want. This will be the topic of my next post.