Reputation: 21
I have a Jenkins pipeline that runs an ansible playbook against a few servers
node {
stage('Get Playbook') {
dir('ansible-dir') {
git branch: 'master',
credentialsId: 'creds',
url: 'ssh://[email protected]'
}
}
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'ansible', usernameVariable: 'username', passwordVariable: 'password']]){
def remote = [:]
remote.name = 'Remote Ansible Server'
remote.host = 'server.domain.com'
remote.user = username
remote.password = password
remote.allowAnyHosts = true
stage('Copy dir to remote server'){
sshPut remote: remote, from: '/localdir', into: '/home/remote/jenkins'
}
stage('Run ansible playbook'){
sshCommand remote: remote, command: 'ANSIBLE_CONFIG=/home/remote/jenkins/ansible.cfg ansible-playbook /home/remote/jenkins/playbook.yml -vvv'
}
stage('Copy report to local machine') {
sshGet remote: remote, from: '/home/remote/jenkins/reports', into: '/home/user/reports', override: true
}
}
The problem I'm having is the 'Copy report to local machine' stage doesn't always run. Occasionally a server will fail in the play recap, which isn't a problem in itself as the report generated in the playbook accounts for that and has logging to identify it.
The problem being that when a server does fail in the play recap, jenkins give the following error.
org.hidetake.groovy.ssh.session.BadExitStatusException: Command returned exit status 2: ANSIBLE_CONFIG=/home/remote/jenkins/ansible.cfg ansible-playbook /home/remote/jenkins/playbook.yml -vvv
This prevents the next stage in the pipeline from running. Is there a way to suppress this error output so the pipeline continues?
Upvotes: 1
Views: 2022
Reputation: 21
Figured this out by putting the stage running the playbook in a try catch
try {
stage('Copy dir to remote server'){
sshPut remote: remote, from: '/localdir', into: '/home/remote/jenkins'
} catch(e) {
build_ok = false
echo e.toString()
}
This lets it continue and to the next stage.
Upvotes: 1