• About Altaro
  • About Altaro VM Backup
  • 101 Free VMware Tools
  • facebook
  • twitter
  • google+
  • linkedin
  • rss
  • About Altaro
  • About Altaro VM Backup
  • 101 Free VMware Tools
Automation and Management
Altaro.com » Altaro's VMware Hub and blog » Automation and Management » vSphere VM Templates – A Complete Guide – Part 2
Jason Fenech
by Jason Fenech in Automation and Management
Tags: customization, Linux, templates, vSphere, vSphere 6.5

vSphere VM Templates – A Complete Guide – Part 2

03 Apr 2017 by Jason Fenech
0 Automation and Management
 

 

 

 

In part 1, I covered the basics of vSphere VM templates and outlined the differences between templates and clones. I also described a standard process on how to create a generic template. Finally, I introduced guest OS customization (GOSC) specifically targeting Linux but stopped short from showing how GOSC can be used to automate virtual machine configuration when deploying from a template.

Today, I’ll start off by creating a Linux VM template and a GOSC. I’ll then deploy a VM based on the Linux template just created using GOSC to automate its configuration.

 

Preparing the Centos Linux template


I prepared a VM running Centos 7 Linux 64-bit as the guest OS. Before converting this to a template, I ran yum update to update the OS to the latest patch level. I also used yum to install Open VM Tools which is one method used to deploy VMware Tools on Linux VMs.

Unintentionally, I forgot to mention the Guest OS Version setting in part 1 of this series. When creating a VM, make sure that its value matches that of guest OS’ name and version you’re actually installing. As per Fig.1, I’ve set mine to Centos 7 (64-bit) to match the VM’s guest OS being installed.  It’s important that there are no mismatches as customization will generally fail if there are.

Figure 1 - Setting the correct Guest OS Version for a VM
Figure 1 – Setting the correct Guest OS Version for a VM

 

The next things to do is to change the hostname to something like Centos7-template and remove any references to any IP network settings. The latter is achieved by editing the ifcfg-xxx file for the primary nic. The ifcfg file is generally found under /etc/sysconfig/network-scripts. Both path and filename may vary depending on the Linux flavor. As it were, both of these steps are optional since these settings are overwritten when GOSC settings are applied. I put them in to show that GOSC settings are indeed being applied during provisioning.

Figure 2 - Displaying the hostname and IP settings in Centos
Figure 2 – Displaying the hostname and IP settings in Centos

 

Here’s another reminder to read through this post as it goes into some detail on how to prepare Linux templates.

Once you’re satisfied with the final configuration, run shutdown immediately followed by Convert to Template using the vSphere Web Client.

Figure 3 - Converting a VM to a template using the vSphere Web client
Figure 3 – Converting a VM to a template using the vSphere Web client

 

Provisioning a Centos VM from template


I’ve already covered how to create a GOSC for Linux in part 1, so I’ll limit myself to pasting screenshots of the settings used in the GOSC. I named the GOSC specification Centos7-Blog-Post, which in hindsight is somewhat of a daft name as it doesn’t really tell you anything! Anyways.

Figure 4 - Displaying the settings defined by the GOSC
Figure 4 – Displaying the settings defined by the GOSC

 

To deploy a new Centos VM from template, right-click on Centos7_Template (Fig. 5) and select New VM from This Template.

Figure 5 - Deploying a VM from template
Figure 5 – Deploying a VM from template

 

This launches the Deploy From Template wizard which takes you through a number of steps:

  • Set the VM name and location or folder.

 

  • Select a cluster and/or ESXi host where the VM will reside.

 

  • Choose the VM’s disk format, the VM storage policy and the datastore where the VM is created.

 

  • Ticking on the Customize the operating system option tells the wizard that a GOSC will be used. You must also tick the Power on virtual machine after creation option to fully automate the configuration process.

 

  • Next, select the GOSC that is applied to the VM being provisioned.

 

  • Finally, review the settings and hit Finish to kick off the provisioning process.

 

After provisioning completes, the VM will power up and is configured before finally rebooting. The reboot is required so that the GOSC settings applied take root. Under my nested environment, the whole process took roughly 3 minutes from start to finish. Not too shabby!

To verify that the settings have been applied, I logged in the freshly deployed VM and ran some basic commands followed by a ping test to ensure proper network connectivity. This is shown in Fig. 6.

Figure 6 - Verifying if the GOSC settings have been applied
Figure 6 – Verifying if the GOSC settings have been applied

 

Now, if I wanted to deploy a second, third or any other number of Centos VMs using different hostnames and IP addresses, I would simply go in and edit the existing GOSC from the Customization Specification Manage and repeat the deployment procedure just described. I could also employ PowerCLI to spice up things.

 

Using PowerCLI to modify an existing GOSC


These are a few PowerCLI commands, related to GOSC, you must know about.

  • Get-OSCustomizationSpec – Retrieve the settings defined in a GOSC.
  • Set-OSCustomizationSpec – Changes one or more settings defined in the GOSC.
  • Get-OSCustomizationNicMapping – Retrieves the network settings defined in a GOSC.
  • Set-OSCustomizationNicMapping – Changes parameters set for a network card.

This is all we need to update a GOSC using PowerCLI. In the examples that follow, I’m updating the Centos7-Blog-Post GOSC created earlier for this post (see Fig. 4).

The changes affected include the hostname and IP address to ensure we don’t end up with duplicate addresses and hostnames on the network. Here’s one way of doing it:

