ver 2
This commit is contained in:
41
roles/postgresql/tasks/backup.yml
Normal file
41
roles/postgresql/tasks/backup.yml
Normal 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
|
21
roles/postgresql/tasks/configure.yml
Normal file
21
roles/postgresql/tasks/configure.yml
Normal 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
|
17
roles/postgresql/tasks/databases.yml
Normal file
17
roles/postgresql/tasks/databases.yml
Normal 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
|
14
roles/postgresql/tasks/initialize.yml
Normal file
14
roles/postgresql/tasks/initialize.yml
Normal 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
|
7
roles/postgresql/tasks/main.yml
Normal file
7
roles/postgresql/tasks/main.yml
Normal 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
|
14
roles/postgresql/tasks/open_firewall.yml
Normal file
14
roles/postgresql/tasks/open_firewall.yml
Normal 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
|
15
roles/postgresql/tasks/setup.yml
Normal file
15
roles/postgresql/tasks/setup.yml
Normal 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
|
7
roles/postgresql/tasks/users.yml
Normal file
7
roles/postgresql/tasks/users.yml
Normal file
@ -0,0 +1,7 @@
|
||||
- name: Create PostgreSQL user
|
||||
community.postgresql.postgresql_user:
|
||||
name: '{{ postgres_user }}'
|
||||
password: '{{ postgres_password }}'
|
||||
state: present
|
||||
tags:
|
||||
- users
|
Reference in New Issue
Block a user