2024-10-20 10:28:36 +00:00
|
|
|
pipeline {
|
|
|
|
agent any
|
|
|
|
|
|
|
|
parameters {
|
2024-10-20 10:31:40 +00:00
|
|
|
string(name: 'TARGET_IP', defaultValue: '192.168.0.72')
|
|
|
|
string(name: 'DB_USER', defaultValue: 'myuser')
|
|
|
|
string(name: 'DB_NAME', defaultValue: 'mydatabase')
|
|
|
|
string(name: 'BACKUP_DIR', defaultValue: '/var/backups/postgresql')
|
2024-10-20 10:28:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
environment {
|
2024-10-20 10:31:40 +00:00
|
|
|
ANSIBLE_HOST_KEY_CHECKING = 'False'
|
2024-10-20 10:28:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stages {
|
2024-10-20 10:31:40 +00:00
|
|
|
stage('Install PostgreSQL') {
|
2024-10-20 10:28:36 +00:00
|
|
|
steps {
|
2024-10-20 10:31:40 +00:00
|
|
|
withCredentials([usernamePassword(credentialsId: 'postgres_password_credential', usernameVariable: 'SSH_USER', passwordVariable: 'POSTGRES_PASSWORD')]) {
|
2024-10-20 10:28:36 +00:00
|
|
|
script {
|
|
|
|
|
|
|
|
writeFile file: '/ansiblesql/hosts.ini', text: """
|
|
|
|
[postgres_servers]
|
|
|
|
${params.TARGET_IP} ansible_user=${SSH_USER} ansible_ssh_pass=${POSTGRES_PASSWORD} ansible_connection=ssh
|
|
|
|
"""
|
2024-10-20 10:31:40 +00:00
|
|
|
|
2024-10-20 10:28:36 +00:00
|
|
|
|
|
|
|
sh """
|
2024-10-20 10:31:40 +00:00
|
|
|
ansible-playbook -i /ansiblesql/hosts.ini /ansiblesql/install_postgresql.yml \\
|
|
|
|
-e postgres_user=${params.DB_USER} \\
|
|
|
|
-e postgres_password=${POSTGRES_PASSWORD} \\
|
2024-10-20 10:28:36 +00:00
|
|
|
-e postgres_db=${params.DB_NAME}
|
|
|
|
"""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-10-20 10:31:40 +00:00
|
|
|
|
|
|
|
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}
|
|
|
|
"""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-10-20 10:28:36 +00:00
|
|
|
}
|
|
|
|
}
|