Last week I published the steps required to install Cloudbase-Init inside a vanilla vSphere Windows Server template to use with vRealize Automation. At the end of the post I mentioned that the installation can be automated as part of a Packer build. In this post I explain the steps that I have put in place to do just that.

As a refresher on Packer, if you need it, it is a software tool from HashiCorp that I use to build up-to-date templates for use in my homelab. It can use installation media for any guest operating systems and produce a working virtual machine template on demand and saves you either constantly patching an existing template or having to spend much time creating them periodically. If you really want to, you can trigger such builds to occur on a schedule or in response to an event, but that's beyond the scope of this post.

After performing a basic OS install, you can execute numerous tasks on the VM to customise it further, for example configuration, patching and software installations. These tasks are referred to as "provisioners" by Packer. For Windows builds, it is possible to use WinRM to execute one or more PowerShell scripts on your VM before it is complete. This is how / when you can install and configure Cloudbase-Init.

If you recall from my previous post, the steps that I went through to install Cloudbase-Init manually were as follows:

  1. Download the software.
  2. Install the software.
  3. Configure the Windows service for Cloudbase-Init.
  4. Remove extraneous config files.
  5. Customise the cloudbase-init.conf file.
  6. Remove the installation file.

If we are to automate the installation of Cloudbase-Init, then we just need to reproduce these steps without the need for any user interaction. The following PowerShell commands accomplish exactly that:

$msiLocation = 'https://cloudbase.it/downloads'
$msiFileName = 'CloudbaseInitSetup_Stable_x64.msi'
Invoke-WebRequest -Uri ($msiLocation + '/' + $msiFileName) -OutFile C:\$msiFileName
Unblock-File -Path C:\$msiFileName
Start-Process msiexec.exe -ArgumentList "/i C:\$msiFileName /qn /norestart RUN_SERVICE_AS_LOCAL_SYSTEM=1" -Wait
$confFile = 'cloudbase-init.conf'
$confPath = "C:\Program Files\Cloudbase Solutions\Cloudbase-Init\conf\"
$confContent = @"
[DEFAULT]
bsdtar_path=C:\Program Files\Cloudbase Solutions\Cloudbase-Init\bin\bsdtar.exe
mtools_path=C:\Program Files\Cloudbase Solutions\Cloudbase-Init\bin\
verbose=true
debug=true
logdir=C:\Program Files\Cloudbase Solutions\Cloudbase-Init\log\
logfile=cloudbase-init.log
default_log_levels=comtypes=INFO,suds=INFO,iso8601=WARN,requests=WARN
local_scripts_path=C:\Program Files\Cloudbase Solutions\Cloudbase-Init\LocalScripts\
metadata_services=cloudbaseinit.metadata.services.ovfservice.OvfService
plugins=cloudbaseinit.plugins.common.userdata.UserDataPlugin
"@
New-Item -Path $confPath -Name $confFile -ItemType File -Force -Value $confContent | Out-Null
Start-Process sc.exe -ArgumentList "config cloudbase-init start= delayed-auto" -wait | Out-Null
Remove-Item -Path ($confPath + "cloudbase-init-unattend.conf") -Confirm:$false 
Remove-Item -Path ($confPath + "Unattend.xml") -Confirm:$false 
Remove-Item C:\$msiFileName -Confirm:$false

Enjoy your Packer builds and using Cloudbase-Init with vRealize Automation!