This commit is contained in:
dima
2024-11-11 00:17:33 +03:00
parent 015d96995e
commit 66471a8a89
17 changed files with 252 additions and 137 deletions

View File

@ -0,0 +1,4 @@
- name: Restart PostgreSQL
service:
name: postgresql
state: restarted

View File

@ -0,0 +1,41 @@
- name: Create backup directory
file:
path: '{{ backup_dir }}'
state: directory
owner: postgres
group: postgres
mode: '0755'
tags:
- backup
- name: Perform database backup
command: >
pg_dump -U {{ postgres_user }} -F c -f "{{ backup_dir }}/db_backup_{{ postgres_db }}_{{ ansible_date_time.iso8601 }}.sql" {{ postgres_db }}
become_user: postgres
environment:
PGPASSWORD: '{{ postgres_password }}'
tags:
- backup
- name: Daily cron full backup
cron:
name: 'PostgreSQL daily full backup'
user: postgres
minute: '0'
hour: '1'
job: "pg_dump -U {{ postgres_user }} -F c {{ postgres_db }} > {{ backup_dir }}/full_db_backup_{{ postgres_db }}_$(date +\\%F-\\%H-%M).sql"
environment:
PGPASSWORD: '{{ postgres_password }}'
tags:
- backup
- name: Hourly cron incremental backup
cron:
name: 'PostgreSQL hourly incremental backup'
user: postgres
minute: '0'
job: "pg_dump -U {{ postgres_user }} -F c --data-only --file=\"{{ backup_dir }}/incremental_db_backup_{{ postgres_db }}_$(date +\\%F-\\%H-%M).sql\" {{ postgres_db }}"
environment:
PGPASSWORD: '{{ postgres_password }}'
tags:
- backup

View File

@ -0,0 +1,21 @@
- name: Configure postgresql.conf with template
template:
src: postgresql.conf.j2
dest: /var/lib/pgsql/data/postgresql.conf
owner: postgres
group: postgres
mode: '0644'
notify: Restart PostgreSQL
tags:
- configure
- name: Configure pg_hba.conf with template
template:
src: pg_hba.conf.j2
dest: /var/lib/pgsql/data/pg_hba.conf
owner: postgres
group: postgres
mode: '0644'
notify: Restart PostgreSQL
tags:
- configure

View File

@ -0,0 +1,17 @@
- name: Create PostgreSQL database
community.postgresql.postgresql_db:
name: '{{ postgres_db }}'
owner: '{{ postgres_user }}'
encoding: UTF8
state: present
tags:
- database
- name: Create contacts table in PostgreSQL
community.postgresql.postgresql_query:
db: '{{ postgres_db }}'
query: 'CREATE TABLE IF NOT EXISTS contacts (id SERIAL PRIMARY KEY, name VARCHAR(100), phone_number VARCHAR(15));'
login_user: '{{ postgres_user }}'
login_password: '{{ postgres_password }}'
tags:
- database

View File

@ -0,0 +1,14 @@
- name: PostgreSQL initdb
command: sudo -u postgres initdb -D /var/lib/pgsql/data
args:
creates: /var/lib/pgsql/data/PG_VERSION
tags:
- init
- name: Systemctl start and enable PostgreSQL
service:
name: postgresql
state: started
enabled: true
tags:
- init

View File

@ -0,0 +1,7 @@
- import_tasks: setup.yml
- import_tasks: initialize.yml
- import_tasks: configure.yml
- import_tasks: users.yml
- import_tasks: databases.yml
- import_tasks: open_firewall.yml
- import_tasks: backup.yml

View File

@ -0,0 +1,14 @@
- name: Open PostgreSQL port in firewall
firewalld:
port: 5432/tcp
permanent: true
state: enabled
become: true
tags:
- firewall
- name: Reload firewall using command
command: firewall-cmd --reload
become: true
tags:
- firewall

View File

@ -0,0 +1,15 @@
- name: Install PostgreSQL packages
zypper:
name:
- postgresql-server
- postgresql-contrib
state: present
tags:
- setup
- name: Install python3-psycopg2
zypper:
name: python3-psycopg2
state: present
tags:
- setup

View File

@ -0,0 +1,7 @@
- name: Create PostgreSQL user
community.postgresql.postgresql_user:
name: '{{ postgres_user }}'
password: '{{ postgres_password }}'
state: present
tags:
- users

View File

@ -0,0 +1,3 @@
{% for entry in postgres_hba_entries %}
{{ entry.type }} {{ entry.database }} {{ entry.user }} {{ entry.address }} {{ entry.method }}
{% endfor %}

View File

@ -0,0 +1,12 @@
listen_addresses = '{{ postgres_listen_addresses | default("*") }}'
port = {{ postgres_port | default(5432) }}
max_connections = {{ postgres_max_connections | default(100) }}
shared_buffers = {{ postgres_shared_buffers | default("128MB") }}
effective_cache_size = {{ postgres_effective_cache_size | default("4GB") }}
maintenance_work_mem = {{ postgres_maintenance_work_mem | default("64MB") }}
checkpoint_completion_target = {{ postgres_checkpoint_completion_target | default(0.7) }}
wal_buffers = {{ postgres_wal_buffers | default("16MB") }}
default_statistics_target = {{ postgres_default_statistics_target | default(100) }}

View File

@ -0,0 +1,11 @@
postgres_listen_addresses: '*'
postgres_port: 5432
postgres_hba_entries:
- { type: 'host', database: 'all', user: 'all', address: '0.0.0.0/0', method: 'md5' }
- { type: 'local', database: 'all', user: 'all', address: '', method: 'trust' }
backup_dir: "/var/lib/pgsql/backups"
postgres_user: "postgres"
postgres_password: "your_password"
postgres_db: "your_database"