DigitalOcean Ansible Guide
Ansible playbooks breakdown#
For modules used from Ansible-Galaxy / GitHub see the requirements file.
Ansible within this template does the following:
main.yml#
- Loads encrypted Variables from crypt_vars/all.yml
- Loads tf_vars from Terraform via tf_vars/tf_vars.yml
- Runs the Apt role to install a handful of required/useful packages
- Sets up 3 users: root, devops, and deploy
- Triggers the strapi_database.ymlplaybook
User definitions are as such (They all have no password set, aka disabled password):
- Root user => initial connection for main.yml, using defined SSH key in Terraform
- Devops user => "Admin" sudo user you should regularly use to connect or allow team members to connect to (adjust to fit your needs) and also used for the database/strapi server setup (stop using root people)
- Deploy user => Strapi's service user, what Strapi runs as
warning
Deploy user does not have sudo perms, this is intended!
Apt packages that are installed on both systems are:
- software-properties-common
- build-essential
- net-tools
- zip
- unzip
It will also automatically apply software updates using the dist upgrade and automatically remove packages that are no longer needed.
playbooks/strapi_database.yml#
- Loads encrypted Variables from crypt_vars/database.yml
- Loads tf_vars from Terraform via tf_vars/tf_vars.yml
- Installs MariaDB v10.3
- Creates a database for Strapi
- Creates a user for Strapi
- Sets user permissions on the database
- Triggers the strapi_server.ymlplaybook
Database name and user are based on the labels you set for the DigitalOcean instances in Terraform, thus the defaults are:
- DB Name: my-strapi-db
- DB User: my-strapi-admin
The password is stored in the crypt_vars/database.yml and this file should be encrypted (see instructions below on dealing with Ansible-Vault)
playbooks/strapi_server.yml#
- Loads encrypted Variables from crypt_vars/strapi.yml
- Loads encrypted Variables from crypt_vars/database.yml
- Loads tf_vars from Terraform via tf_vars/tf_vars.yml
- Installs Node via the version defined in group_vars/strapi.yml(default is v14)
- Installs yarn
- Installs the Acme.sh client (way better than certbot)
- Requests Let's Encrypt SSL Cert using Cloudflare DNS-01 Verification
- Installs Nginx, configures upstream, deploys configs for HTTP => HTTPs
- Creates the deploy directory and various child directories /srv/deploy/*
- Installs PM2 globally
- Sets up PM2 to be loaded on reboot and start previous services
- Triggers the strapi_dply.ymlplaybook
playbooks/strapi_dply.yml#
First off don't ask why it's named this way, the Ansible linter I use throws errors if I use deploy and I'm lazy and don't feel like fixing it. ¯\_(ツ)_/¯
- Loads encrypted Variables from crypt_vars/strapi.yml
- Loads encrypted Variables from crypt_vars/database.yml
- Loads tf_vars from Terraform via tf_vars/tf_vars.yml
- Uses the ansistrano deployment system to version deployments and make it easier to rollback if failures happen.
- Pulls project from Git source
- Pushes templated .envandecosystem.config.jsfor Strapi and PM2
- Installs node_modules (using yarn, fuck npm)
- Builds the Strapi Admin in production mode (Stop deploying dev servers)
- Starts/Reloads Strapi application
Eventually I want to add checking to ensure the Strapi project started correctly and do an automated DB backup. Should failures occur then it would run the rollback playbook and restore the DB from the backup.
playbooks/strapi_rollback.yml#
info
This feature is not developed yet
Ansible Requirements#
First off, if you are not familiar with Ansible-Vault what are you doing with your life? Go do some research. There is a default Ansible config that sets some sane defaults located here. I suggest you read through it to understand how it's setup.
This template uses various roles from Ansible-Galaxy and misc GitHub repos, I suggest you look at the requirements file and review their documentation if you plan to make changes. There is also a script to automatically install them.
Next you need to make a vault_password file at the ansible folder root to encrypt/decrypt the crypt_vars/* files. See the example folder and it's README.md for some templates and a sample encrypted file. There is a password generation script located here for my fellow lazy folks. Keep that password safe and handy, if you lose it, back to square one on configuring shit
Ansible Instructions#
info
This section is still a WIP
Rough breakdown for those that can't wait:
- run the ./install_requirements.sh
- create your vault_passwordfile
- configure your vars
- run the ./run.sh main.yml