Files
matrix-docker-ansible-deploy/molecule-shared/tasks/postgres.yml
T
Slavi PantaleevandClaude Opus 5 db4eb94058 Set PGDATA explicitly in the Molecule Postgres container, like the role does
The scenarios worked around Postgres 18's data layout change by mounting the
tmpfs at /var/lib/postgresql. The postgres role solved the same problem
differently and better, when it bit us there: it sets PGDATA explicitly, and to
/data rather than anywhere beneath /var/lib/postgresql, because that path is a
VOLUME in the official image and nothing can be mounted under it.

Doing the same here means the scenarios exercise the layout the role actually
deploys, and that a future image changing its own default cannot move the data
directory out from under them.

Ref: https://github.com/docker-library/postgres/pull/1259

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SEH3vxYSQ5SV4N5z61eyGT
2026-08-27 18:02:53 +03:00

79 lines
3.3 KiB
YAML

# SPDX-FileCopyrightText: 2026 Slavi Pantaleev
#
# SPDX-License-Identifier: AGPL-3.0-or-later
---
# Stands up Postgres on a container network, for scenarios whose role has a database.
#
# Include from a scenario's prepare.yml:
#
# - name: Ensure Postgres is running
# ansible.builtin.include_tasks:
# file: "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/../../../molecule-shared/tasks/postgres.yml"
# vars:
# molecule_shared_postgres_network: "{{ <role>_container_network }}"
# molecule_shared_postgres_database: matrix_<role>
# molecule_shared_postgres_username: matrix_<role>
# molecule_shared_postgres_password: <something>
#
# The role should then be pointed at it with its own `_database_engine: postgres` and
# `_database_hostname: matrix-postgres-molecule` (or whatever hostname is passed here).
#
# This is what the playbook does in a real run: `group_vars/matrix_servers` selects postgres
# whenever postgres is enabled, which is the default. sqlite is the path almost nobody uses.
- name: Ensure a previous Postgres is gone
ansible.builtin.command:
argv:
- docker
- rm
- --force
- "{{ molecule_shared_postgres_name | default('matrix-postgres-molecule') }}"
register: molecule_shared_postgres_removal
changed_when: molecule_shared_postgres_removal.rc == 0
failed_when: false
- name: Ensure Postgres is running
ansible.builtin.command:
argv:
- docker
- run
- --detach
- --name={{ molecule_shared_postgres_name | default('matrix-postgres-molecule') }}
- --network={{ molecule_shared_postgres_network }}
- --network-alias={{ molecule_shared_postgres_hostname | default('matrix-postgres-molecule') }}
- --env=POSTGRES_DB={{ molecule_shared_postgres_database }}
- --env=POSTGRES_USER={{ molecule_shared_postgres_username }}
- --env=POSTGRES_PASSWORD={{ molecule_shared_postgres_password }}
# PGDATA is set explicitly, and to /data rather than anywhere under
# /var/lib/postgresql, for the same reasons the postgres role does it: v18 changed the
# default to a versioned subdirectory, and /var/lib/postgresql is a VOLUME in the image,
# so nothing can be mounted beneath it. Setting it explicitly also means a future image
# changing its own default cannot move the data out from under this.
#
# A tmpfs, because the database is thrown away with the container and not writing it to
# the overlay filesystem is faster.
- --env=PGDATA=/data
- --tmpfs=/data
- "{{ molecule_shared_image_postgres }}"
register: molecule_shared_postgres_start
changed_when: molecule_shared_postgres_start.rc == 0
# `pg_isready` needs the username: it defaults to the OS user of whoever runs it, which inside
# this container is `postgres` and may not be the role the scenario created.
- name: Wait for Postgres to accept connections
ansible.builtin.command:
argv:
- docker
- exec
- "{{ molecule_shared_postgres_name | default('matrix-postgres-molecule') }}"
- pg_isready
- --username={{ molecule_shared_postgres_username }}
- --dbname={{ molecule_shared_postgres_database }}
- --quiet
register: molecule_shared_postgres_ready
changed_when: false
until: molecule_shared_postgres_ready.rc == 0
retries: 30
delay: 2