
How to use Packer v1.6 to build a Windows Server template for VMware vSphere
Note: the code in this is story is compatible with Packer v1.6.x. For Packer v1.5.x and previous versions please take a look at this story.
Packer is an open-source tool used to create virtual machine templates from a .json file.
To automatize the creation of templates in VMware vSphere, there are two major approaches:
- Use the VMware-iso provider. This provider creates VMware VMs from an ISO file as a source. It currently supports building virtual machines on hosts running VMware Fusion for OS X, VMware Workstation for Linux & Windows, VMware Player on Linux, and the free VMware ESXi Hypervisor.
- Use the vSphere-iso provider. This provider originally created by JetBrains and now is merged into the official Packer repository and released with Packer. It builds VMs on VMware vSphere directly using vSphere API.
In this story, we will use the vsphere-iso provider to create a Windows Server 2019 using Packer for VMware vSphere with vCenter.
Creating Linux templates with Packer is an almost a straight forward process, however, Windows Server templates will require some extra work. If you want to learn more about the process of building Linux Templates with Packer, check the following posts.
- Ubuntu 18.04 Templates for vSphere with Packer → How to use Packer to build an Ubuntu 18.04 template for VMware vSphere
- CentOS/RedHat Templates for vSphere with Packer → How to use Packer to build a CentOS template for VMware vSphere
- Debian 10 Templates for vSphere with Packer → How to use Packer to Build a Debian 10 Template for VMware vSphere
Requirements:
- Packer → https://www.packer.io
- Windows System Image Manager (SIM) feature from the Windows Assessment and Deployment Kit (ADK) to create or modify the autounattend.xml file → https://go.microsoft.com/fwlink/?linkid=873065
- VMware vSphere with VMware vCenter: the code was tested on vSphere 6.7 and vSphere v7.x
Packer Template for Windows Server 2019
There are three files required to deploy a Windows Server in VMware using Packer:
- the credentials.json file (the credentials file)
- the windows.json file (the Packer template)
- the autounattend.xml file (the answer file for the unattended Windows setup)
The credential.json file for Windows Server 2019:
This file defines variables to connect to VMware vSphere and the virtual machine:
{
"vsphere-server": "kopi-vsphere-01",
"vsphere-user": "[email protected]",
"vsphere-password": "Ins3cur3P@ssw0rd",
"vsphere-datacenter": "KopiCloud",
"vsphere-cluster": "Kopi-Cluster",
"vsphere-network": "VM Network",
"vsphere-datastore": "Kopi-Datastore",
"vsphere-folder": "Templates",
"vm-name": "Win2019-Template-Base",
"vm-cpu-num": "2",
"vm-mem-size": "4096",
"os-disk-size": "40960",
"disk-thin-provision": "true",
"winadmin-password": "S3cr3t0!",
"os_iso_path": "[Play-Datastore-02] ISO-Windows/Windows_2019.ISO"
}The windows.json file for Windows Server 2019:
This file creates the VMware virtual machine. We are going to break the files in several pieces, for better understanding.
Builders section: Establish the connection with the VMware vSphere and define the virtual machine:
"builders": [
{
"type": "vsphere-iso",
"vcenter_server": "{{user `vsphere-server`}}",
"username": "{{user `vsphere-user`}}",
"password": "{{user `vsphere-password`}}",
"insecure_connection": "true",
"datacenter": "{{user `vsphere-datacenter`}}",
"cluster": "{{user `vsphere-cluster`}}",
"network": "{{user `vsphere-network`}}",
"datastore": "{{user `vsphere-datastore`}}",
"folder": "{{user `vsphere-folder`}}",Builders section: The communicator, uses winrm in Windows (or ssh in Linux) to communicate with the VMware virtual machine:
"communicator": "winrm",
"winrm_username": "Administrator",
"winrm_password": "{{user `winadmin-password`}}",Builders section: virtual machine settings. The convert_to_template is used to convert the VMware Virtual Machine to VMware Template.
"convert_to_template": "true",
"vm_name": "{{user `vm-name`}}",
"guest_os_type": "windows9Server64Guest",
"CPUs": "{{user `vm-cpu-num`}}",
"RAM": "{{user `vm-mem-size`}}",
"RAM_reserve_all": true,Builders section: disk configuration for a single OS drive:
"storage": [
{
"disk_size": "{{user `os-disk-size`}}",
"disk_thin_provisioned": "{{user `disk-thin-provision`}}"
}
],
"disk_controller_type": "lsilogic-sas",Optional: Builders section: disk configuration for multiple drives. In this example, we have 4 disks for a SQL Server: OS disk, SQL Data disk, SQL Log disk, and SQL Temp disk.
"storage": [
{
"disk_size": "{{user `os-disk-size`}}",
"disk_thin_provisioned": "{{user `disk-thin-provision`}}"
},
{
"disk_size": "{{user `sql-data-disk-size`}}",
"disk_thin_provisioned": "{{user `disk-thin-provision`}}"
},
{
"disk_size": "{{user `sql-log-disk-size`}}",
"disk_thin_provisioned": "{{user `disk-thin-provision`}}"
},
{
"disk_size": "{{user `temp-db-disk-size`}}",
"disk_thin_provisioned": "{{user `disk-thin-provision`}}"
}
],
"disk_controller_type": "lsilogic-sas",Builders section: the final part of the Builders section is used to configure ISO images and floppy disks.
Here, we will define ISOs images: the Windows iso file specified in the variables section and the VMware Tools file iso, located in the vmimages folder.
Also, we will create a floppy and copy there the autounattend.xml file and a few extra scripts used to configure the virtual machine.
"iso_paths": [
"{{user `os_iso_path`}}",
"[] /vmimages/tools-isoimages/windows.iso"
],
"floppy_files": [
"autounattend.xml",
"../scripts/disable-network-discovery.cmd",
"../scripts/enable-rdp.cmd",
"../scripts/enable-winrm.ps1",
"../scripts/install-vm-tools.cmd",
"../scripts/set-temp.ps1"
]Provisioners section: the windows-shell provisioner runs commands on a Windows machine using cmd. It assumes it is running over WinRM. In this case, we execute the ipconfig command to confirm the machine is running and received an IP address
"provisioners": [
{
"type": "windows-shell",
"inline": ["ipconfig"]
}
]How to use:
From the command line, execute:
packer build -var-file=credentias.json windows.jsonThe complete code to build a Windows Server 2019 template is here → https://github.com/guillermo-musumeci/packer-vsphere-iso-windows-v2
And that’s all folks. If you liked this story, please show your support by 👏 this story. Thank you for reading!
