pipeline { agent any environment { ANSIBLE_SERVER = "157.230.120.252" } stages { stage("copy playbook to ansible-server") { steps { script { echo "copying ansible folder to ansible server" sshagent(['ansible-server-key']) { // ${ANSIBLE_SERVER}:/root without root will give jenkins@${ANSIBLE_SERVER}:/root sh "scp -o StrictHostKeyChecking=no ansible/ansible.cfg root@${ANSIBLE_SERVER}:/root" } } } } 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 { echo "executing ansible-playbook" def remote = [:] remote.name = "ansible-server" remote.host = ANSIBLE_SERVER remote.allowAnyHosts = true withCredentials([sshUserPrivateKey(credentialsId: 'ansible-server-key', keyFileVariable: 'identity', usernameVariable: 'user')]) { remote.identityFile = identity remote.user = user sshCommand remote: remote, command: "ls -l" } } } } } }