PowerShell
1
2
Connect-VIServer 192.168.16.50
Get-OSCustomizationSpec Centos7-blog-post | Set-OSCustomizationSpec -NamingPrefix "Centos7-Apache" -NamingScheme fixed

PowerShell
1
Get-OSCustomizationNicMapping -OSCustomizationSpec Centos7-Blog-Post | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress 192.168.16.53 -SubnetMask 255.255.240.0 -DefaultGateway 192.168.16.1

To verify that the GOSC has picked up the new settings, you could simply have a look at it in Customization Specification Manager (Fig. 7) or issue the following PowerCLI command:

PowerShell
1
(Get-OSCustomizationSpec centos7-blog-post).NamingPrefix; (Get-OSCustomizationNicMapping -OSCustomizationSpec centos7-blog-post).IPAddress

Figure 7 - Displaying the settings defined by a GOSC
Figure 7 – Displaying the settings defined by a GOSC

 

Using PowerCLI to deploy a VM from template and GOSC configure it


At this point, we have sufficient PowerCLI knowledge to allow us to deploy a VM from template and configure it with GOSC. The New-VM cmdlet is one more command you need to know about. The cmdlet, as implied by the name, will create a VM from scratch but can also be used to clone or deploy a VM from template. New-VM takes a number of mandatory parameters as seen in the script below. I’ve commented every script line to make it easier to follow.

The (Get-VMHost)[0] line, simply picks the first ESXi from a list of retrieved hosts since I’m not targeting a DRS enabled cluster.

PowerShell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#Set the VM name.
$VMName="Centos 7 Apache Server"
 
#The datastore on which the VM is created.
$DS=Get-Datastore -Name "iSCSI_LUNb"
 
#The location where the VM will be created. Here I'm targeting a VM folder I created.
$Location = Get-Folder "Centos7 Template Example"
 
#Retrieve a list of ESXi hosts and select the first one. No need for this when DRS is enabled.
$VMHost=(Get-VMHost)[0]
 
#Specify the VM template to use.
$CentosTemp = Get-Template -Name Centos7_Template
 
#Specify which GOSC is applied.
$CentosSpec = Get-OSCustomizationSpec -name Centos7-Blog-Post
 
#Create a new VM based on the set template and GOSC.
New-VM -Name $VMName -Template $CentosTemp -OSCustomizationSpec $CentosSpec -Location $Location -VMHost $VMHost -Datastore $DS
 
#Power up the VM to have GOSC settings applied.
Start-VM -VM $VMName

If you do have a DRS enabled cluster, amend as follows:

PowerShell
1
2
3
4
#Replace "cluster name" with the name of the cluster you're deploying to
$ResourcePool = Get-cluster -Name "cluster name" 
 
New-VM -Name $VMName -Template $CentosTemp -OSCustomizationSpec $CentosSpec -Datastore $DS -ResourcePool $ResourcePool -Location $Location

As per Figure 8, the above can be run as individual commands. Alternatively, save the whole lot and execute it as a PowerCLI (.ps1) file. The progress bar at the top of the PowerCLI window shows the New-VM cloning task at work.

The Start-VM cmdlet powers up the VM allowing GOSC customization to complete. As mentioned, the VM will reboot one more time after which it is ready for use. I carried out the same tests as before to verify that all went according to plan.

Figure 8 - A collated view of the PowerCLI commands executing, the provisioned VM as listed in Navigator and a quick look at the VM settings applied from console
Figure 8 – A collated view of the PowerCLI commands executing, the provisioned VM as listed in Navigator and a quick look at the VM settings applied from console

 

Conclusion


In today’s post, we’ve seen how to deploy a VM running a Linux guest OS from template and configure it automatically using GOSC. I only touched on Centos but the same applies pretty much to other Linux distributions with some minor difference related to the commands and syntax used, file names and paths and so on.

In the part 3, I’ll tackle Windows templates pretty much in the same vein.

Have any questions or feedback?

Leave a comment below!

Jason Fenech
Jason Fenech

An IT veteran for over 23 years, I covered various roles throughout my career. Prior to joining Altaro as a blog writer and QA tester, I was employed as an infrastructure engineer at a cloud services provider working exclusively with VMware products. The Altaro VMware blog enables me to share the experience and knowledge gained and, much to my surprise, is what got me the vExpert 2017 award. Besides being a techie and a science buff, I like to travel and play guitars. I also do some photography and love having a go at playing the occasional XBOX game, Halo being my absolute favourite. I am also a proud father of two and parent to a crazy Dachshund called Larry.

All Posts   WEBSITE   EMAIL

Click here to cancel reply.

Have a question or comment? We'd love to hear it! Cancel reply

Your email address will not be published. Required fields are marked *

XHTML: You can use these tags <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

 

Featured product

Download Altaro VM Backup

Download 30-day trial or Free Edition (free for 2 VMs, forever.)

Site categories

  • vSphere
  • Storage
  • Automation and Management
  • Altaro News
  • Desktop Virtualization
  • HyperConvergence
  • Cloud
  • Uncategorized

Altaro Software

  • About Altaro
  • Altaro VM Backup

Altaro VM Backup

  • Altaro VM Backup
  • Download Free Version
  • Download 30-day Trial

Our writers

  • Jason Fenech Jason Fenech
    142 Posts
  • Andy Syrewicze (Chief Editor) Andy Syrewicze (Chief Editor)
    24 Posts
  • Ryan Birk
    22 Posts
  • Luke Orellana Luke Orellana
    18 Posts

Copyright © 2018 Altaro Software.

  • facebook
  • twitter
  • google+
  • linkedin
  • rss
[contact-form-7 id="4731" title="Act-On subs"]