This is not an all-new automation, instead it builds on what I've used before but this time adding in support for deploying preconfigured VMs using VMware's OVF tool an evolution of the bash scripts I use to control the process so they're more modular and can be resused by different deployments. 

In the Beginning The Wheel Turns

I've had a Geeklab / House Server for many years, starting with a second hand laptop running Windows XP that sat in the lounge of a shared apartment in Boston through to its current incarnation of a Dell T440 Server running mostly Linux VMs under the free VMware ESXi hypervisor which sits in a purpose built enclosure (made from the remains of an Ikea wardrobe if you must know) under the stairs of our home.

And now the wheel of change turns once more. Most of the VMs are Ubuntu Server 20.04 LTS which at the time of writing has just over 6 months before support finishes. I skipped the upgrade to Ubuntu 22.04 LTS since Canonical broke Bacula and while I could have worked around that quite honestly I didn't feel the need as everything just worked. But now that Ubuntu Server 24.04 LTS has been released and I've upgraded my Bacula Pi server to use it I really do need to upgrade the remaining servers.

Most of the later VMs were built using an automation process based on bash scripts and Ansible playbooks which took the build time for a new server from a good 3 nights work doing entirely by hand down to about half an hour and I've documented one application of the process in Nextcloud Deployment Automation if you're interested.

But I was still doing the initial Ubuntu installation by hand which gave a degree of freedom but also make the automation more complicated as it had to handle the deployment of the additional applications as well as their entire configuration. The deployment steps could also be quite slow with repeated calls to apt to install packages and there was always the risk that something would change with any of the apps and the first I'd know is when my deployment scripts stopped working.

For the rebuild I decided a different approach was needed. If I could automate the ESXi definition of VMs that would save some time and tools like Terraform do that extremely well. But one of the drawbacks of the free VMware licence (apart from Broadcom have stopped issuing them) is the API's are locked as read-only so you cannot use automation to do anything to it. Even the unofficial Terraform ESXi provider needs a vCenter to work with and that's certainly not free.

However VMware provide OVFtool for free and that allows you to import and export VMs as OVF Templates using the command line. This means that I can not only create new VMs I can also include a preconfigured OS and then run Ansible playbooks to do any additional customisations I require. I've read it's also possible to make some adjustments to an OVF template before uploading which opens up another level of possibilities.

And so a plan was formed...

Deployer Configuration

There is lots of very clever things you can do when it comes to deployment of infrastructure and applications. But since my needs are quite simple, I'm just one person and will mostly be deploying a single VM at a time I really don't need to go over the top. It's sufficient to have a builder VM that I can either log into via a desktop or connect via SSH and then run bash scripts to control the process. I'm currently using a Mint 22 VM running on VMware Player on another Mint desktop. The VM has VScode installed which is a nice git-aware editor and of course an ansible runtime. 

I need a method  that will be flexible so as my needs, understanding and ambition changes I can adapt as required. If you're looking to orchestrate fleets of VMs or have lots of different builds being launched by different teams or people at the same time this approach isn't going to scale well, but then you're hardly going to be doing that against a Geeklab are you?

My original approach for the deployer has served well so I don't see any need for major change, just extended to include the new tooling.

Directory structure for deployer, text gives full details

The ansible directory is the same layout as before:

  • control - keys and variables that I do not want included in any git repo and other deployer-specific parameters that will be referenced by playbooks
  • inventory - directories for live and dev inventories, which are two branches of the same git repo
  • playbooks - a directory for each deployable VM instance, or any ansible task I guess

The deploy directory contains links to the launcher script in each playbook just so I don't need to navigate down 4 levels to do a deployment.

The ovftool directory holds an installation of the VMware OVFtool that will be called by the playbooks (and maybe other tools or scripts)

VMtemplates contains versioned OVF templates which will be imported by the playbooks.

I suppose I should have a dev and a live deployer setup so I can still deploy an instance of a VM even if I'm in the middle of updating it. While that has happened a few times I've found it sufficient to have two directories under playbooks for each deployment, one containing the main branch (e.g. playbooks/u2404base in the diagram) of the git repo, and another containing the dev branch (e.g. playbooks/u2404base-dev).

 

Deployment Process

Ideally I just want to say "hey deployer, build me a XXX server" and off it goes. And while talking to your computer is now relatively common there a lot of detail that has to be specified somewhere and I don't want to be explaining this in a 10 minute conversation putting this into script input parameters each time. Ansible has the very flexible inventory structure and since ansible is the main tool I'm using I might as well use the inventory to define each VM and what needs to be done to it.

