From d8dda2f0ce4b6e6767525e3acc80b4d4a9f061cd Mon Sep 17 00:00:00 2001 From: Nana Janashia Date: Sat, 3 Apr 2021 18:13:49 +0200 Subject: [PATCH] Add copying ssh key for ec2 --- Jenkinsfile-v1 | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/Jenkinsfile-v1 b/Jenkinsfile-v1 index e01b845..e8191a9 100644 --- a/Jenkinsfile-v1 +++ b/Jenkinsfile-v1 @@ -4,25 +4,22 @@ pipeline { ANSIBLE_SERVER = "157.230.120.252" } stages { - stage("copy playbook to ansible-server") { + stage("copy ansible folder and ec2 access key to ansible-server") { steps { script { - echo "copying ansible folder to ansible server" sshagent(['ansible-server-key']) { + echo "copying ansible folder to ansible server" // ${ANSIBLE_SERVER}:/root without root will give jenkins@${ANSIBLE_SERVER}:/root - sh "scp -o StrictHostKeyChecking=no ansible/ansible.cfg root@${ANSIBLE_SERVER}:/root" + sh "scp -o StrictHostKeyChecking=no ansible/* root@${ANSIBLE_SERVER}:/root" + + echo "copying ssh keys for ec2 instances" + withCredentials([sshUserPrivateKey(credentialsId: 'ec2-server-key', keyFileVariable: 'keyfile', usernameVariable: 'user')]) { + sh "scp ${keyfile} root@${ANSIBLE_SERVER}:/root/key.pem" + } } } } } - stage("copy ssh keys for Ansible") { - steps { - script { - echo "copying ssh keys for ec2 instances" - - } - } - } stage("execute ansible playbook from the ansible-server") { steps { script { @@ -33,8 +30,8 @@ pipeline { remote.host = ANSIBLE_SERVER remote.allowAnyHosts = true - withCredentials([sshUserPrivateKey(credentialsId: 'ansible-server-key', keyFileVariable: 'identity', usernameVariable: 'user')]) { - remote.identityFile = identity + withCredentials([sshUserPrivateKey(credentialsId: 'ansible-server-key', keyFileVariable: 'keyfile', usernameVariable: 'user')]) { + remote.identityFile = keyfile remote.user = user sshCommand remote: remote, command: "ls -l"