From 803c80efff5331c5a6f387c77706f4177f01c9a1 Mon Sep 17 00:00:00 2001 From: dima Date: Sun, 20 Oct 2024 13:28:36 +0300 Subject: [PATCH] Jenkens integration with variables and dynamic hosts.ini file --- hosts.ini | 2 -- install_postgresql.yml | 6 +++--- pipeline.groovy | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 5 deletions(-) delete mode 100644 hosts.ini create mode 100644 pipeline.groovy diff --git a/hosts.ini b/hosts.ini deleted file mode 100644 index fdc31b9..0000000 --- a/hosts.ini +++ /dev/null @@ -1,2 +0,0 @@ -[postgres_servers] -192.168.0.72 ansible_user=root ansible_password='XSW@1qaz' ansible_connection=ssh diff --git a/install_postgresql.yml b/install_postgresql.yml index 310fe81..324e617 100644 --- a/install_postgresql.yml +++ b/install_postgresql.yml @@ -3,9 +3,9 @@ hosts: postgres_servers become: yes vars: - postgres_user: myuser - postgres_password: mypassword - postgres_db: mydatabase + postgres_user: "{{ postgres_user }}" + postgres_password: "{{ postgres_password }}" + postgres_db: "{{ postgres_db }}" tasks: - name: Update zypper diff --git a/pipeline.groovy b/pipeline.groovy new file mode 100644 index 0000000..861c189 --- /dev/null +++ b/pipeline.groovy @@ -0,0 +1,38 @@ +pipeline { + agent any + + parameters { + string(name: 'TARGET_IP', defaultValue: '192.168.0.72', description: 'IP адрес машины для установки PostgreSQL') + string(name: 'DB_USER', defaultValue: 'myuser', description: 'Имя пользователя для базы данных PostgreSQL') + string(name: 'DB_NAME', defaultValue: 'mydatabase', description: 'Имя базы данных PostgreSQL') + } + + environment { + ANSIBLE_HOST_KEY_CHECKING = 'False' + } + + stages { + stage('Run Ansible Playbook') { + steps { + withCredentials([usernamePassword(credentialsId: 'postgres_password_credential', usernameVariable: 'SSH_USER', passwordVariable: 'POSTGRES_PASSWORD'), + usernamePassword(credentialsId: 'sqlpass', usernameVariable: 'SQL_USER', passwordVariable: 'SQL_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=${SQL_PASSWORD} \ + -e postgres_db=${params.DB_NAME} + """ + } + } + } + } + } +}