Petya Petrova - Fotolia
Templates are nothing new to the world of IT, but combining them with PowerCLI enables you to deploy a VM from a template more easily and efficiently. Using a tool to automate machine creation from a template, such as Packer, also helps you maintain version control.
For many years, IT has used the idea of a golden image for deploying new desktops or servers. The basic premise is that you can both standardize and more quickly deploy them with a preconfigured image that already has an OS and software.
In vSphere, VM templates are considered golden images. You can quickly create and run a VM from either the web client or PowerCLI with fewer steps rather than starting from scratch via installation media.
Create a VM template with PowerCLI
The first step to creating a VM template in vSphere is to install and configure the OS. This step varies in length of time and difficulty depending on whether you use Windows or Linux, but, ultimately, this process is the same as setting up any new VM in vSphere. You want to get it to a state where you can customize and deploy the VM.
Once a VM is ready to become a template, use the New-Template cmdlet in PowerCLI. In the example below, assume you already created a new VM called Win2016VM, installed the Windows OS and configured the VM as a golden image. With the New-Template cmdlet, take that VM and create a new template called Windows2016Template using the location VMLocation on the datastore VMDatastore.
C:> New-Template -VM 'Win2016VM' -Name "Windows2016Template" -Datastore 'VMDatastore' -Location 'VMLocation'
Create a VM template with Packer
Packer, the open source image builder from HashiCorp, provides a more modern method of creating a vSphere template. It can create platform-independent machine images for AWS, Azure and VMware, as well as other on-premises and cloud providers. Another advantage of Packer is that the machine details of a VM's creation -- both the machine itself and processed template -- can be kept in code. For this reason, users need not change an existing VMware VM template in vCenter -- they can simply change the existing Packer template in code when a change is necessary and deploy the new VM from Packer, minimizing time and effort. An example configuration for the Packer post-processor looks like this:
This type of code is part of a Packer template. Such templates also include configuration for creating the machine image.
Deploy a VM template with PowerCLI
Once you create a VM template in vSphere, you can use it to deploy new VMs. Before you do, though, create an OS customization specification in PowerCLI. This task ensures that when a VM is created from a template, you can still change certain settings to make it unique. These settings include the organization name, security identifier, local administrator password for the template, Active Directory domain, time zone, domain credentials used to join AD, Windows product key and the AutoLogonCount registry key.
In PowerCLI, the cmdlet might look like this:
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
The Read-Host and Get-Credential cmdlets makes it so the user creating a VM from the template has to provide admin credentials.
After this, you can deploy a VM from the template and the OS customization specification. Even better, you can create VMs over and over with the same template, knowing each one has the same configuration.
To deploy a VM from the template, first place the OS customization specifications you created into the variable $Specs:
$Specs = Get-OSCustomizationSpec -Name 'WindowsServer2016'
Next, use the VM template you created in the variable called $Template:
$Template = Get-Template -Name ' Windows2016Template'
Finally, deploy your VM with the New-VM cmdlet using your template and OS specfications. Place the VM on the "VMHost-1" ESXi host and store the VM on the "TestDatastore" datastore:
New-VM -Name 'Windows16VM' -Template $Template -OSCustomizationSpec $Spec -VMHost ‘ESXiHost' -Datastore 'VMDatastore’