Files
computer-playbook/roles/svc-db-mariadb/tasks/01_init.yml
kevinveenbirkenbachandClaude Opus 5 bc7e28d8b0 fix(svc-db-mariadb): guard the container-path init writes against the restart
MariaDB's entrypoint initialises on a temporary server, shuts it down and starts the real one -- the same two-phase startup that killed the postgres sync in run 30699990912. The probes in 00_core.yml pass against the first server, and these two writes then ran with no retries at all in the swarm path.

A blank until is correct here, unlike the postgres case: both SQL files use IF NOT EXISTS, so a repeat is a no-op rather than an error.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-01 18:32:20 +02:00

68 lines
2.4 KiB
YAML

---
- block:
- name: "📁 Create database: {{ database_name }} (host via mysql_db)"
ansible.mysql.mysql_db:
name: "{{ database_name }}"
state: present
login_user: root
login_password: "{{ MARIADB_ROOT_PWD }}"
login_host: "{{ MARIADB_HOST }}"
login_port: "{{ MARIADB_PORT }}"
encoding: "{{ MARIADB_ENCODING }}"
collation: "{{ MARIADB_COLLATION }}"
config_file: ""
- name: "📁 Create database user: {{ database_username }} (host via mysql_user)"
ansible.mysql.mysql_user:
name: "{{ database_username }}"
password: "{{ database_password }}"
host: "%"
priv: "`{{ database_name }}`.*:ALL"
state: present
login_user: root
login_password: "{{ MARIADB_ROOT_PWD }}"
login_host: "{{ MARIADB_HOST }}"
login_port: "{{ MARIADB_PORT }}"
config_file: ""
when: MARIADB_EXPOSE_LOCAL | bool
- block:
- name: "📁 Create database: {{ database_name }} (container via mariadb CLI)"
ansible.builtin.shell:
cmd: >-
container exec -i
{{ MARIADB_CONTAINER_ADDRESS }}
sh -c 'mariadb -uroot -p"$MARIADB_ROOT_PASSWORD"'
stdin: |
SET @db_name := '{{ database_name }}';
SET @charset := '{{ MARIADB_ENCODING }}';
SET @collation := '{{ MARIADB_COLLATION }}';
{{ lookup('file', role_path ~ '/files/sql/create/database.sql') }}
register: mariadb_create_db
retries: 30
delay: 3
until: mariadb_create_db.rc == 0
delegate_to: "{{ STACK_HOST }}"
no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}"
changed_when: false
- name: "📁 Create database user + grant privileges (container via mariadb CLI)"
ansible.builtin.shell:
cmd: >-
container exec -i
{{ MARIADB_CONTAINER_ADDRESS }}
sh -c 'mariadb -uroot -p"$MARIADB_ROOT_PASSWORD"'
stdin: |
SET @db_user := '{{ database_username }}';
SET @db_pwd := '{{ database_password | replace("'", "''") }}';
SET @db_name := '{{ database_name }}';
{{ lookup('file', role_path ~ '/files/sql/create/user_grant.sql') }}
register: mariadb_create_user
retries: 30
delay: 3
until: mariadb_create_user.rc == 0
delegate_to: "{{ STACK_HOST }}"
no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}"
changed_when: false
when: not (MARIADB_EXPOSE_LOCAL | bool)