Last active
March 28, 2023 22:04
-
-
Save ramcq/f76bc2a581270da9245d97b198160bf9 to your computer and use it in GitHub Desktop.
Ansible role to configure borgmatic with the MariaDB and Postgres instances running in Docker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- name: Update APT cache | |
apt: update_cache=yes cache_valid_time=3600 | |
- name: Ensure required docker dependencies | |
package: | |
name: python3-docker | |
- name: Get running container IDs | |
community.docker.docker_host_info: | |
containers: true | |
register: host_info | |
- name: Get mysql container details | |
community.docker.docker_container_info: | |
name: "{{ item }}" | |
register: mysql_containers | |
loop: "{{ host_info | json_query('containers[?starts_with(Image, `mariadb:`) | |
|| Ports[?PrivatePort == `3306`]].Id') }}" | |
- name: Get postgresql container details | |
community.docker.docker_container_info: | |
name: "{{ item }}" | |
register: postgresql_containers | |
loop: "{{ host_info | json_query('containers[?starts_with(Image, `postgres:`) | |
|| Ports[?PrivatePort == `5432`]].Id') }}" | |
- set_fact: | |
mysql_databases: [] | |
postgresql_databases: [] | |
- name: Build mysql_databases list | |
vars: | |
name: "{{ item | json_query('container.Config.Env[?starts_with(@, `MYSQL_DATABASE`) || | |
starts_with(@, `MARIADB_DATABASE`)]') | first | split('=') | last }}" | |
hostname: "{{ item | json_query('container.NetworkSettings.Networks.*.IPAddress') | first }}" | |
port: "{{ item | json_query('keys(''container.NetworkSettings.Ports'')[0]') | split('/') | first | int }}" | |
username: "{{ item | json_query('container.Config.Env[?starts_with(@, `MYSQL_USER`) || | |
starts_with(@, `MARIADB_USER`)]') | first | split('=') | last }}" | |
password: "{{ item | json_query('container.Config.Env[?starts_with(@, `MYSQL_PASSWORD`) || | |
starts_with(@, `MARIADB_PASSWORD`)]') | first | split('=') | last }}" | |
set_fact: | |
mysql_databases: "{{ mysql_databases + [{ | |
'name': name, | |
'hostname': hostname, | |
'port': port | int, | |
'username': username, | |
'password': password, | |
'options': '--default-character-set=utf8mb4' | |
}] | list }}" | |
loop: "{{ mysql_containers.results }}" | |
- name: Build postgresql_databases list | |
vars: | |
name: "{{ item | json_query('container.Config.Env[?starts_with(@, `POSTGRES_DB`)]') | first | split('=') | last }}" | |
hostname: "{{ item | json_query('container.NetworkSettings.Networks.*.IPAddress') | first }}" | |
port: "{{ item | json_query('keys(''container.NetworkSettings.Ports'')[0]') | split('/') | first | int }}" | |
username: "{{ item | json_query('container.Config.Env[?starts_with(@, `POSTGRES_USER`)]') | first | split('=') | last }}" | |
password: "{{ item | json_query('container.Config.Env[?starts_with(@, `POSTGRES_PASSWORD`)]') | first | split('=') | last }}" | |
set_fact: | |
postgresql_databases: "{{ postgresql_databases + [{ | |
'name': name, | |
'hostname': hostname, | |
'port': port | int, | |
'username': username, | |
'password': password | |
}] | list }}" | |
loop: "{{ postgresql_containers.results }}" | |
- name: Add databases to borgmatic_hooks | |
set_fact: | |
borgmatic_hooks: "{{ borgmatic_hooks | combine({ | |
'postgresql_databases': postgresql_databases, | |
'mysql_databases': mysql_databases | |
}) }}" | |
- name: Configure borgmatic | |
include_role: | |
name: borgbackup |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment