The first two playbooks completed the VM installation and made it ready. Now we get to start configuring our VM as a Nextcloud server. This playbook installs the prerequisite applications Apache2, MariaDB and PHP
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 at least installed the acl package from Playbook 2 - OS Config even if you skipped the rest.
Playbook Requirements
In the previous two playbooks we completed all of the usual tasks for building a brand new Ubuntu virtual machine. We now need to install the prerequisites: Apache2, MariaDB and PHP.
This playbook will:
- Install MariaDB 10.6 from a mirror of mariadb.org since the default Ubuntu 20.04 version is too old
- Install Apache2
- Install the required PHP from the ondrej/php PPA since the default Ubuntu 20.04 version is too old as well
- Enable PHP-fpm
There's a good argument for these being made into roles and then this playbook is merged with the nextcloud4.yaml instead. The reasoning for not doing that is it keeps this a general LAMP installer playbook that can be reused for other applications such as Joomla. And also there's dependencies between Apache2 and PHP that means I'm not sure they could be packaged as separate roles.
Update the Ansible Inventory
No update needed for this playbook
Update the Playbook Launcher
We need to enable nextcloud3.yaml in nextcloud.sh, our playbook launcher script.
RunPlaybookSSH "nextcloud2.yaml"RunPlaybookSSH "nextcloud3.yaml"
#RunPlaybookSSH "nextcloud3.yaml"
#RunPlaybookSSH "nextcloud4.yaml"
#RunPlaybookSSH "nextcloud5.yaml"
Simply remove the # from the beginning of the line.
Update the Ansible Requirements File
No update needed for now, but of course I will need one once the role local_mariadb has been properly packaged up.
Play: Deploy Foundation Applications
The installs mean this one takes a little while to run even though it's technically quite simple. We use the var/nextcloud.yaml to provide the version of PHP to install which is related to the version of Nextcloud too.
Filename: nextcloud3.yaml
---# Ansible Playbook
# New Nextcloud host setup# Stage 3: Deploy foundation apps## Uses the ansible SSH key
- name: Deploy foundation applications hosts: "{{ target }}"
vars_files: - vars/nextcloud.yaml
roles: - role: local_mariad
The MariaDB install is nicely self-contained so I've made this a role and I'll package it as a proper role at some point too.
tasks:
# Install Apache2
- name: Deploy default Apache2 become: true ansible.builtin.package: name: apache2 state: present
This task installs the default version of the Apache2 web server from the standard Ubuntu repositories.
# Install PHP8.2 - name: Add PPA for ondrej/php become: true ansible.builtin.apt_repository: repo: 'ppa:ondrej/php' mode: '644' update_cache: true
- name: Install PHP base become: true ansible.builtin.package: name: - "php{{ php_release }}-fpm" - libapache2-mod-fcgid
Ubuntu 20.04 has PHP 7.4 in the standard repositories which went out of support in 2022. So we install a PPA for the ondrej PHP packages and use that version instead. And when PHP 8.3 becomes the recommended version for Nextcloud (it's already supported) it will be a simple change to switch to that version.
- name: Activate required Apache2 modules become: true shell: /usr/sbin/a2enmod proxy_fcgi setenvif rewrite
- name: Activate php-fpm become: true shell: /usr/sbin/a2enconf php{{ php_release }}-fpm
- name: Restart apache2 become: true ansible.builtin.systemd: state: restarted name: apache2
...
Finally we enable the Apache2 proxy_fcgi, setenvif and rewrite modules, the configuration for php-fpm and then restart Apache2 so it they take effect.
Role: local_mariadb
The default version of MariaDB in the Ubuntu 20.04 repositories is too old so we install this from mariadb.org. Since there's no other dependencies I've made this a local role for this playbook and will move this to a full role later on.
Filename: roles/local_mariadb/files/mariadb10-6-sources
# MariaDB 10.6 repository list - created 2023-12-28 19:44 UTC# https://mariadb.org/download/X-Repolib-Name: MariaDBTypes: deb# deb.mariadb.org is a dynamic mirror if your preferred mirror goes offline. See https://mariadb.org/mirrorbits/ for details.# URIs: https://deb.mariadb.org/10.6/ubuntuURIs: https://mirror.netcologne.de/mariadb/repo/10.6/ubuntuSuites: focalComponents: main main/debugSigned-By: /etc/apt/trusted.gpg.d/mariadb-keyring.pgp
The sources file is generated from the MariaDB Server Repositories tab at https://mariadb.org/download and I should look at that dynamic mirror thing too.
Filename: roles/local_mariadb/tasks/main.yaml
---# Ansible Role Tasks# Ubuntu 20.04 LTS# Deploy MariaDB 10.6
- name: Get MariaDB signing apt-key become: true ansible.builtin.get_url: url: https://mariadb.org/mariadb_release_signing_key.pgp dest: /etc/apt/trusted.gpg.d/mariadb-keyring.pgp owner: root group: root mode: '0644'
- name: Drop MariaDB source config become: true ansible.builtin.copy: src: mariadb10-6.sources dest: /etc/apt/sources.list.d/mariadb10-6.sources owner: root group: root mode: '0644'
- name: Install MariaDB become: true ansible.builtin.apt: update_cache: true name: - mariadb-server - mariadb-client...
The role gets the MariaDB signing key and save it to /etc/apt/trusted.gpg.d and then drops the sources file we generated into the /etc/apt/sources.list.d directory. It then does an apt update so the repository is recognised and finally installs the MariaDB server and client packages.
Variables: Nextcloud
For this playbook we need the version of PHP to install which is held in the php_release variable. We will add more to this variable file in the next playbook too.
Filename vars/nextcloud.yaml
---# Ansible Vars# Variables for Nextcloud playbook# Commented variables should be in inventory
php_release: "8.2"
[...]
Review
This is nice simple playbook and once it's run we'll be ready to start installing Nextcloud itself.