126 lines
3.8 KiB
YAML
126 lines
3.8 KiB
YAML
- name: Install PostgreSQL and Backup
|
|
hosts: all
|
|
become: true
|
|
vars:
|
|
postgres_user: '{{ postgres_user }}'
|
|
postgres_password: '{{ postgres_password }}'
|
|
postgres_db: '{{ postgres_db }}'
|
|
backup_dir: '{{ backup_dir }}'
|
|
|
|
tasks:
|
|
- name: Update zypper
|
|
command: zypper refresh
|
|
register: zypper_refresh
|
|
changed_when: "'Refreshing' in zypper_refresh.stdout"
|
|
|
|
- name: Update System
|
|
zypper:
|
|
name: '*'
|
|
state: latest
|
|
when: zypper_refresh.changed
|
|
|
|
- name: Update PostgreSQL package
|
|
zypper:
|
|
name:
|
|
- postgresql-server
|
|
- postgresql-contrib
|
|
state: present
|
|
|
|
- name: PostgreSQL initdb
|
|
command: sudo -u postgres initdb -D /var/lib/pgsql/data
|
|
args:
|
|
creates: /var/lib/pgsql/data/PG_VERSION
|
|
|
|
- name: Systemctl start and enable PostgreSQL
|
|
service:
|
|
name: postgresql
|
|
state: started
|
|
enabled: true
|
|
|
|
- name: python3-psycopg2 install
|
|
zypper:
|
|
name: python3-psycopg2
|
|
state: present
|
|
|
|
- name: Change listen_addresses in postgresql.conf
|
|
lineinfile:
|
|
path: /var/lib/pgsql/data/postgresql.conf
|
|
regexp: '^#?listen_addresses\\s*='
|
|
line: "listen_addresses = '*'"
|
|
notify: Restart PostgreSQL
|
|
|
|
- name: Change pg_hba.conf
|
|
lineinfile:
|
|
path: /var/lib/pgsql/data/pg_hba.conf
|
|
regexp: '^host\\s+all\\s+all\\s+0\\.0\\.0\\.0/0\\s+md5'
|
|
line: 'host all all 0.0.0.0/0 md5'
|
|
notify: Restart PostgreSQL
|
|
|
|
- name: Create User PostgreSQL
|
|
community.postgresql.postgresql_user:
|
|
name: '{{ postgres_user }}'
|
|
password: '{{ postgres_password }}'
|
|
state: present
|
|
|
|
- name: Create Base PostgreSQL
|
|
community.postgresql.postgresql_db:
|
|
name: '{{ postgres_db }}'
|
|
owner: '{{ postgres_user }}'
|
|
encoding: UTF8
|
|
state: present
|
|
|
|
- name: Create a 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));'
|
|
login_user: '{{ postgres_user }}'
|
|
login_password: '{{ postgres_password }}'
|
|
|
|
- name: Open firewall 5432 port
|
|
command: firewall-cmd --add-port=5432/tcp --permanent
|
|
become: true
|
|
|
|
- name: Restart Firewall
|
|
command: firewall-cmd --reload
|
|
become: true
|
|
|
|
- name: Create backup directory
|
|
file:
|
|
path: '{{ backup_dir }}'
|
|
state: directory
|
|
owner: postgres
|
|
group: postgres
|
|
mode: '0755'
|
|
|
|
- name: PostgreSQL database backup
|
|
become_user: postgres
|
|
command: 'pg_dump -U {{ postgres_user }} -F c -f "{{ backup_dir }}/db_backup_{{ postgres_db }}_{{ ansible_date_time.iso8601 }}.sql" {{ postgres_db }}'
|
|
environment:
|
|
PGPASSWORD: '{{ postgres_password }}'
|
|
|
|
- name: Create cron for daily 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 }}'
|
|
|
|
- name: Create cron backup script
|
|
cron:
|
|
name: 'PostgreSQL hourly backup'
|
|
user: postgres
|
|
minute: '0'
|
|
hour: '*'
|
|
job: 'pg_dump -U {{ postgres_user }} -F c {{ postgres_db }} > {{ backup_dir }}/db_backup_{{ postgres_db }}_$(date +\\\\%F-\\\\%H-%M).sql'
|
|
environment:
|
|
PGPASSWORD: '{{ postgres_password }}'
|
|
|
|
handlers:
|
|
- name: Restart PostgreSQL
|
|
service:
|
|
name: postgresql
|
|
state: restarted
|