2023-12-29 23:58:57 +01:00
|
|
|
- name: Create Docker network for PostgreSQL
|
|
|
|
docker_network:
|
2024-01-05 20:22:34 +01:00
|
|
|
name: central_postgres
|
2023-12-29 23:58:57 +01:00
|
|
|
state: present
|
|
|
|
when: run_once_docker_postgres is not defined
|
|
|
|
|
|
|
|
- name: Install PostgreSQL
|
|
|
|
docker_container:
|
2024-01-04 20:57:02 +01:00
|
|
|
name: central-postgres
|
|
|
|
image: "postgres:{{database_version}}"
|
2023-12-29 23:58:57 +01:00
|
|
|
detach: yes
|
|
|
|
env:
|
|
|
|
POSTGRES_PASSWORD: "{{ central_postgres_password }}"
|
2023-12-31 11:14:18 +01:00
|
|
|
POSTGRES_INITDB_ARGS: "--encoding=UTF8 --locale=C" # Necessary for docker-matrix
|
2023-12-29 23:58:57 +01:00
|
|
|
networks:
|
2024-01-05 20:22:34 +01:00
|
|
|
- name: central_postgres
|
2023-12-29 23:58:57 +01:00
|
|
|
published_ports:
|
|
|
|
- "127.0.0.1:5432:5432"
|
2024-01-04 20:57:02 +01:00
|
|
|
volumes:
|
|
|
|
- central_postgres_database:/var/lib/postgresql/data
|
2024-01-12 20:57:58 +01:00
|
|
|
restart_policy: "{{docker_restart_policy}}"
|
2024-01-05 22:00:59 +01:00
|
|
|
healthcheck:
|
|
|
|
test: ["CMD-SHELL", "pg_isready -U postgres"]
|
|
|
|
interval: 10s
|
|
|
|
timeout: 5s
|
|
|
|
retries: 5
|
|
|
|
start_period: 30s
|
2024-01-15 01:00:59 +01:00
|
|
|
register: setup_postgres_container_result
|
2023-12-29 23:58:57 +01:00
|
|
|
when: run_once_docker_postgres is not defined
|
|
|
|
|
2024-01-15 01:00:59 +01:00
|
|
|
- name: wait for database
|
|
|
|
pause:
|
|
|
|
seconds: "{{pause_duration}}"
|
|
|
|
when: setup_postgres_container_result.changed and run_once_docker_postgres is not defined
|
2023-12-31 11:14:18 +01:00
|
|
|
|
2024-01-04 20:57:02 +01:00
|
|
|
- name: install python-psycopg2
|
|
|
|
pacman:
|
|
|
|
name: python-psycopg2
|
|
|
|
state: present
|
|
|
|
when: run_once_docker_postgres is not defined
|
|
|
|
|
2024-01-06 14:32:49 +01:00
|
|
|
- name: "Create database: {{ database_name }}"
|
2023-12-31 11:14:18 +01:00
|
|
|
postgresql_db:
|
2024-01-06 14:32:49 +01:00
|
|
|
name: "{{ database_name }}"
|
2023-12-31 11:14:18 +01:00
|
|
|
state: present
|
|
|
|
login_user: postgres
|
|
|
|
login_password: "{{ central_postgres_password }}"
|
|
|
|
login_host: 127.0.0.1
|
|
|
|
login_port: 5432
|
|
|
|
|
2024-01-04 20:57:02 +01:00
|
|
|
- name: "Create database user: {{ database_username }}"
|
2023-12-31 11:14:18 +01:00
|
|
|
postgresql_user:
|
2024-01-04 20:57:02 +01:00
|
|
|
name: "{{ database_username }}"
|
|
|
|
password: "{{ database_password }}"
|
2024-01-06 14:32:49 +01:00
|
|
|
db: "{{ database_name }}"
|
2024-01-04 20:57:02 +01:00
|
|
|
state: present
|
|
|
|
login_user: postgres
|
|
|
|
login_password: "{{ central_postgres_password }}"
|
|
|
|
login_host: 127.0.0.1
|
|
|
|
login_port: 5432
|
|
|
|
|
|
|
|
- name: "Set privileges for database user: {{ database_username }}"
|
|
|
|
postgresql_privs:
|
2024-01-06 14:32:49 +01:00
|
|
|
db: "{{ database_name }}"
|
2024-01-04 20:57:02 +01:00
|
|
|
role: "{{ database_username }}"
|
|
|
|
objs: ALL_IN_SCHEMA
|
|
|
|
privs: ALL
|
|
|
|
type: table
|
|
|
|
state: present
|
|
|
|
login_user: postgres
|
|
|
|
login_password: "{{ central_postgres_password }}"
|
|
|
|
login_host: 127.0.0.1
|
|
|
|
login_port: 5432
|
|
|
|
|
|
|
|
- name: Grant all privileges at the database level
|
|
|
|
postgresql_privs:
|
2024-01-06 14:32:49 +01:00
|
|
|
db: "{{ database_name }}"
|
2024-01-04 20:57:02 +01:00
|
|
|
role: "{{ database_username }}"
|
|
|
|
privs: ALL
|
|
|
|
type: database
|
|
|
|
state: present
|
|
|
|
login_user: postgres
|
|
|
|
login_password: "{{ central_postgres_password }}"
|
|
|
|
login_host: 127.0.0.1
|
|
|
|
login_port: 5432
|
|
|
|
|
|
|
|
- name: Grant all privileges on all tables in the public schema
|
|
|
|
postgresql_privs:
|
2024-01-06 14:32:49 +01:00
|
|
|
db: "{{ database_name }}"
|
2024-01-04 20:57:02 +01:00
|
|
|
role: "{{ database_username }}"
|
|
|
|
objs: ALL_IN_SCHEMA
|
|
|
|
privs: ALL
|
|
|
|
type: table
|
|
|
|
schema: public
|
2023-12-31 11:14:18 +01:00
|
|
|
state: present
|
|
|
|
login_user: postgres
|
|
|
|
login_password: "{{ central_postgres_password }}"
|
|
|
|
login_host: 127.0.0.1
|
|
|
|
login_port: 5432
|
|
|
|
|
2024-01-04 20:57:02 +01:00
|
|
|
- name: Set comprehensive privileges for user on public schema
|
|
|
|
postgresql_query:
|
2024-01-06 14:32:49 +01:00
|
|
|
db: "{{ database_name }}"
|
2024-01-04 20:57:02 +01:00
|
|
|
login_user: postgres
|
|
|
|
login_password: "{{ central_postgres_password }}"
|
|
|
|
login_host: 127.0.0.1
|
|
|
|
login_port: 5432
|
|
|
|
query: |
|
|
|
|
GRANT USAGE ON SCHEMA public TO {{ database_username }};
|
|
|
|
GRANT CREATE ON SCHEMA public TO {{ database_username }};
|
|
|
|
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL PRIVILEGES ON TABLES TO {{ database_username }};
|
|
|
|
|
2023-12-29 23:58:57 +01:00
|
|
|
- name: Run the docker_postgres tasks once
|
|
|
|
set_fact:
|
|
|
|
run_once_docker_postgres: true
|
|
|
|
when: run_once_docker_postgres is not defined
|