From bfad48fff124d573ddd08e73b67cd595708109f5 Mon Sep 17 00:00:00 2001 From: reqwizz Date: Mon, 4 Nov 2024 20:28:19 +0300 Subject: [PATCH] remove hosts, change run without shell cmd --- ansible/Jenkinsfile | 43 ++++++++++++++++++++++--------------------- ansible/hosts.ini | 6 ------ ansible/inventory.yml | 14 ++++++++++++++ 3 files changed, 36 insertions(+), 27 deletions(-) delete mode 100644 ansible/hosts.ini create mode 100644 ansible/inventory.yml diff --git a/ansible/Jenkinsfile b/ansible/Jenkinsfile index a331dd1..6c57f2e 100644 --- a/ansible/Jenkinsfile +++ b/ansible/Jenkinsfile @@ -27,20 +27,7 @@ pipeline { ansible-vault decrypt ./ansible/id_ed25519_vault --output=${decryptedKeyFile} --vault-password-file=${vaultPassFile} """ - def hostsFile = "${tempDir}/hosts.ini" - def hostsFileContent = """ - [SiteA] - 192.168.0.61 ansible_user=ansible ansible_ssh_private_key_file=${decryptedKeyFile} - - [SiteB] - 192.168.0.62 ansible_user=ansible ansible_ssh_private_key_file=${decryptedKeyFile} - - [proxy] - 192.168.0.63 ansible_user=ansible ansible_ssh_private_key_file=${decryptedKeyFile} - """ - writeFile file: hostsFile, text: hostsFileContent - - env.HOSTS_FILE = hostsFile + env.DECYPTED_KEY_FILE = decryptedKeyFile } } } @@ -49,14 +36,28 @@ pipeline { steps { script { def sanitized_content = params.ADDITIONAL_CONTENT.replaceAll("'", "\\'").replaceAll('"', '\\"') - - if (params.SITE_OPTION == 'SiteA') { - sh "export PATH=/usr/local/bin:\$PATH && ansible-playbook /coursework/ansible/playbook.ymlansible/playbook.yml -i ${env.HOSTS_FILE} -l SiteA -e \"additional_content='${sanitized_content}'\"" - } else if (params.SITE_OPTION == 'SiteB') { - sh "export PATH=/usr/local/bin:\$PATH && ansible-playbook /coursework/ansible/playbook.ymlansible/playbook.yml -i ${env.HOSTS_FILE} -l SiteB -e \"additional_content='${sanitized_content}'\"" - } else if (params.SITE_OPTION == 'SiteA&B') { - sh "export PATH=/usr/local/bin:\$PATH && ansible-playbook /coursework/ansible/playbook.yml -i ${env.HOSTS_FILE} -l SiteA,SiteB,proxy -e \"additional_content='${sanitized_content}'\"" + + def siteOption = params.SITE_OPTION + def hostsFile = env.HOSTS_FILE + + def targetGroups = '' + if (siteOption == 'SiteA') { + targetGroups = 'SiteA' + } else if (siteOption == 'SiteB') { + targetGroups = 'SiteB' + } else if (siteOption == 'SiteA&B') { + targetGroups = 'SiteA,SiteB,proxy' } + + ansiblePlaybook( + playbook: 'ansible/playbook.yml', + inventory: "ansible/inventory.yml", + extraVars: [ + additional_content: sanitized_content, + ansible_ssh_private_key_file: env.DECYPTED_KEY_FILE + ], + limit: targetGroups + ) } } } diff --git a/ansible/hosts.ini b/ansible/hosts.ini deleted file mode 100644 index 7330d19..0000000 --- a/ansible/hosts.ini +++ /dev/null @@ -1,6 +0,0 @@ -[SiteA] -192.168.0.61 ansible_user=ansible -[SiteB] -192.168.0.62 ansible_user=ansible -[proxy] -192.168.0.63 ansible_user=ansible \ No newline at end of file diff --git a/ansible/inventory.yml b/ansible/inventory.yml new file mode 100644 index 0000000..68a701c --- /dev/null +++ b/ansible/inventory.yml @@ -0,0 +1,14 @@ +all: + children: + SiteA: + hosts: + 192.168.0.61: + ansible_user: ansible + SiteB: + hosts: + 192.168.0.62: + ansible_user: ansible + proxy: + hosts: + 192.168.0.63: + ansible_user: ansible \ No newline at end of file