While the Ansible Playbooks do the actual work, they're not much use without information about the landscape they need to work on. The Ansible Inventory describes the target landscape in a structured way that both humans and playbooks can understand.
The Ansible Inventory
In its simplest form the inventory is just a plain text file formatted in the old Windows INI format, in it's most complex the inventory can be generated on demand using YAML format files or plugins to CMDBs and other databases, VMware vSphere or whatever cloud infrastructure platform you're using.
Since I'm deploying to my GeekLab and don't have access to the vSphere API's I use YAML-formatted text files to hold my inventory.
Editing and Version Control
One of the advantages of having my entire inventory in YAML files is simplicity. All you need is a text editor and a directory to work in and you're ready to go. While you could use vi, or xed or whatever your desktop uses, something that will let you see a directory tree, open multiple files at once and search across them is going make life just that little bit easier.
Another thing to consider is version control. Your inventory files are a critical resource in Ansible. Make a mistake and interesting and embarrassing things can happen. Sometimes, especially during development, you may want to try something and then undo it. Since we're using files we may also want to share them between machines too, perhaps having development ones on a desktop VM and the finished version accessible from a controller VM. This can very quickly get messy but the problem was solved years ago with version control software. Or at least, the problem was formalised and processes developed to help control it.
Since we're in a Linux environment, we have access to the infamous git, a package that when it comes to usability frequently lives down to its name. But it works and more importantly there's a lot of tooling that works with it, including Ansible itself. And so do a lot of editors.
I've ended up using the Linux edition of Visual Studio Code for Ansible as it lets me see an entire tree of files and edit with ease, has plugins to use git and to also view the repository and do most admin tasks, helps check and report issues with lots of different file types and formats including bash and YAML. There's lots of choices and options so find one that works best for you.
I've found having separate repositories for inventory and each playbook works best for me, and I also have a fileserver VM that acts as a git server (and also gets backed up each night for additional safety) so I create repositories there and then create a local clone on a Desktop VM dedicated to Ansible for development work.
I am not going to delve any deeper into git in these articles, but I will probably add an separate how-to on configuring my setup. While git is very useful it's a learning experience all by itself.
Vaults
As part of the process of deploying Nextcloud we need to handle usernames and passwords which are kinda sensitive. And if you're using git to manage your Ansible files it's very easy to accidentally include a password in a file which even if you remove it from the file or delete the file completely, it will remain in the git repository. Which, as people find out the hard way far too often, is a bad thing.
While we could do stuff like mark certain files as excluded from git, if we want to deploy our playbooks from another VM we'll have to manually update with the missing info each time.
Ansible has the concept of vaults that are encrypted files that you can safely store secrets in and the files can then be included in source control as without the corresponding key to access them the data is safe. Obviously you do not want to include the key in any file that gets checked in to the same repository.
I keep the keys to the vault files in a control directory, along with the SSH key that will be used by Ansible to connect to the target machines. This isn't under git control and I can use other techniques to protect these files, such as making it a LUKS filesytem or using a secret server if I need to without having to rewrite everything.
Ansible Directory Structure
In my simple approach I need three directories for Ansible:
- Control directory for keys and credentials
- Ansible Inventory - initialised as a git repository
- Ansible playbook - initialised as a git repository
They can all be under the same project directory, and it's good practice to have separate live and development inventories too. I have a playbooks directory under which I create directories for each playbook
~/Projects
ansible
control
inventory
playbooks
nextcloud
otherbook
The control and playbook directory will be properly introduced in the next article.
Inventory Structure
As mentioned above I started out with a single file inventory.yaml for my inventory which is works perfectly well but after a while it starts to get a bit on the long side. I then broke it up into 4 smaller files with a base and then one file for each for dev, test and live landscapes which brought in the advantage that config for one landscape shouldn't be affected by changes to another.
This means my basic layout for the inventory directory looks like this
inventory
000-base.yaml
100-dev.yaml
200-test.yaml
If using an directory as your inventory instead of a single file Ansible will read the files in filename order. Some configuration items also need to be specified in order too so by using a three digit prefix I can still use descriptive names but also be sure of the order in which they'll be read.
000-base.yaml
The 000-base.yaml file contains variables that apply to the entire inventory.
--- # Ansible Inventory
# Dev configuration
# 27 Dec 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
all:
# hosts:
vars:
# email for notifications to go here - central for all playbooks
config_mailalert_host: mail.example.com
config_mailalert_port: 25
#config_mailalert_username: maybe comes from vault
#config_mailalert_password: must come from vault
config_mailalert_secure: never
config_mailalert_sender: no-reply[at]automation.example.com
config_mailalert_to: alert+automation[at]example.com
bacula_director_host: selene.example.com
bacula_director_name: selene-dir
bacula_deploy: true
children:
control:
hosts:
localhost:
ansible_connection: local
...
There's no Nextcloud specific content in this file. The config_mailalert_* variables define the connection to a mail server that allows some of my Ansible roles to send an email notification with the results such as expiry dates of certificates they've generate so I can add them to a calendar. The bacula_* variables define the Bacula director to be configured by my Bacula roles to configure the backups.
100-dev.yaml
The 100-dev.yaml file holds variables and hosts for the dev landscape
--- # Ansible Inventory
# Local development
# 4 Aug 2023
# Depends on: 000-base.yaml
# Notes:
# Group names must be globally unique or they well get merged. Parents do not distinguish
# Check: ansible-inventory -y -i path-to-inventory --list
#################################################
# Local Development
#
dev:
vars:
config_landscape_tier: "D"
apcupsd_device: 192.168.524.1
apcupsd_batterylevel: 50
apcupsd_runtime: 10
bacula_deploy: true
bacula_server_clientconfig: false
network_ntp: 192.168.524.1
children:
nextcloud_dev:
hosts:
svu-ncdeploy-1.sandpit.example.com:
svu-ncdeploy-2.sandpit.example.com:
svu-ncdeploy-3.sandpit.example.com:
...
We make our first reference to Nextcloud in this file, although there's no actual configuration here. It defines a dev group and sets variables to be assigned to all hosts within my dev landscape. In this case I'm setting some variables needed by my roles that deploy apcupsd and Bacula clients and setting which host to use for NTP for another playbook.
This is the file where I define all the hosts in the dev landscape. I have defined 3 hosts for use with developing and testing the playbooks and placed them into a nextcloud-dev group as I will need to set some additional variables for them. These hosts are also members of the dev group since nextcloud-dev is defined within dev.
While I could place the variables for the Nextcloud playbooks into this file under the nextcloud-dev group or the hosts, Ansible provides an alternative mechanism that can make automation of the inventory a bit easier.
I could also move the definition of the nextcloud hosts to a seaprate file such as 110-dev-nextcloud.yaml and keep this one just for permanent hosts such as the sandpit subnets firewall/router, mail and file servers.
Group_vars
Ansible reserves a special directory group_vars into which we can place yaml files with variables to be used for groups, or even sub-directories with yaml files. So while I could have put a vars: section for nextcloud-dev in the 100-dev.yaml, I have put these into a nextcloud_dev.yaml file instead.
---
# Ansible Vars
# group_vars for nextcloud
nextcloud_mail_smtphost: devmail1
nextcloud_mail_smtpport: 25
nextcloud_mail_smtpsecure: ''
nextcloud_mail_smtpauth: False
nextcloud_mail_from_address: "{{ inventory_hostname_short }}-notify"
nextcloud_mail_domain: sandpit.example.com
...
The advantage is if I want to change the config for Nextcloud I don't have to touch the 100-dev.yaml file so no risk of messing that up. I'm not sure if it makes much difference to the ease of administration yet.
host_vars
Just like we can move group level variables into a group_vars directory, we can do the same with hosts, so instead of editing the 100-dev.yaml for each host, we can create configuration files for each host individually.
We can either have a single file for each host, or create a directory for each host and then put individual configuration files into each hosts directory.
inventory
host_vars
svu-ncdeploy-1.sandpit.example.com
nextcloud.yaml
nextcloud-vault.yaml
The hostname needs to match While this could help with inventory automation as adding a new host could be as simple as create the directory and then generate the config files and not have to parse and edit the 100-dev.yaml, the big advantage is allows us to use vaults to securely hold sensitive data within the inventory.
nextcloud.yaml
This file hold host-specific configuration for a Nextcloud installation. It could also override any group-level configuration set in group_vars/nextcloud-dev.yaml
---
# Ansible Vars
# Variable for host svu-ncdeploy-1
nextcloud_admin_user: ncdevmin
nextcloud_admin_pass: "{{ nextcloud_vault_admin_pass }}"
nextcloud_db_name: nextclouddb
nextcloud_db_user: nextcloud
nextcloud_db_pass: "{{ nextcloud_vault_db_pass }}"
...
The detail of this will be covered in [playbook-4], the key point here is we can easily set per-host configuration. The sensitive entries nextcloud_admin_pass and nextcloud_db_pass are not given in this file, but instead refer to two different variables nextcloud_vault_admin_pass and nextcloud_vault_db_pass which means from a playbook level I don't have to care if these are vaulted parameters or not.
nextcloud-vault.yaml
For the sensitive parameters I can hold them in a separate file within the host subdirectory
---
# Vaulted Nextcloud host vars for svu-ncdeploy-1
nextcloud_vault_admin_pass: AdminPassHere
nextcloud_vault_db_pass: DBpassHere
...
Before this file is checked in to git it needs to be encrypted with ansible-vault. Once encrypted the contents of nextcloud-vault.yaml will look something like this:
$ANSIBLE_VAULT;1.1;AES256
65663862653364356537366336356233636234643161626633643435633635633432306338306133
6638636334306562663636623636383363333437363766320a323133353563663532323738386266
39653039666639613332663836373764633639366364383635613538613933306231613836323763
3263666437333039390a313861303234313639656237346236303030316266656338363835623761
When we run the playbook we will need to supply the password to read the vault which could be given passed as a parameter to the execution script or read from a file such as control/nextcloud-vault.txt which only an ansible execution user has access to.
Final Layout
Combining this together the layout of our inventory directory now looks something like this:
~/Projects
ansible
control
nextcloud-vault.txt
inventory
000-base.yaml
100-dev.yaml
group_vars
nextcloud-dev.yaml
host_vars
svu-ncdeploy-1.sandpit.example.com
nextcloud.yaml
nextcloud-vault.yaml
svu-ncdeploy-2.sandpit.example.com
nextcloud.yaml
nextcloud-vault.yaml
playbooks
nextcloud
The inventory directory should be initialised as a git repository (if you're happy with using git) to provide version control and let you easily make notes of why you made certain changes and using this approach you would also set the inventory directory as a whole as your ansible inventory rather than a file within it.
Setting the inventory location will be covered in the next chapter as we create our first playbook to start preparing our VM for Nextcloud installation.
Bootnote
Most of this series is written in the order that things happened, even if there's a bit of a delay. The configuration of the Ansible Inventory evolved significantly while creating the Nextcloud playbooks so in the interests of clarity I'm going to skip most of the beginning, all of the middle where my inventory evolved and bring the end back in time to before the start and we'll build our inventory as if we were starting scratch but heading directly to its final configuration. For what it's worth the way Inventories can be created and managed has changed significantly over time and during this project I finally got up to date.
All host names have been changed to example.com and IP addresses are illegal so that if you copy these config it'll force you to correct them and hopefully prevent accidentally doing something silly on your own systems.