A VMware VM template -- also known as a golden image -- is a perfect copy of a VM from which you can deploy identical VMs. Templates include a VM's virtual disks and settings, and they can not only save users time but help them avoid errors when configuring new Windows and Linux VMs.
VM templates enable VMware admins to create exact copies of VMs for cloning, converting and deploying. They can be used to simplify configuration and ensure the standardization of VMs throughout your entire ecosystem. Templates can also be used as long-term backups of VMs. However, you can't operate a VM template without converting it back to a standard VM.
VSphere templates can be accessed through your content library. The content library wizard will then walk you through configuration steps, such as publishing and optimizing templates. It designates roles and privileges that you can then assign to users, and it eases VM deployment options.
Best practices for Hyper-V templates
You can create and deploy VMware VM templates through Hyper-V, as well. Hyper-V templates enable users to deploy VMs quickly with greater security, such as with shielded VMs, and reduce network congestion. They rely on System Center Virtual Machine Manager (SCVMM) and require specific configurations.
To create a Hyper-V template, select a base object from which you want to create the template -- an extant VM template, a virtual hard disk or a VM. Assign a name to the new template and configure the virtual hardware and operating settings the deployed VM will use.
Keep in mind that not every VM is a viable template candidate. If your system partition is not the same as your Windows partition, you won't be able to use that VM as a template source.
To create a shielded VM -- one that protects against a compromised host -- run the Shielded Template Disk Creation Wizard. Specify your required settings in the wizard and click Generate to produce the template disk, then copy that disk to your template library. The disk should appear in your content library with a small shield icon, which signifies that it has shielded technology.
How to create a VMware VM template with Packer
Packer is a free tool that can help you automate vSphere template creation and management. It features multiple builders optimized for VMware Fusion, Workstation Pro or Workstation Player. The vmware-iso Packer plugin builder supports using a remote ESXi server to build a template, and the vsphere-iso plugin helps you connect to a vCenter environment and build on any host in a cluster.
When you use Packer to make a VM template, you use two main file types. The JSON file makes up the template, and the autounattend.xml file automates Windows installation on your VM. Once your scripts, JSON file and autounattend file are ready, you can build a VM template in Packer. When the build is complete, Packer converts the VM to a template that you can view and deploy through PowerCLI.
Use PowerCLI to deploy a template
You can use PowerCLI to deploy new VMs from a template. Create an OS customization specification through PowerCLI to start the deployment process and to ensure that when you create your VMs from a template, you can still change certain settings to make them unique. These settings would include organization name, security identifier, local administrator password, Active Directory domain, time zone, domain credentials, Windows product key and AutoLogonCount registry key. The PowerCLI cmdlet might resemble the following:
C:> New-OSCustomizationSpec -Name 'WindowsServer2016' -FullName 'TestName' -OrgName 'MyCompany' -OSType Windows -ChangeSid -AdminPassword (Read-Host -AsSecureString) -Domain 'NTDOMAIN' -TimeZone 035 -DomainCredentials (Get-Credential) -ProductKey '5555-7777-3333-2222' -AutoLogonCount 1
After your OS is customized, you can easily deploy a VM from a template or multiple VMs from the same template. Start by placing the OS customization specifications into the variable $Specs.
$Specs = Get-OSCustomizationSpec -Name 'WindowsServer2016'
Then, use the VM template in the variable $Template.
$Template = Get-Template -Name ' Windows2016Template'
Finish by deploying your VM using the New-VM cmdlet and piping in your template and OS specifications.
New-VM -Name 'Windows16VM' -Template $Template -OSCustomizationSpec $Spec -VMHost ‘ESXiHost' -Datastore 'VMDatastore’
Troubleshoot VM templates
There are a few common mistakes to VM template creation and deployment that you'll want to avoid.
Creating a VMware template directly from a VM ends up destroying the VM. Always create a clone of a VM prior to creating a template from one. Even if you create a VM solely to become a template, template creation could fail and destroy your VM. A common reason for template creation failure is trying to create a template from a Linux VM. In that case, the template creation process wants to Sysprep a VM but Sysprep is designed for Windows OSes.
You also need to ensure that the model VM you want to turn into a template isn't domain-jointed. Joining a VM to an Active Directory domain can cause the system to create a computer account for the VM, which then leaves that computer account orphaned during the template creation process. To work around this issue, have the template itself handle the domain join and secure the library share in a way that prevents anyone other than VM admins from having access.
Finally, don't include any preinstalled applications on a VM template. The Sysprep process often breaks such applications. You can instead use an application profile or configure a VM template to run a script for automated application installation.