From 8149601d66e8dbf71d50e181de04c029d93a6bff Mon Sep 17 00:00:00 2001 From: chatoasis Date: Mon, 21 Dec 2020 09:16:16 +0800 Subject: [PATCH] added section to import database with a password --- .../matrix-postgres/tasks/import_postgres.yml | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/roles/matrix-postgres/tasks/import_postgres.yml b/roles/matrix-postgres/tasks/import_postgres.yml index 33d98691d..82ec0ab05 100644 --- a/roles/matrix-postgres/tasks/import_postgres.yml +++ b/roles/matrix-postgres/tasks/import_postgres.yml @@ -60,6 +60,25 @@ # role (`matrix_postgres_connection_username`) and database (`matrix_postgres_db_name`). # The dump most likely contains those same entries and would try to re-create them, leading to errors. # We need to skip over those lines. +- name: Generate Postgres database import command + set_fact: + matrix_postgres_import_command: >- + {{ matrix_host_command_docker }} run --rm --name matrix-postgres-import + --log-driver=none + --user={{ matrix_user_uid }}:{{ matrix_user_gid }} + --cap-drop=ALL + --network={{ matrix_docker_network }} + --env-file={{ matrix_postgres_base_path }}/env-postgres-psql + --mount type=bind,src={{ server_path_postgres_dump }},dst=/{{ server_path_postgres_dump|basename }},ro + --entrypoint=/bin/sh + {{ matrix_postgres_docker_image_latest }} + -c "export PGPASSWORD='{{ matrix_postgres_connection_password }}'; cat /{{ server_path_postgres_dump|basename }} | + {{ 'gunzip |' if server_path_postgres_dump.endswith('.gz') else '' }} + grep -vE '^CREATE ROLE {{ matrix_postgres_connection_username }}' | + grep -vE '^CREATE DATABASE {{ matrix_postgres_db_name }}' | + psql -v ON_ERROR_STOP=1 -h matrix-postgres" + when: (matrix_postgres_connection_password is defined) and (matrix_postgres_connection_password|length > 0) + - name: Generate Postgres database import command set_fact: matrix_postgres_import_command: >- @@ -77,6 +96,8 @@ grep -vE '^CREATE ROLE {{ matrix_postgres_connection_username }}' | grep -vE '^CREATE DATABASE {{ matrix_postgres_db_name }}' | psql -v ON_ERROR_STOP=1 -h matrix-postgres" + when: matrix_postgres_connection_password is not defined + # This is a hack. # See: https://ansibledaily.com/print-to-standard-output-without-escaping/