added opening of ports, created an ansible playbook for backup and integrated into Jenkens pipeline

This commit is contained in:
dima 2024-10-20 13:31:40 +03:00
parent 803c80efff
commit 67166656f3
3 changed files with 152 additions and 97 deletions

37
backup_postgresql.yml Normal file
View File

@ -0,0 +1,37 @@
---
- name: Backup PostgreSQL and set up cron job
hosts: postgres_servers
become: true
vars:
backup_dir: "/var/backups/postgresql"
tasks:
- name: Ensure backup directory exists
file:
path: "{{ backup_dir }}"
state: directory
owner: postgres
group: postgres
mode: '0755'
- name: Perform 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 job for hourly PostgreSQL backup
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 }}"

View File

@ -1,87 +1,88 @@
--- ---
- name: Install PostgreSQL on openSUSE Leap - name: Install PostgreSQL on openSUSE Leap
hosts: postgres_servers hosts: postgres_servers
become: yes become: yes
vars: vars:
postgres_user: "{{ postgres_user }}" postgres_user: "{{ postgres_user }}"
postgres_password: "{{ postgres_password }}" postgres_password: "{{ postgres_password }}"
postgres_db: "{{ postgres_db }}" postgres_db: "{{ postgres_db }}"
tasks:
- name: Update zypper tasks:
command: zypper refresh
register: zypper_refresh
changed_when: "'Refreshing' in zypper_refresh.stdout"
- name: Update System - name: Update zypper
zypper: command: zypper refresh
name: '*' register: zypper_refresh
state: latest changed_when: "'Refreshing' in zypper_refresh.stdout"
when: zypper_refresh.changed
- name: Update PostgreSQL package - name: Update System
zypper: zypper:
name: name: '*'
- postgresql-server state: latest
- postgresql-contrib when: zypper_refresh.changed
state: present
- name: PostgreSQL initdb - name: Update PostgreSQL package
command: sudo -u postgres initdb -D /var/lib/pgsql/data zypper:
args: name:
creates: /var/lib/pgsql/data/PG_VERSION - postgresql-server
- postgresql-contrib
state: present
- name: Systemctl start and enable PostgreSQL - name: PostgreSQL initdb
service: command: sudo -u postgres initdb -D /var/lib/pgsql/data
name: postgresql args:
state: started creates: /var/lib/pgsql/data/PG_VERSION
enabled: yes
- name: python3-psycopg2 install - name: Systemctl start and enable PostgreSQL
zypper: service:
name: python3-psycopg2 name: postgresql
state: present state: started
enabled: yes
- name: Change listen_addresses in postgresql.conf - name: python3-psycopg2 install
lineinfile: zypper:
path: /var/lib/pgsql/data/postgresql.conf name: python3-psycopg2
regexp: '^#?listen_addresses\s*=' state: present
line: "listen_addresses = '*'"
notify: Restart PostgreSQL
- name: Change pg_hba.conf - name: Change listen_addresses in postgresql.conf
lineinfile: lineinfile:
path: /var/lib/pgsql/data/pg_hba.conf path: /var/lib/pgsql/data/postgresql.conf
regexp: '^host\s+all\s+all\s+0\.0\.0\.0/0\s+md5' regexp: '^#?listen_addresses\s*='
line: "host all all 0.0.0.0/0 md5" line: "listen_addresses = '*'"
notify: Restart PostgreSQL notify: Restart PostgreSQL
- name: Create User PostgreSQL - name: Change pg_hba.conf
community.postgresql.postgresql_user: lineinfile:
name: "{{ postgres_user }}" path: /var/lib/pgsql/data/pg_hba.conf
password: "{{ postgres_password }}" regexp: '^host\s+all\s+all\s+0\.0\.0\.0/0\s+md5'
state: present line: "host all all 0.0.0.0/0 md5"
notify: Restart PostgreSQL
- name: Create Base PostgreSQL - name: Create User PostgreSQL
community.postgresql.postgresql_db: community.postgresql.postgresql_user:
name: "{{ postgres_db }}" name: "{{ postgres_user }}"
owner: "{{ postgres_user }}" password: "{{ postgres_password }}"
encoding: UTF8 state: present
state: present
- name: Create table "contacts" - name: Create Base PostgreSQL
community.postgresql.postgresql_query: community.postgresql.postgresql_db:
db: "{{ postgres_db }}" name: "{{ postgres_db }}"
query: | owner: "{{ postgres_user }}"
CREATE TABLE IF NOT EXISTS contacts ( encoding: UTF8
имя VARCHAR(100), state: present
телефон VARCHAR(20)
);
login_user: "{{ postgres_user }}" - name: Открыть порт PostgreSQL 5432 в файрволе
login_password: "{{ postgres_password }}" command: firewall-cmd --add-port=5432/tcp --permanent
become: yes
- name: Перезагрузить файрвол для применения изменений
command: firewall-cmd --reload
become: yes
handlers:
- name: Restart PostgreSQL
service:
name: postgresql
state: restarted
handlers:
- name: Restart PostgreSQL
service:
name: postgresql
state: restarted

