Automation and DevOps Banner

automate VMware vsphere template with Packer

In this blog article I want to show you how you can automate VMware vsphere templates with Packer. I like it because packer can be used on other platforms like Hyper-V, Azure, AWS or soagar for vmware Workstation or Virtual Box. With this post I want to show you, how to create Windows Server 2016 and Windows Server 2019 templates for VMware vsphere automated.
Today I start with creating and deploying on vmware vsphere. Since I want to provide a template automatically as easy and fast as possible, the prerequisite in this case is that this can be done directly from the ISO.
I use these templates for various test environments, but also for the daily work in the data center or at the customers. You can also use the templates as base for your Citrix Virtual Apps and Desktop or VMware Horizon environment. If you want, Take also a look on my other blog posts.

Why should a template be created automatically at all?

The reason is a not very simple one. In many environments templates are created once and then extended again and again. However, this leads to the fact that the templates accumulate more and more garbage and become larger. It therefore makes sense to create templates again and again.
In addition, I want to offer identical templates for different languages in my environment. For different operating systems, for example for Microsoft Windows Server, the settings within the templates should be the same for all versions and languages.

To be honest, I’m a big friend of Evergreen environments, because otherwise you can never really understand what exactly has been customized on the systems.

-Automation is the admins friend!

Why do I use packer to create the templates?

In my opinion, packer is very easy to learn and it is compatible with many platforms. Actually, I ended up with Packer, which was the only tool to create templates for VMware Workstation. My LAB environment at home runs on VMware Workstation and Microsoft Azure. At least I have not found another one. If you need more information about Packer, visit the website https://www.packer.io/ . There you can also get a lot of syntax examples.

What do I need to get started?

To start you only need packers. The whole thing can be set up a bit easier by using the finished Chocolatey packages.
To get more information about Chocolatey: https://chocolatey.org/

First I install Chocolatey. The whole thing I do via Powershell in admin mode.

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

Now we have to restart the Powershell and start it again in admin mode. Then insert the following commands to install Packer and the required Packer plugins. Also install the Provisioning Plugin for Windows Updates, because we want to update the created template automatically.

#install Packer Version 1.5.5

choco install packer --version=1.5.5

#install Packer Windows Update Provisioner

choco install Packer-Provisioner-Windows-Update

If you want to test if Packer is correctly installed on your system, you can use the following Command in Powershell

packer version

You should now see the installed version of Packer. In our case it is Packer Version 1.5.5.

With the installation of Packer we have now already laid the foundation you can automate VMware vsphere template with Packer. Now we can go on! Now we want to start to create our automated VMware vsphere templates with Packer.

Starting to automate our first VMware vsphere template with Packer

If you want, you can just clone my repository from Github. Here I have made ready-made templates available.

https://github.com/thomaspreischl/vsphere_packer_templates

In this repository you can find template deployment files for Windows Server 2019 and also for Windows Server 2016. If you have cloned the repository to your PC we can go on to prepare the scripts as you need.

Preparing the script files for you environment

Before we can start to automate VMware vsphere template with Packer, we have to prepare the scripts and files for you environment. First of all it is important that you download the desired template for the operating system you want to deploy later. In my case this is Microsoft Windows Server 2019 in German. You can download the images from your MSDN Account or from the Microsoft Evaluation Center: https://www.microsoft.com/evalcenter/.

After downloading your ISO file, you have to Upload the ISO to one of the Datastores of your VMWare vsphere environment. Please make sure that the vcenter account and the Host we are later using are able to access the ISO file.

Edit the variables.json file for your environment

If you have uploaded the iso, you can go on to the next step. Now we copy the variables.json file and name it to *mycopy*_variables.json. We can edit the variables file now as we want. Please Fill out the Login Accounts, Passwords and so on. I think it is self-expanatory. If not, please leave me a comment and I will help you.

