Makes converting a docker-compose.yml setup into an ansible role faster using copier
.
To bootstrap the template, copier
tools is used alongside custom jinja extensions:
pipx install copier
pipx inject copier copier-templates-extensions
Usage:
mkdir my_role && touch my_role/docker-compose.yml
# Add your desired compose config
copier gl:lksch-group/deploy-docker-compose-template --trust my_role
- As of version 2.18.0, docker compose v2 module can be used. This requires
python-docker
package anddocker-compose-plugin
apt packages to be installed. - For docker compose v1, use
compose_v1: true
. This installs docker-compose python package and the docker package as pip package for docker user. (You might need the packageacl
installed when you run into privilege escalation problems)
- If any of the
docker-compose.yml
defaults have{{
in it, you have to override the ansible default with!unsafe "{{ ..."
manually - The service names inside
docker-compose.yml
shall not use spaces or-
. Always use_
to be compatible with ansible variables env_file
support is limited. Ondocker compose config
, the variables are merged with values inenvironment
. Use the pre-startup commands to generate this env file
The template rendering context is enriched using a custom extension that:
- Uses
docker compose config
in order to analyze the compose file. - Identifies secret strings and construct a secret path using one of the supported secret provider
- Uses the length of the example secret in
docker-compose.yml
or usesmin_secret_length
, whatever is longer
- Uses the length of the example secret in
- Transforms env vars into ansible defaults
- If a
user
key is present for a service, it signals a rootless deployment (eitheruser: 1000
oder with a group:user: 1000:1000
). The uid is replaced withdocker_user_id
from the copier answers. - Identifies all exposed ports of the setup (e.g. might be used for firewall rules config by ansible)
- Identifies mounted volumes locally or docker volumes and extracts paths for eventual backup configs
- Generates a final combined docker-compose config injecting external
proxy-tier
network if needed.
- All variables defined by
environment
are converted into role defaults - If a file is mounted from
compose_file_path
, it's checked if it can be read as an env file, and rendered as part of the role. See minio example - If an
env_file
is specified instead ofenvironment
, you have to create this file yourself via the pre-startup commands.