This commit is contained in:
dima
2024-12-08 22:11:48 +03:00
parent 66471a8a89
commit 08cc9fc069
16 changed files with 166 additions and 112 deletions

View File

@ -5,8 +5,6 @@
owner: postgres
group: postgres
mode: '0755'
tags:
- backup
- name: Perform database backup
command: >
@ -14,28 +12,22 @@
become_user: postgres
environment:
PGPASSWORD: '{{ postgres_password }}'
tags:
- backup
- name: Daily cron full backup
cron:
name: 'PostgreSQL daily full backup'
name: 'PostgreSQL daily full backup {{ postgres_user }}'
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'
name: 'PostgreSQL hourly incremental backup {{ postgres_user }}'
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
PGPASSWORD: '{{ postgres_password }}'

View File

@ -1,4 +1,4 @@
- name: Configure postgresql.conf with template
- name: Configure postgresql.conf
template:
src: postgresql.conf.j2
dest: /var/lib/pgsql/data/postgresql.conf
@ -6,10 +6,8 @@
group: postgres
mode: '0644'
notify: Restart PostgreSQL
tags:
- configure
- name: Configure pg_hba.conf with template
- name: Configure pg_hba.conf
template:
src: pg_hba.conf.j2
dest: /var/lib/pgsql/data/pg_hba.conf
@ -17,5 +15,3 @@
group: postgres
mode: '0644'
notify: Restart PostgreSQL
tags:
- configure

View File

@ -4,14 +4,15 @@
owner: '{{ postgres_user }}'
encoding: UTF8
state: present
tags:
- database
- name: Create contacts table in PostgreSQL
- name: Create contacts table
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));'
query: |
CREATE TABLE IF NOT EXISTS contacts (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
phone_number VARCHAR(15) UNIQUE
);
login_user: '{{ postgres_user }}'
login_password: '{{ postgres_password }}'
tags:
- database

View File

@ -2,13 +2,9 @@
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,11 @@
- name: Insert data into PostgreSQL database
community.postgresql.postgresql_query:
db: '{{ postgres_db }}'
query: |
INSERT INTO contacts (name, phone_number) VALUES
('dima', '123123123'),
('vlad', '8800525252'),
('denis', '77777777')
ON CONFLICT (phone_number) DO NOTHING;
login_user: '{{ postgres_user }}'
login_password: '{{ postgres_password }}'

View File

@ -1,7 +1,31 @@
- import_tasks: setup.yml
tags:
- setup
- import_tasks: initialize.yml
tags:
- init
- import_tasks: configure.yml
tags:
- configure
- import_tasks: users.yml
tags:
- users
- import_tasks: databases.yml
tags:
- database
- import_tasks: insert_data.yml
tags:
- insert
- import_tasks: open_firewall.yml
- import_tasks: backup.yml
tags:
- firewall
- import_tasks: backup.yml
tags:
- backup

View File

@ -4,11 +4,9 @@
permanent: true
state: enabled
become: true
tags:
- firewall
- name: Reload firewall using command
command: firewall-cmd --reload
- name: Reload firewalld
systemd:
name: firewalld
state: reloaded
become: true
tags:
- firewall

View File

@ -4,12 +4,8 @@
- postgresql-server
- postgresql-contrib
state: present
tags:
- setup
- name: Install python3-psycopg2
zypper:
name: python3-psycopg2
state: present
tags:
- setup

View File

@ -2,6 +2,4 @@
community.postgresql.postgresql_user:
name: '{{ postgres_user }}'
password: '{{ postgres_password }}'
state: present
tags:
- users
state: present