{


    "vsphere-server": "YOUR vCenterIP",
        "vsphere-user": "administrator@vsphere.local",
        "vsphere-password": "YOUR_Password",
        
        "vsphere-datacenter": "YOUR_Dataceneter",
        "vsphere-cluster": "YOUR_Cluster",
        "vsphere-network": "YOUR_Network",
        "vsphere-datastore": "Your_Datastore",
        "vsphere-folder": "Your_vmfolder",
          
          "vm-name": "Win2019-Template-Base",
          "vm-cpu-num": "2",
          "vm-mem-size": "4096",
          "vm-disk-size": "40960",
          "winadmin-password": "Your WindowsPassword",
          "os_iso_path": "[Area12_Auto-Tier_LUN1] ISO/Windows Server 2019/SW_DVD9_Win_Server_STD_CORE_2019_64Bit_German_DC_STD_MLF_X21-96583.ISO",
          "winrm_username": "Administrator",
            
        

    }

When we have adjusted our variable file, we save it and now adjust our autounattend.xml This is located in the folder w2019.base. Here we may have to change the language of the operating system and also the login password for the Tempaltes operating system.

Update the passwords in autounattend.xml

Please enter your password, which you have stored under “winadmin-password”, at the following locations in the autounattend.xml:

<AutoLogon>
                <Password>
                    <Value>####YOUR_PASSWORD####</Value>
                    <PlainText>true</PlainText>
                </Password>
                <LogonCount>2</LogonCount>
                <Username>Administrator</Username>
                <Enabled>true</Enabled>
            </AutoLogon>

And also edit this section:

<UserAccounts>
                <AdministratorPassword>
                    <Value>####YOUR_PASSWORD####</Value>
                    <PlainText>true</PlainText>
                </AdministratorPassword>
            </UserAccounts>

Language Settings and time zone of our template

If you want to automate VMware vsphere template with Packer in different languages you can also copy the win2019.base folder and edit the language settings and the time zone as you want.

To edit the language plase update the following section of your autounattend.xml. You only need to replace the “de-DE” part with the language code you want to use, for example “en-US”.

A list with the language codes for Windows you can find here: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-lcid/

<component name="Microsoft-Windows-International-Core-WinPE" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <SetupUILanguage>
                <UILanguage>de-DE</UILanguage>
            </SetupUILanguage>
            <InputLocale>de-DE</InputLocale>
            <SystemLocale>de-DE</SystemLocale>
            <UILanguage>de-DE</UILanguage>
            <UserLocale>de-DE</UserLocale>
        </component>

To edit the Time Zone you have to update “Romance Standard Time” to your time zone. Update this section:

<component name="Microsoft-Windows-Shell-Setup" processorArchitecture="amd64" publicKeyToken="31bf3856ad364e35" language="neutral" versionScope="nonSxS" xmlns:wcm="http://schemas.microsoft.com/WMIConfig/2002/State" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <TimeZone>Romance Standard Time</TimeZone>
        </component>

Here you can get a list with all names of time zones for windows os: https://docs.microsoft.com/windows-hardware/manufacture/desktop/default-time-zones

After we have made all settings according to our requirements, we can try the whole thing out.

automate VMware vsphere template with Packer – Let´s take a flight!

All settings are done. We sit relaxed at our desk. The coffee is empty…

Now we start our first run and can relax and get a coffee 😉 I hope I’m not promising too much at this point 😀

To automate the vmware vsphere template with packer, we just have to change into our folder for the template and start our packer build. I will give you some tips for debugging and troubleshooting below.

automate VMware vsphere template with Packer - Running the job

Now we have to wait some time, the template will now be installed, updated and finished. Of course you can watch the whole process in the vCenter Console and enjoy your coffee while doing so.

automate VMware vsphere template with Packer - Template GUI

If the job is completed successfully, you will find the template in your vcenter in the appropriate folder.

automate VMware vsphere template with Packer - templates

Tips for troubleshooting

Here you can find a quick overview how to get more informations and troubleshoot your Packer jobs.

To start a job in debug mode you can use:

packer build xxx YOUR Variables, Files and so on xxx -debug

If you want to override artifacts you can use the parameter “-force”.

If you want to output the timestamp of the UI output as line prefix . You can use: “-timestamp-ui”

For more informations take a look on: https://www.packer.io/guides

Link to the Git Repo

Here you can find the Link to the repo one again:

https://github.com/thomaspreischl/vsphere_packer_templates

I hope you enjoy the post and get a new input for your IT Skills. If you wan´t, you can buy me a coffee or follow me on twitter or facebook.

Leave a Comment

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