Ansible
configures both servers remotely, over . Everything installed on the machines is written ininfra/ansible/: a server can be rebuilt from scratch with one command.Files
| File | Role |
|---|---|
inventory.ini | The machines, in two groups: [dev] and [prod] |
group_vars/all/main.yml | Plain variables: SSH user, /opt/trinity folder |
group_vars/all/vault.yml | Secret variables, encrypted: runner tokens |
playbook.yml | Which roles to apply, in what order |
roles/*/tasks/main.yml | Each role's tasks |
yml
---
- name: Setup Trinity servers
hosts: all
become: true
roles:
- role: docker
tags: docker
- role: ufw
tags: ufw
- role: traefik
tags: traefik
- role: gitlab-runner
tags: gitlab-runnerThe four roles
| What it does | |
|---|---|
docker | Adds Docker's official repository, installs Docker Engine and the Compose plugin, enables Swarm mode |
ufw | Installs the firewall, allows 22, 80 and 443, blocks everything else |
traefik | Creates the trinity network, copies the config and the routes for the machine's group (dynamic/dev/ or dynamic/prod/), creates acme.json, starts Traefik |
gitlab-runner | Installs and registers the runners. See Runners |
Each role has a tag with the same name, to run just one.
Running the playbook
sh
cd infra/ansible
ansible-playbook playbook.yml --ask-vault-pass # everything
ansible-playbook playbook.yml --tags traefik --ask-vault-pass # a single role
ansible-playbook playbook.yml --limit prod --ask-vault-pass # a single machine
ansible-playbook playbook.yml --check --diff --ask-vault-pass # dry run, changes nothingThe playbook is : run again on an already configured machine, it only changes what differs.
Why Ansible rather than commands by hand?
A command typed by hand leaves no trace. With Ansible, the configuration is versioned, reviewed in merge requests, and reproducible: if a VM is lost, a new one is ready in minutes.
The Vault
group_vars/all/vault.yml is encrypted with . It can be committed: without its password, it is unreadable.
sh
ansible-vault view group_vars/all/vault.yml # read
ansible-vault edit group_vars/all/vault.yml # editThe Vault password
It never goes into the repository. The .gitignore excludes .vault_pass* if you store it in a local file. It is passed from person to person, outside Git.
What the playbook does not do
- It does not deploy the applications: that is the pipeline's job.
- It does not create the VMs: they were created by hand in the Google Cloud console.
- It does not manage the domains' DNS.