64 lines
1.6 KiB
YAML
64 lines
1.6 KiB
YAML
- name: Install PostgreSQL packages
|
|
apt:
|
|
name:
|
|
- postgresql
|
|
- postgresql-contrib
|
|
update_cache: yes
|
|
|
|
# --- Dynamiczne wykrywanie wersji PostgreSQL ---
|
|
- name: Detect installed PostgreSQL version
|
|
find:
|
|
paths: /etc/postgresql
|
|
file_type: directory
|
|
depth: 1
|
|
register: pg_version_dirs
|
|
|
|
- name: Set PostgreSQL version fact
|
|
set_fact:
|
|
pg_version: "{{ pg_version_dirs.files | map(attribute='path') | map('basename') | sort | last }}"
|
|
|
|
- name: Define PostgreSQL cluster path
|
|
set_fact:
|
|
pg_cluster_path: "/etc/postgresql/{{ pg_version }}/main"
|
|
|
|
# --- Stop cluster before config ---
|
|
- name: Stop PostgreSQL cluster
|
|
shell: "pg_ctlcluster {{ pg_version }} main stop"
|
|
args:
|
|
warn: false
|
|
ignore_errors: true
|
|
|
|
# --- Configure PostgreSQL ---
|
|
- name: postgresql.conf
|
|
template:
|
|
src: postgresql.conf.j2
|
|
dest: "{{ pg_cluster_path }}/postgresql.conf"
|
|
|
|
- name: pg_hba.conf
|
|
template:
|
|
src: pg_hba.conf.j2
|
|
dest: "{{ pg_cluster_path }}/pg_hba.conf"
|
|
|
|
# --- Ensure cluster exists and started ---
|
|
- name: Ensure PostgreSQL cluster exists
|
|
shell: |
|
|
if ! pg_lsclusters | grep -q " {{ pg_version }} main "; then
|
|
pg_createcluster {{ pg_version }} main --start
|
|
else
|
|
pg_ctlcluster {{ pg_version }} main start
|
|
fi
|
|
|
|
# --- Wait for socket to be ready ---
|
|
- name: Wait for PostgreSQL socket
|
|
wait_for:
|
|
path: "/var/run/postgresql/{{ pg_version }}-main/.s.PGSQL.5432"
|
|
timeout: 60
|
|
|
|
# --- Install Python PostgreSQL libraries ---
|
|
- name: Install python postgres libraries
|
|
apt:
|
|
name:
|
|
- python3-psycopg2
|
|
state: present
|
|
update_cache: yes
|