Adding the capability to automatically deploy OVF templates means some changes are needed to my Deployer to support the the new tooling, which turned into a fairly major overhaul and I ended up rebuilding on a new Mint 22 virtual machine as well.
This also needed updates to the Ansible inventory which in turn led to a reorganisation there too.
Importing an OVF is really quite easy and can be done from the ESXi Host Client, but I'm looking to add this to my deployment automation and restrictions on the free ESXi hypervisor block Ansible from doing this. But just as we used the VMware OVF Tool to export a virtual machine from a command line (nicely wrapped into a little bash script) at the end of Finalise and export template, we can also use the OVF Tool to import a virtual machine too. Which means we can also use an Ansible command task to do this as well.
ESXi Import User
Since export is pretty much an interactive process it makes sense to use my own user credentials to do so. But for import it's a different story. This will be scripted and I do not want to put my admin credentials into any scripts, even with the protections offered by Ansible Vault for storage.
In the ESXi Host Client go to Host > Manage > Security & Users > Roles
Click Add Role and give it a name and then assign the following permissions. All system privileges need to be assigned, but just a selection from each of Datastore, Network, Virtual Machine, Resource and VApp.
| Path | Privileges | |
![]() |
Root | System |
| → Datastore | AllocateSpace | |
| → Network | Assign | |
| → Virtual Machine | ||
| → → Inventory | Create, Register | |
| → → Interact | PowerOn, PowerOff | |
| → → Config | AddNewDisk, AddRemoveDevice, AdvancedConfig | |
| → Resource | AssignVMToPool | |
| → VApp | Import |
Then go to Host > Manage > Security & Users > Users and click Add User

Give a username and a suitably long and complex password and also enable shell access.
The final step is to assign our ansible role to our ansible user. For this go to Host > Actions > Permissions

Click onto our Ansible user and click Assign Role

Pick the Ansible role from the drop-down and click Assign Role then close. You'll be able to see a summary of the permissions the user now has too.
We now have a dedicated user for deploying new VMs which although isn't a full administrator I suspect it could still be used for some mischief if the credentials were to leak.
Deployer Preparation
As discussed in Approach you can make deployment automation incredibly powerful and complex with the option to setup and destroy entire system landscapes in minutes. Sometimes even accidentally. My needs are relatively modest and as I'm just one person and I'll mostly be deplying a single VM at a time. I'm also happy with running the whole process from a desktop on Deployer VM too.
I'm currently using Linux Mint 22 XFCE edition for the deployer, running inside a VMware Player virtual machine. The Deployer VM has 2 vCPU, 3GB of RAM and 32 GB disk of which 20 GB is in use - and a quarter of that is exported OVF templates.
In terms of tooling it needs:
- Ansible - from Mint repositories
- Git - also from Mint repositories
- Visual Studio Code - from MS download site and repos.
- OVFtool - download from VMware and unpack
- Network connection
- A mug of coffee - for me
All the tooling is freely available apart from the coffee which you need to buy and that is of course optional. You don't even need VScode as there other editors available, you could even use the default Xed editor that comes with Mint and use git from a command line.
I have another VM in the geeklab that acts as a simple git server using SSH and of course gets backed up (using my Bacula Pi project) but otherwise it's pretty self contained.
Building a Mint VM is beyond the scope of this article, but maybe I'll write that up at some point.
Directory structure
In my home directory I created a folder for Projects and the deployer directory structure sits inside that with automation as a top-level directory under which everything else is arranged.

