This repository has been archived on 2020-05-08. You can view files and clone it, but cannot push or open issues or pull requests.
dokku-ansible-playbook/README.md

124 lines
3.9 KiB
Markdown
Raw Normal View History

2020-03-19 00:13:54 +00:00
# dokku-ansible-playbook
Run ansible playbooks during deployment.
2020-03-22 11:41:23 +00:00
This plugin can be useful when you need to provision your server before or after a deployment of your application (or on [any hook](https://github.com/dokku/dokku/blob/master/docs/development/plugin-triggers.md), just raise an issue and let's add it) and you prefer to use Ansible instead of Bash for certain tasks. Bash can still be the right tool for other things but sometimes, it can be become tricky to manage the idempotent case in Bash.
For example, you can make use of the official [ansible-dokku](https://github.com/dokku/ansible-dokku/) roles.
2020-03-19 00:13:54 +00:00
## Requirements
2020-03-21 00:08:57 +00:00
- dokku 0.19.13+
- Debian based system (uses `apt` package manager for installing dependencies)
2020-03-19 00:13:54 +00:00
## Installation
```shell
$ dokku plugin:install https://github.com/decentral1se/dokku-ansible-playbook.git
$ dokku plugin:install-dependencies
2020-03-19 00:13:54 +00:00
```
## Usage
2020-03-21 22:20:52 +00:00
All files must be placed within the `ansible` folder of your git repository. Everything is copied into `$DOKKU_LIB_ROOT/data/ansible/$APP` on the `post-extract` hook. Dokku will make sure that your Ansible plays are run on various hooks against the Dokku server localhost.
2020-03-19 00:13:54 +00:00
2020-03-21 00:08:57 +00:00
- `requirements.yml`: what role dependencies to download before running your plays.
- `pre-deploy.yml`: play run before a deployment
- `post-deploy.yml`: play run after a deployment
2020-03-22 11:45:21 +00:00
- `post-delete.yml`: play run after an application delete
- `vars/...`: variable files (you'll need to include manually with the [include_vars](https://docs.ansible.com/ansible/latest/modules/include_vars_module.html) module)
2020-03-19 00:55:49 +00:00
2020-03-21 00:08:57 +00:00
## Passwords
2020-03-21 22:20:52 +00:00
Ansible uses the [vault](https://docs.ansible.com/ansible/latest/user_guide/vault.html) password file which can be used to decrypt secrets.
2020-03-21 00:26:35 +00:00
2020-03-21 22:20:52 +00:00
To get started with enabling this, you should generate a vault password for your self and run the following on your Dokku host.
2020-03-21 00:08:57 +00:00
```bash
2020-03-21 23:50:52 +00:00
$ dokku ansible-playbook:vault-pass
2020-03-21 00:08:57 +00:00
```
2020-03-21 22:20:52 +00:00
Then you can start to encrypt your passwords on your local machine with the following.
2020-03-21 00:08:57 +00:00
```bash
$ ansible-vault \
encrypt_string \
--vault-password-file ansible/.vault.sh \
--name mysecretname \
mysecretvalue
```
2020-03-21 22:20:52 +00:00
Where `ansible/.vault.sh` might look like this.
```bash
#!/bin/bash
set -eu -o pipefail
echo "my-cool-vault-password"
```
2020-03-22 11:45:21 +00:00
Then for example, if you want to pass a sudo password, you might include a `vars/ansible_become_password.yml`.
2020-03-21 22:20:52 +00:00
```yaml
---
ansible_become_password: !vault ...
```
2020-03-19 22:29:59 +00:00
2020-03-21 00:33:46 +00:00
## Permissions
2020-03-21 22:20:52 +00:00
Since the `dokku` user account runs the plays on the host, you will need to deal with sudo permissions when you want to use `become: true` to run a privilege escalation to the root account. You can give your `dokku` user account passwordless sudo access but that would give a lot of power to people who can get access to that user account. A solution to this can be to add your `dokku` to the sudoers group, give the account a password (`passwd dokku && usermod -aG sudo dokku`) and pass `ansible_become_password` in as a variable.
2020-03-21 00:33:46 +00:00
2020-03-22 11:45:21 +00:00
## Injected variables
Same as the plugin available variables but in your Ansible plays.
- `dokku_lib_root`
2020-03-19 00:55:49 +00:00
## Example
2020-03-19 22:29:59 +00:00
### ansible/requirements.yml
2020-03-19 00:55:49 +00:00
```yaml
---
- src: dokku_bot.ansible_dokku
version: v2020.3.15
```
2020-03-22 11:46:32 +00:00
### ansible/vars/ansible_become_password.yml
```yaml
---
ansible_become_pass: !vault |
$ANSIBLE_VAULT;1.1;AES256
34396236353735666531323238656533643465303131663464613162396333313836363630666266
6539323631656635333864316166633064633366323936610a656137616334313534333635313232
```
2020-03-19 22:29:59 +00:00
### ansible/pre-deploy.yml
2020-03-19 00:55:49 +00:00
```yaml
---
- hosts: all
tasks:
2020-03-22 11:45:21 +00:00
- name: Load variables
include_vars:
dir: "{{ dokku_lib_root }}/data/ansible/gitea/vars/"
extensions:
- yml
2020-03-21 00:08:57 +00:00
- name: Configure the foobar environment
dokku_config:
app: foobar
restart: false
config:
2020-03-22 11:45:21 +00:00
FOO: "BAR"
- name: Setup host group
group:
name: barfoo
system: true
state: present
become: true
2020-03-19 00:55:49 +00:00
```