mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-08-29 15:06:26 +02:00
Implemented helper role docker-central-database and refactored code
This commit is contained in:
71
roles/docker-central-database/README.md
Normal file
71
roles/docker-central-database/README.md
Normal file
@@ -0,0 +1,71 @@
|
||||
# Database Setup Role 🚀
|
||||
|
||||
This Ansible role provides the necessary tasks, files, templates, and variables to set up databases in your Docker Compose environment. It is essential for configuring your application's database, whether using a local or a central instance of **MariaDB** or **PostgreSQL**.
|
||||
|
||||
---
|
||||
|
||||
## Overview 🔍
|
||||
|
||||
- **Database Variables**
|
||||
Defined in [./vars/database.yml](./vars/database.yml), these variables include:
|
||||
- `database_instance`
|
||||
- `database_host`
|
||||
- `database_name`
|
||||
- `database_username`
|
||||
- `database_port`
|
||||
- `database_env`
|
||||
|
||||
- **Tasks**
|
||||
Located in [./tasks/main.yml](./tasks/main.yml), the tasks perform the following:
|
||||
- Include the Docker Compose role.
|
||||
- Load database variables.
|
||||
- Create the environment file for the chosen database from a template.
|
||||
- Optionally create a central database (if enabled).
|
||||
|
||||
- **Templates**
|
||||
- **Environment Files:**
|
||||
- [PostgreSQL Environment Template](./templates/env/postgres.env.j2)
|
||||
- [MariaDB Environment Template](./templates/env/mariadb.env.j2)
|
||||
- **Service Files:**
|
||||
- [MariaDB Service Template](./templates/services/mariadb.yml.j2)
|
||||
- [PostgreSQL Service Template](./templates/services/postgres.yml.j2)
|
||||
|
||||
---
|
||||
|
||||
## Usage 📋
|
||||
|
||||
To use this role, include it in your playbook as follows:
|
||||
|
||||
```yaml
|
||||
- hosts: all
|
||||
roles:
|
||||
- your_database_role_name
|
||||
```
|
||||
|
||||
When executed, the role will:
|
||||
|
||||
1. Load database configuration variables.
|
||||
2. Generate the appropriate environment file for the database.
|
||||
3. Incorporate the Docker Compose routines.
|
||||
4. Create a central database if `enable_central_database` is set to `true`.
|
||||
|
||||
---
|
||||
|
||||
## Author
|
||||
|
||||
Developed by [Kevin Veen-Birkenbach](https://www.veen.world/) 💻🌐
|
||||
|
||||
---
|
||||
|
||||
## Acknowledgments & ChatGPT Conversations 🤖💬
|
||||
|
||||
This role was created with the assistance of ChatGPT. The following ChatGPT conversations helped shape the design and implementation of this role:
|
||||
|
||||
- https://chatgpt.com/share/67a23d18-fb54-800f-983c-d6d00752b0b4
|
||||
- https://chatgpt.com/share/67a244bb-11e4-800f-980f-5ef0e8b109d7
|
||||
|
||||
Feel free to explore these discussions for insights into design decisions and implementation details.
|
||||
|
||||
---
|
||||
|
||||
Happy automating! 🎉
|
19
roles/docker-central-database/tasks/main.yml
Normal file
19
roles/docker-central-database/tasks/main.yml
Normal file
@@ -0,0 +1,19 @@
|
||||
# Docker Routines
|
||||
- name: "include docker-compose role"
|
||||
include_role:
|
||||
name: docker-compose
|
||||
|
||||
# Database Routines
|
||||
- name: "load variables from {{ role_path }}/vars/database.yml for whole play"
|
||||
include_vars: "{{ role_path }}/vars/database.yml"
|
||||
|
||||
- name: "create {{database_env}}"
|
||||
template:
|
||||
src: "env/{{database_type}}.env.j2"
|
||||
dest: "{{database_env}}"
|
||||
notify: docker compose project build and setup
|
||||
|
||||
- name: create central database
|
||||
include_role:
|
||||
name: docker-{{database_type}}
|
||||
when: enable_central_database | bool
|
5
roles/docker-central-database/templates/env/mariadb.env.j2
vendored
Normal file
5
roles/docker-central-database/templates/env/mariadb.env.j2
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
MYSQL_DATABASE="{{database_name}}"
|
||||
MYSQL_USER="{{database_username}}"
|
||||
MYSQL_PASSWORD="{{database_password}}"
|
||||
MYSQL_ROOT_PASSWORD="{{database_password}}"
|
||||
MARIADB_AUTO_UPGRADE="1"
|
4
roles/docker-central-database/templates/env/postgres.env.j2
vendored
Normal file
4
roles/docker-central-database/templates/env/postgres.env.j2
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
POSTGRES_PASSWORD={{database_password}}
|
||||
POSTGRES_USER={{database_username}}
|
||||
POSTGRES_DB={{database_name}}
|
||||
POSTGRES_INITDB_ARGS=--encoding=UTF8 --locale=C
|
@@ -0,0 +1,22 @@
|
||||
# This template needs to be included in docker-compose.yml, which depend on a mariadb database
|
||||
{% if not enable_central_database | bool %}
|
||||
database:
|
||||
container_name: {{application_id}}-database
|
||||
logging:
|
||||
driver: journald
|
||||
image: mariadb
|
||||
restart: {{docker_restart_policy}}
|
||||
env_file:
|
||||
- mein_env_file.env
|
||||
command: "--transaction-isolation=READ-COMMITTED --binlog-format=ROW"
|
||||
volumes:
|
||||
- database:/var/lib/mysql
|
||||
healthcheck:
|
||||
test: "/usr/bin/mariadb --user={{database_username}} --password={{database_password}} --execute \"SHOW DATABASES;\""
|
||||
interval: 3s
|
||||
timeout: 1s
|
||||
retries: 5
|
||||
networks:
|
||||
- default
|
||||
{% endif %}
|
||||
{{ "\n" }}
|
@@ -0,0 +1,21 @@
|
||||
# This template needs to be included in docker-compose.yml, which depend on a postgres database
|
||||
{% if not enable_central_database | bool %}
|
||||
database:
|
||||
image: postgres:{{applications.postgres.database_version}}-alpine
|
||||
container_name: {{application_id}}-database
|
||||
env_file:
|
||||
- {{database_env}}
|
||||
restart: {{docker_restart_policy}}
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U {{database_name}}"]
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 6
|
||||
volumes:
|
||||
- type: volume
|
||||
source: database
|
||||
target: /var/lib/postgresql/data
|
||||
networks:
|
||||
- default
|
||||
{% endif %}
|
||||
{{ "\n" }}
|
6
roles/docker-central-database/vars/database.yml
Normal file
6
roles/docker-central-database/vars/database.yml
Normal file
@@ -0,0 +1,6 @@
|
||||
database_instance: "{{ 'central-' + database_type if enable_central_database | bool else application_id }}"
|
||||
database_host: "{{ 'central-' + database_type if enable_central_database | bool else 'database' }}"
|
||||
database_name: "{{ application_id }}"
|
||||
database_username: "{{ application_id }}"
|
||||
database_port: "{{ 3306 if database_type == 'mariadb' else 5432 }}"
|
||||
database_env: "{{docker_compose.directories.env}}{{database_type}}.env"
|
Reference in New Issue
Block a user