Human Launch script Deploy scripts Playbooks Comment
Update inventory        
Execute launch script source script config      
source ansible config      
call deploy script for OS Lock deployment   Wait until we can get a deployment lock
Call playbook - deploy Call ovftool to import VM from VMtemplates library Executes on ansible controller
call playbook - build Rename and reboot VM  
Unlock deployment   Release lock for next deployment to proceed
call playbook - build Base config and reboot  
call playbook - build Domain join or create local ansible user  
call playbook - admin Configure optional utils e.g. backup  
call playbook - admin Delete build user and clean up  
call deploy script for app call app-specific playbook 1 Apply app-specific config  
call app-specific playbook X Apply app-specific config  
Start using new application        

 

Human

Most of the parameters to create a new VM are already defined in the inventory at group level. I added parameters for the ESXi connection to the base all: group for:

  • ESXi host
  • ESXi username for ansible
  • ESXi password (vaulted of course)
  • ESXi datastore for deployment

Having just one ESXi host and with all VMs going to a single datastore this makes it really easy for me. The only parameter I need to specify on a per-VM level is the path to the template to use relative to the Template base path which is set per deployer.

This means defining a new VM can be as simple as simply adding an entry such as below

...

hosts:

  dvu-u2404test1.dev.example.com:

    esx_ovf_source: u2404-base101/X_U2404-base-1.ovf

...

While the esx_ovf_source can be set at a group level (even all:) as a default and then overridden as needed there's a lot to be said for specifying it for each VM as this will provide a record of what it was built with.

After that it just remains to cd into the appropriate playbook directory and running the launch script. 

Launch Script

The launch script orchestrates the entire build process for a given VM type, calling deployers in the required sequence.

The launch script has two includes:

  • Script config specifies script-specific items such as the location parameters for connecting to ESXi, the location of build and admin keys in the control directory and the hostname of a newly deployed template. Application-specific launchers also need to include the location of any precursor deploy scripts such as for the base OS.
  • Ansible config specifies general ansible items such as the location of the inventory and vault password file and on any given deployer the expectation is these will mostly be the same.

Both of these export bash environment variables and this allows me to separate configuration parameters which may be deployer dependent from the script itself.

The launch script will first call the OS deployer - which may be in another directory - and wait for it to complete. It may then call an application deployer to build / configure the application on the VM that has just been deployed.

These are currently bash scripts as we're not doing anything too clever at this layer.

Deploy Scripts

The deploy script is called by a launch script which provides it with all the required input to execute the playbooks.

The Ubuntu deploy script, playbooks and OVF templates have dependencies on each to make the process run more smoothly and much faster than before; the playbooks are not expected to run against any other VM which lets me take a few shortcuts, but also means I need to make some additional customisations.

The deployer script calls the playbooks and any other actions for each stage of the deployment process.

For the Ubuntu deployer it starts using a predefined ansible user which is baked into the OVF template, and its first task is to rename the VM at OS level since the OVFtool cannot do this for us. It then reboots the VM to complete the rest of the OS configuration, creates a users

Ansible Playbooks

Nearly all actions on the VM are performed using Ansible playbooks, and most of these have required very little, if any, modification to work with Ubuntu 24.04, which was a pleasant surprise. Mostly it was a case of removing roles that were no longer required as either the issue was fixed or I applied the fix when creating the VM for the template.

For the Ubuntu deployer the breakdown of plays and playbooks is pretty arbitrary. I didn't want to have a reboot occuring mid-playbook so that introduced some natural boundaries. I also wanted to keep a simple numeric naming structure on the .yaml files with them split into 3 groups

  1. Deployment of the OVF template and renaming it
  2. Initial configuration up to the point it can join the domain
  3. Configuration of any other general components e.g. backup, monitoring,

For application deployers they will follow their own logic, but the same principles will apply with playbooks ordered into groups or phases for the particular application.

I will walk through the details of the scripts and playbooks in part 3 of this series.

Modularity & Reuse

Ansible is really good for reusing components with its roles concept, especially as you can import roles into a playbook via git if you desire. So even though I have a separate set of playbooks for each VM type I deploy I can still have common components between them all at an Ansible level.

I can also take this one stage futher with the split between the launcher and deployer scripts. As hinted above, a launcher script can call any deployer script it it likes - as long as it provides the required variables of course. For example:

  1. The Nextcloud launch script in playbooks/u2404nextcloud can call the OS deployer script in playbooks/u2404base to deploy the VM using a Nextcloud specific OVF template - this is same as the u2404base but with additional disks defined
  2. The Nextcloud launch script then calls the Nextcloud deployer in the playbooks/u2404nextcloud directory to continue the process and trigger the initial Nextcloud configuration.

I don't have to duplicate all the Ubuntu playbooks into the Nextcloud deployment and worry about keeping them in sync with any changes to the Ubuntu deployment. And the same for any other applications too.

There is also a degree of separation between the individual deployments and the ansible control node itself. By placing configuration files in the control directory I can use the same scripts from my desktop VM on a central admin server or maybe even a container of some kind if I ever got the urge to play with that. It also means that credentials and keys are separate from the deployment scripts so there's no risk of them being accidentally uploaded to git repos that maybe one day I'll get around to publishing.