View File

@ -2,9 +2,10 @@ pipeline {
agent any agent any
parameters { parameters {
string(name: 'TARGET_IP', defaultValue: '192.168.0.72', description: 'IP адрес машины для установки PostgreSQL') string(name: 'TARGET_IP', defaultValue: '192.168.0.72')
string(name: 'DB_USER', defaultValue: 'myuser', description: 'Имя пользователя для базы данных PostgreSQL') string(name: 'DB_USER', defaultValue: 'myuser')
string(name: 'DB_NAME', defaultValue: 'mydatabase', description: 'Имя базы данных PostgreSQL') string(name: 'DB_NAME', defaultValue: 'mydatabase')
string(name: 'BACKUP_DIR', defaultValue: '/var/backups/postgresql')
} }
environment { environment {
@ -12,10 +13,9 @@ pipeline {
} }
stages { stages {
stage('Run Ansible Playbook') { stage('Install PostgreSQL') {
steps { steps {
withCredentials([usernamePassword(credentialsId: 'postgres_password_credential', usernameVariable: 'SSH_USER', passwordVariable: 'POSTGRES_PASSWORD'), withCredentials([usernamePassword(credentialsId: 'postgres_password_credential', usernameVariable: 'SSH_USER', passwordVariable: 'POSTGRES_PASSWORD')]) {
usernamePassword(credentialsId: 'sqlpass', usernameVariable: 'SQL_USER', passwordVariable: 'SQL_PASSWORD')]) {
script { script {
writeFile file: '/ansiblesql/hosts.ini', text: """ writeFile file: '/ansiblesql/hosts.ini', text: """
@ -25,14 +25,31 @@ pipeline {
sh """ sh """
ansible-playbook -i /ansiblesql/hosts.ini /ansiblesql/install_postgresql.yml \ ansible-playbook -i /ansiblesql/hosts.ini /ansiblesql/install_postgresql.yml \\
-e postgres_user=${params.DB_USER} \ -e postgres_user=${params.DB_USER} \\
-e postgres_password=${SQL_PASSWORD} \ -e postgres_password=${POSTGRES_PASSWORD} \\
-e postgres_db=${params.DB_NAME} -e postgres_db=${params.DB_NAME}
""" """
} }
} }
} }
} }
stage('Setup PostgreSQL Backup with Cron') {
steps {
withCredentials([usernamePassword(credentialsId: 'postgres_password_credential', usernameVariable: 'SSH_USER', passwordVariable: 'POSTGRES_PASSWORD')]) {
script {
sh """
ansible-playbook -i /ansiblesql/hosts.ini /ansiblesql/backup_postgresql.yml \\
-e postgres_user=${params.DB_USER} \\
-e postgres_password=${POSTGRES_PASSWORD} \\
-e postgres_db=${params.DB_NAME} \\
-e backup_dir=${params.BACKUP_DIR}
"""
}
}
}
}
} }
} }