pipeline { agent any parameters { 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') } environment { ANSIBLE_HOST_KEY_CHECKING = 'False' } stages { stage('Install PostgreSQL') { steps { withCredentials([usernamePassword(credentialsId: 'postgres_password_credential', usernameVariable: 'SSH_USER', passwordVariable: 'POSTGRES_PASSWORD')]) { script { writeFile file: '/ansiblesql/hosts.ini', text: """ [postgres_servers] ${params.TARGET_IP} ansible_user=${SSH_USER} ansible_ssh_pass=${POSTGRES_PASSWORD} ansible_connection=ssh """ sh """ ansible-playbook -i /ansiblesql/hosts.ini /ansiblesql/install_postgresql.yml \\ -e postgres_user=${params.DB_USER} \\ -e postgres_password=${POSTGRES_PASSWORD} \\ -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} """ } } } } } }