This is pretty much a housekeeping playbook. We fix two more issues from the OFV template, generate a TLS certificate request and install the standard admin applications I use on all Linux VMs.
Automation Overview
If you refer back to the Automation Approach I broke the process for deploying a Nextcloud VM down into six steps:
- Create a virtual machine with the operating system installed and ready for Ansible to start configuration ✅
- Ansible playbook for initial OS configuration to prepare for the main automation execution. ✅
- Ansible playbook to complete OS configuration and install standard utilities and applications
- Ansible playbook to configure Nextcloud-specific pre-requisites such as filesystems, PHP and MariaDB
- Ansible playbook to create the database and deploy Nextcloud itself
- Ansible playbook to do the initial configuration of Nextcloud
This playbook will connect to our target VM as the ansible user with an SSH key so there no need for any further manual intervention (unless something breaks of course).
If you're following along then you'll need to have completed the config from Playbook 1 - First Contact and it's worth taking another snapshot at this point so you can quickly roll the VM back if something breaks. You may also want to skip most of this, or substitute your own configuration if it's not relevant to your environment.
Playbook Requirements
In the previous playbook we applied the bare minimum configuration to our freshly deployed VM just so it could run this playbook under an ansible user. We now need to complete the remaining OS configuration tasks so that by the end of this playbook our Ubuntu instance is ready for application installation to begin.
This playbook will:
- Cleanup the unused swapfile left by the Ubuntu installer.
- Blacklist the VMware device driver in multipathd so it doesn't fill the logs with complaints
- Generate a TLS certificate request
- Apply 3 selected package updates
- Install apcupsd so the VM can be nicely shut down if I have a power failure
- Install the Bacula client so the VM can be backed up.
Update the Ansible Inventory
All the inventory variables we need for these roles are already defined in our Ansible Inventory as either applicable to all hosts in the 000-base.yaml file or such as for the apcupsd configuration in our dev landscape file 100-dev.yaml.
Update the Playbook Launcher
We need to make two updates to the playbook launcher to include our new playbook which will be called nextcloud2.yaml.
Filename: nextcloud.sh
# Placeholder for RunPlaybookSSH ()
Remove this line if you like since we're now going to add in a bash function for RunPlaybookSSH()
RunPlaybookSSH () {
local PLAYBOOK=$1 local PLAYBOOK_RET="X"
echo "Running $PLAYBOOK for $TARGET_HOST"
ansible-playbook -v --private-key ../../control/ansible -u ansible $PLAYBOOK --extra-vars $ANSIBLE_TARGET
PLAYBOOK_RET=$? if [[ $PLAYBOOK_RET == 0 ]]; then echo "Playbook $PLAYBOOK reported success for $TARGET_HOST" else echo "Playbook $PLAYBOOK reported error $PLAYBOOK_RET for $TARGET_HOST, stopping." exit 1 fi
}
Copy this bash function where the placeholder was. The --private-key parameter for ansible-playbook specifies the location of the SSH private key (and pubic key or certificate) that was generated at the end of Playbook Preparation to use for connecting to the target host. The -u parameter says who we're going to connect as and $PLAYBOOK is the playbook we're going to run with is passed into the function as the first parameter. As before --extra-vars is used to set the target to run the playbook against.
# Need to pause here until target host becomes available
while [ $PING_RET -gt 0 ]; do
ping -c 3 -i 10 $TARGET_HOST
PING_RET=$?
echo -e "$(date): Got $PING_RET, waiting another minute for $1"
sleep 60s
done
RunPlaybookSSH "nextcloud2.yaml"#RunPlaybookSSH "nextcloud3.yaml"#RunPlaybookSSH "nextcloud4.yaml"#RunPlaybookSSH "nextcloud5.yaml"
echo ""echo "------------------------"echo "Nextcloud Build Complete"echo ""
At the end of our script after while loop that checks if the target host is alive after running the first playbook we want to add lines to run the remaining playbooks via our RunPlaybookSSH function.
For now comment out the lines for nextcloud3.yaml to nextcloud5.yaml playbooks since we've not written them yet.
Update the Ansible Requirements File
In Playbook 1 - First Contact we created a requirements.yaml file to allow us to easily import roles from a local git server. We can reuse three more roles from the newhost playbooks by adding them to this file.
Note: And just like in the first playbook, until I sort out making these roles publicly available (probably via github or gitlab) you don't need this file unless of course you already have your own set of default roles to apply.
As an interim measure details of these roles will be available in these links: [host-multipath] [host-tls] [apcupsd] [bacula-client]
Filename: roles/requirements.yaml
[...]
############################# nextcloud2.yaml
# Three more roles will be added here for playbook 2
- name: host-multipath
scm: git
src: tiger.example.com:/srv/git/ansible/roles/host-multipath.git
version: master
- name: host-tls scm: git src: tiger.example.com:/srv/git/ansible/roles/host-tls.git version: master
- name: apcupsd scm: git src: tiger.example.com:/srv/git/ansible/roles/apcupsd.git version: master
- name: bacula-client scm: git src: tiger.example.com:/srv/git/ansible/roles/bacula-client.git version: master
# And add collections too. Needs an explicit call from ansible-galaxy to install.
collections: - name: community.general - name: community.crypto
[...]
You can remove the comment about three more roles will be added if you like.
My host-tls role needs the community.crypto collection and along with bacula-client needs the community.general for sending mail notifications from the Ansible control host. Any newly added roles or collections will need our refresh-roles.sh script to be run to actually download and deploy them.
Play: Main OS Config
This is also based on my newhost playbooks and its job is to complete all other standard OS configuration tasks for hosts in my Geeklab.
Filename: nextcloud2.yaml
---# Ansible Playbook
# New Nextcloud host setup# Stage 2: Remaining OS prep and deploy the admin applications## Uses the ansible SSH key
- name: Main OS Config hosts: "{{ target }}"
roles: - role: local_host-swapfile
- role: host-multipath when: "'workstation' not in group_names"
These first two roles correct two issues from the Ubuntu installer. The local_host-swapfile removes the redundant swapfile that Ubuntu configures even though I use swap partition. This should be made into a proper role and shared back to the newhost playbook, and of course I should fix this in the OVF template so it's not needed here anyway.
The second role host-multipath blacklists the VMware Virtual disk device in the multipath config which isn't used but fills the logs with errors. I should also fix this in the OVF template so it's not needed here.
- role: host-tls vars: - tls_host_requestcert: true
The host-tls role deploys my internal CA root certificate to all Linux hosts, which is why it runs in this playbook. By setting tls_host_requestcert to true it also generate a private TLS key for the host and creates a certificate signing request which is retrieved by the Ansible control note and mailed to me using Ansible Inventory variables to set the address. I then generate the certificate while the rest of the playbooks are running and it's ready by the time Nextcloud is installing.
tasks:
- name: apt update - selected components become: true ansible.builtin.apt: autoclean: true autoremove: true update_cache: true name: - linux-image-generic - openssh-server - acl state: latest notify: Reboot host handlers: - name: Reboot host become: true ansible.builtin.reboot:
Once my Ubuntu config roles have run I then update the apt cache, and upgrade to the latest Linux kernel and OpenSSH server as these are critical, and install the ACL package. Installs seem to be quite slow under Ansible so I save a general upgrade of everything else untill the very end. Since we've upgraded the kernel a reboot is required but at least this time is should retain its IP address so we can easily reconnect and more importantly the playbook will continue once the host is back and running which in our case means going on to the second play.
Role: local_host-swapfile
Ubuntu creates a swapfile even if you specify a swap partition during installation which takes disk space. This role nicely deletes it. This will shortly be moved to a proper reusable role and distributed via my internal git server the requirements file.
Filename: roles/local_host-swapfile/tasks/main.yaml
---# Ansible Role Tasks# Ubuntu 20.04 LTS# Role for new host setup: Remove unwanted swapfile
- name: Disable swap in fstab become: true ansible.builtin.lineinfile: backrefs: true path: /etc/fstab regexp: '^/swap.img(.*)' line: '# /swap.img\1'
- name: Delete swapfile become: true ansible.builtin.file: path: /swap.img state: absent
...
You would only want to do this if you have a swap partition defined otherwise Bad Things can happen. And it also needs a reboot to become effective so there should be a notify for the reboot handler, but that happens later in this play anyway.
The first task uses regular expressions, a huge topic in its own right, to look in the file /etc/fstab and look for a line that starts with /swap.img and then remember everything that comes after that. The ^ character means start of the line, the .* means to match everything from this point on until we get to some other characters, which in this case we don't. The () means to remember whatever characters are within them which is the rest of the line.
We then set the line to be # /swap.img followed by everything it remembered in the 1st set of brackets. So effectively we've just commented out this /swap.img line.
The second task does exactly what it says and deletes the /swap.img file by setting its state to absent. If I wanted to be more careful I would check to see if the /swap.img is actually in use as a swapfile first of course.
Play: Deploy admin applications
This is the second half of the playbook and handles the admin applications that I apply to all Linux VMs in my GeekLab.
Filename: nextcloud2.yaml (continued)
- name: Deploy admin applications hosts: "{{ target }}"
roles: - role: apcupsd when: "'workstation' not in group_names"
- role: bacula-client when: bacula_deploy
...
My GeekLab server is protected by a UPS which gets used a couple of times a year - it used to be a couple of times a month when we first moved here - so it's very much needed. We may be 10 miles from the UK's second city but it's semi-rural (in the "horsey belt" as a colleague once described it - just far enough out of the city to be green, but not so far that it the city dwellers too long to go see them and do horsey stuff).
The apcupsd role deploys the apcupsd service from the standard repos and then configures it using the apcupsd_* variables for the dev group (which is actually a subnet) in our Ansible Inventory, unless the host is in the workstations group which are not VMs so aren't protected by the UPS.
I also use Bacula to backup (and very occasionally restore) each VM and workstation. My bacula-client role deploys the client to the target host (unless it's specifically excluded using the bacula_deploy = false), configures it to connect to the Bacula director and even drops a default configuration file onto the Bacula server for me too.
At this point we're ready to now start installing application that that's handled in our next playbook.
~/Projects
ansible
control
ansible
ansible.pub
ansible-cert.pub
host-ca
vault-pass
inventory
[inventory files]
playbooks
nextcloud
.gitignore
ansible.cfg
ansible-env-dev.sh
refresh-roles.sh
nextcloud.sh
nextcloud1.yaml
nextcloud2.yaml
roles
requirements.yaml
local_host-rename
tasks
main.yaml
local_host-id
tasks
main.yaml
local_host-swapfile
tasks
main.yaml
[other downloaded roles]
vars
config-nextcloud.yaml
vault-nextcloud.yaml
vm-template.yaml
Review
This playbook was based on the second of my newhost playbooks and the roles I created as part of that, and the write-up is also on my article backlog. It would add too much to this article to go into them here and yes, I know it makes this article a bit harder to follow, especially if you're using it as a tutorial. I also intend to put all the roles and playbooks onto an easy location for download, possibly GitLab or GitHub or maybe just a zip download but there's a few things I need to work through first - and probably a couple of articles will come from that process as well.
As mentioned in previous articles, since there's already a degree of dependency between the OVF template it might be worth pre-installing things like apcupsd and the Bacula client so only updates would be needed. The redundant swapfile and fix for multipath should also have been applied while creating the OVF template. It would saved some typing at least.