Ansible
Installing ansible itself is just a case of going into the Software Manager and installing ansible and ansible-core, or using apt install ansible to achieve the same.
All my ansible-related file are located under the ansible directory for convenience.
The control directory contains SSH keys and credentials that I do not want checked into any repository along with deployer-specific ansible variables files that are referenced by playbooks but would not make sense to check into a repo as they'd be no use on another deployer host. I'll reference each file as needed as we build our deployer.
The inventory directory contains sub-directories for a dev and live version of the Ansible inventory. I keep the inventory in a git repository and the Live branch of that is in the live directory. Dev is currently local files only for any working on any inventory changes in a new playbook
If you already have an Inventory then copy / clone it it here, otherwise the previous Ansible Inventory article to get started - it's a little out of date now and I'll return to this later with some of the changes.
The playbooks directory is where all the playbooks for the deployments are kept, each deployment having it's own sub-directory.
Deploy
This is intended to hold scripts for complete deployments, but for the moment has links to the launch scripts since everything is based on ansible. At least is saves a bit of tying as ~/automation/deploy/u2404.sh is shorter than ~/automation/ansible/playbooks/u2404base/u2404.sh
OVFtool
The OVFtool can be downloaded from Broadcom's developer site (free, no registration needed either!) at https://developer.broadcom.com/tools/open-virtualization-format-ovf-tool/latest and just needs to be unzipped. It will be called by playbooks so this location needs to be known.
In the ansible/control directory create an Ansible variables file called esx-deployment.yaml with the following content:
--- # Ansible variables
# Variables to pass to the ansible playbooks handling ESXi deployment# These variables are deployer-dependent
ovf_tool_location: ~/Projects/automation/ovftool/ovftoolesx_ovf_base: /home/user/Projects/automation/VMtemplates
...
The ovf_tool_location can use the ~/ notation to mean home directory if you prefer and needs to point to the location of the ovftool launch script. The esx_ovf_base parameter must be the full path from root to the VMtemplate directory which we'll come to next. Using this variable file lets us avoid hard-coding the path to the OVF tool in our playbooks or inventory. The variable file itself will be specified by a relative path from the playbooks too e.g. ../../control/esx-deployment.yaml.
There is nothing else we need to do with the OVFtool apart from checking for updates from time to time.
VMtemplates
This directory holds each OVF template that could be deployed, each in its own sub-directory. The location must also be in the esx-deployment.yaml file (see above) so that playbooks can find it as - more on that when we get to the playbooks - and again lets us avoid hardcoding the path to the template into our playbooks.
I also have an export directory to be used when exporting VMs to OVF (see Finalise and export template). Once exported I move the files into the appropriate sub-directory.
The directory naming is based on the template with a version encoding on the end. So the first (successful) Ubuntu 2404 template VM is in u2404-base-100. I then updated it and re-exported so that went into u2404-base-101. A subsequent rebuild became u2404-base-200 (not shown), while an extension of the base for use with a Nextcloud deployment became u2404-nextcloud-100. There's lots of different ways you could organise this, I strongly suggest you work to a consistent naming standard as it'll make things a bit easier to manage over time.
Inventory Upgrade
As I've mentioned in other articles, the Ansible inventory is a key resource for information about the landscape and getting the design right for your needs can make a huge difference in how easy or difficult it is to use Ansible effectively.
I have a single GeekLab server which on average contains a single ESXi instance with 5 networks of between 3 and 8 VMs each depending on what I'm playing with at the time so although my inventory is quite small it still has to cope with many of the same challenges as a fair-sized organisation.
Most of the parameters I need to deploy a VM are related to the landscape and networks they'll be deployed into so can be set at that level. For example the VMware network name, the host to get the UPS status from and the shutdown parameters and the backup pool to use. Some parameters can even be defaulted for all networks such as the VMware datastore to deploy to and the Bacula director to use for backups.
I had to introduce new parameters specifically for the VMware deployments since this was the first time I'd used Ansible for this and these are all needed by OVFtool. These are set at three different levels in the inventory.
At the top level:
- VMware ESXi hostname
- VMware Username and password for VM deployments (vaulted)
- VMware Datastore for VMs
At the network level:
- VMware network name
At the VM level:
- Name of the OVF template to use
All these parameters can of course be overridden on a per-VM basis if needed just by setting values under the VM itself. I could also set the OVF template to use at the network or top level if I wished and then override it for specific VMs, although always setting this at the VM level means I also have a record of what got used for each VM at the time it was built given that templates are likely to evolve over the 5 years of an LTS release.
Even though the ESXi username and password are vaulted there's a good argument they should be held in a variables file in the control directory, but then we have the issue of the where the key to that is kept (preferably not in a file in the control directory too) so this may change. At least having it the inventory means an update take effect immediately which is why I moved them from the esx-deploy.yaml to the inventory.
My inventory has evolved from a single file to a directory containing several files and now group_vars and host_vars subdirectories too as I've had a chance to explore more. I use a numeric based naming structure to help make sense of it since Ansible processes files in name order.
The top level file is now called 0000-base.yaml and I've added four additional variables for the VMware hostname, credentials for Ansible to use plus the datastore.
all:# hosts:
vars: [...]
esx_host: geeklab.example.com esx_user: ansible esx_pass: "{{ vault_esx_pass }}" vault_esx_pass: !vault | $ANSIBLE_VAULT;1.1;AES256 [encrypted data here] esx_datastore: DS-VM
It should be obvious what each variable should be used for. All the VMware related variables start with esx_ in their name.
For each network I have a dedicated file too, for example the sandpit network is defined in 4000-sandpit.yaml and I've added the network variable esx_network here, the value must be the network name as defined in the ESXi Host Client you're deploying to.
################################################# # Sandpit1 Network#
sandpit: vars: [...] esx_network: Hall-Sandpit
For a temporary host while working on a new build of the template I created a 4900-templatecheck.yaml inventory file and added host svu-base2.sandpit.example.com and specified the template in the esx_ovf_source parameter
################################################# # Sandpit1 Network#
sandpit:
children:
# Dev servers template_check: vars: config-landscape_tier: "D"
hosts: svu-base2.sandpit.example.com: esx_ovf_source: u2404-base-2/X_U2404-base-2.ovf
The esx_ovf_source variable specifies the directory and the .ovf file for the template to be deployed and as we'll see later this is appended to the value of esx_ovf_base which is defined in the esx-deployment.yaml file located in the control directory. This would give the full path to the template to be used for host svu-base-2 as /home/user/Projects/automation/VMtemplates/u2404-base-2/X_U2404-base-2.ovf.
And of course I could have specified an alternative ESXi host or datastore just be setting different values of esx_host or esx_datastore for svu-base2 if I wanted that particular VM to be deployed somewhere else.
Inventory Example
So putting this all together into an example Inventory we now have a structure like this.
0000-base.yaml
This sets default and global parameters for all hosts in our inventory.
--- # Ansible Inventory# Global configuration# 24 Aug 2024
# Notes:# Group names must be globally unique or they will get merged. Parents do not distinguish# Check: ansible-inventory -y -i path-to-inventory --list
all:# hosts:
vars: # email for notifications to go here - central for all playbooks config_mailalert_host: mailer.example.com config_mailalert_port: 25 config_mailalert_secure: never config_mailalert_sender: config_mailalert_to:
bacula_director_host: selene.example.com bacula_director_name: selene-dir bacula_deploy: true
esx_host: geeklab.example.com esx_user: ansible esx_pass: "{{ vault_esx_pass }}" vault_esx_pass: !vault | $ANSIBLE_VAULT;1.1;AES256 [ encrypted data ] esx_datastore: DS-VM children: control: hosts: localhost: ansible_connection: local
...
0010-group-ad.yaml
This defines groups used for triggering joining a domain during the second stage of deployment
--- # Ansible Inventory# Global configuration - Top-Level groups# 4 Aug 2023
# Notes:# Group names must be globally unique or they will get merged. Parents do not distinguish# Check: ansible-inventory -y -i path-to-inventory --list
# Group members defined in other files
####################### ad_domain# Members of an AD domain used for triggering domain joiningad_domain: children: domain_test_example_com: domain_sandpit_example_com:
samba_dc: children: sambadc_test: sambadc_sandpit:
...
4000-sandpit.yaml
This is an example of a network, which is called Hall-Sandpit in VMware. For hosts on this network the UPS agent checks against 192.168.241 and will shut themselves down once battery level drops to 60% or if we've been on battery for more than 10 minutes. While the Bacula agent gets configured on the VM, the client config isn't automatically uploaded to the Bacula server.
Sandpit is also an Ansible group, which also contains groups sandpit_router, sandpit_linux, sambadc_sandpit and domain_sandpit_example_com. Group names must be unique, as I remind myself in each inventory file.
--- # Ansible Inventory# Sandpit Networks# 9 Sept 2024
# Depends on: 0000-base.yaml
# Notes:# Group names must be globally unique or they will get merged. Parents do not distinguish# Check: ansible-inventory -y -i path-to-inventory --list
################################################# # Sandpit1 Network#
sandpit: vars: config_landscape_tier: "S" esx_network: Hall-Sandpit
apcupsd_device: 192.168.241.1 apcupsd_batterylevel: 60 apcupsd_runtime: 10
# bacula_deploy: true bacula_server_clientconfig: false
network_ntp: 192.168.241.1
children:
sandpit_router: hosts: svr-gw.sandpit.example.com: apcupsd_device: "192.168.111.42" apcupsd_batterylevel: 40
sandpit_linux: hosts: svu-mail.sandpit.example.com: esx_ovf_source: U2404-base-200/X_U2404-base-2.ovf network_type: "static"
# Samba DC treated as non-ad machines for now sambadc_sandpit: vars: network_type: static bacula_server_clientconfig: true
hosts: svu-dc-1.sandpit.example.com: esx_ovf_source: U2404-base-100/X_U2404-base-1.ovf
# All sandpit domains and their members MUST be listed here domain_sandpit_example_com: # member of ad-domain, see 01-group-ad.yaml vars: ad_domain: "sandpit.example.com" ad_type: "ad2008"
hosts: file01.sandpit.example.com:
...
We've also got some other hosts defined here, which are all "permanent" sandpit hosts. A gateway that gets UPS status from 192.168.111.42 and will trigger its own shutdown once the battery level reaches 40%, a mail server using the U2404-base-100 template and a Samba DC using the U2404-base-100 template and that also had it's Bacula config activated.
4900-templatecheck.yaml
And just to show how we can add a new group as a child of the sandpit group and define an additional VM. This is useful when I just want to quickly deploy, test and destroy a VM without touching any other inventory files. At the end of the testing I can delete this inventory file.
--- # Ansible Inventory# Sandpit Networks - Template Checking# 10 Sept 2024
# Depends on: 0000-base.yaml, 4000-sandpit.yaml
# Notes:# Group names must be globally unique or they will get merged. Parents do not distinguish# Check: ansible-inventory -y -i path-to-inventory --list
################################################# # Sandpit1 Network#
sandpit:
children:
# Temporary servers for template development template_check: vars: config-landscape_tier: "D"
hosts: svu-base2.sandpit.example.com: esx_ovf_source: X_U2404-base-2/X_U2404-base-2.ovf
...
And yes, the group name template_check kinda breaks my naming convention and I had a little word with myself and promised not to do it again.
This numeric based naming convention gives me 9 sections to the inventory, numbered 1000 to 9000 which should be more than enough. If I need more I guess I switch to hex. The text part is to help me tell at glance what the file is for without having to worry about the the effect it has on the order the files are interpreted it.
For hosts with a lot of custom parameters we can use the host_vars directory and have either a file named as the fully-qualified name of a host in the inventory with all its variables in, or a directory named as the fully-qualified name of a host in the inventory and then individual files within that for each set of variables.
So for example I could have a directory under host_vars called svu-base2.sandpit.example.com which contains YAML files for variables controlling esx, apcupsd, ubuntu and bacula which could also be links. While this gives a huge amount of flexibility in how you can manage an Ansible Inventory I can't help feeling that "just because you can doesn't mean that you should" also applies.
