Inthis series of posts I'd like to show how to automate the process of setting up virtual infrastructure consisting of several Windows Server 2016 machines. Most articles I've come across cover the use of cloud providers (e.g. AWS) as a virtualization platform, so I decided to make a write up about my experience with VMware vSphere.
Packer uses builder plugins to actually build images. There are two builders for VMware available out of the box: vmware-iso and vmware-vmx. The latter uses existing VMs to create images, so it doesn't fit in my concept of building everything from scratch.vmware-iso however starts from ISO file and creates brand new VM. But to use vmware-iso builder remotely on VMware vSphere hypervisor you need to modify your ESXi host to allow SSH access, because vmware-iso uses SSH instead of API to talk to VMware hypervisor. Looks pretty much like a dirty hack to me, not to mention that I don't have enough privileges to make such modifications to ESXi hosts in my environment.
Fortunately there is a third-party builder by JetBrains called vsphere-iso which does pretty much the same as vmware-iso but using vCenter API instead of SSH (they also have a vsphere-clone builder as a vmware-vmx alternative).
First you need to install Packer on your workstation. I use Ubuntu, but the installation process is fairly similar on all supported OSes. There are no packages of Packer for Ubuntu, but it can be installed as a precompiled binary very easily.
This tells packer to do the following:1. Connect to vSphere and create virtual machine2. Mount ISO images specified in json file3. Create floppy disk and put files from setup dir on it4. Mount floppy disk5. Power on VM and wait for it to get an IP address * When VM is powered on it boots from the first ISO which is Windows installation disk * Windows setup reads autounattend.xml from floppy drive * autounattend.xml runs vmtools.cmd batch script during the "specialize" pass * autounattend.xml runs setup.ps1 Powershell script after the first autologon * autounattend.xml sets local administrator user and password which are later used by packer to connect to Windows via WinRM6. Connect to Windows via WinRM7. Run provisioning script (in my case it just lists files on C: drive)8. Shut down VM and convert it to VM template
I try to use variables for everything and put actual values in a separate file. This file is just an example and you should rename it to vars.json and change all the values according to your environment.
This is the main configuration file for packer. The first section just declares all the variables and marks vsphere_password and winadmin_password as sensitive so that their values are not echoed during the build run.
Next section called builders tells vpshere-iso how to connect to vSphere and where to put the VM template.
Here you point to Windows ISO file and VMware tools ISO which will be presented to guest OS as CDROM drives. Guest OS will mount this ISOs exactly in that order (Windows ISO as "D:", vmtools as "E:"). This is important because other scripts refer to particular Windows drive letters.
This is the second most important file called answer file which allows to fully automate Windows installation. Windows setup reads this file either from installation media (ISO) or floppy drive automatically.
You can create this file by yourself or use one from my repository. If you choose to make your own answer file I recommend to read this article by Derek Seaman. Although it is focused on Windows Server 2012 it works for Windows 2016 too, except for this two details:
Now you have very basic but working configuration that lets you fully automate Windows Server 2016 build. There are a lot of things you can add and improve here. To name a few, you can automate Windows updates installation, pre-install software using Chocolatey, enable remote desktop and so on. I provided a couple of resources below which can serve as a good source of inspiration.
3a8082e126