How to check inside Jenkins pipeline script if Ansible play-book succeeded or failed.

25 views
Skip to first unread message

Shifa Shaikh

unread,
Apr 1, 2020, 1:02:53 AM4/1/20
to Jenkins Users

Below is my Jenkins pipeline script. I wish to call ex("ansible-failed") function whenever the ansible-playbook test.yml fails and thereby abort the pipeline. Below is my pipeline script.


def ex(param)
   
{    echo "ABORT due to:" + param    }

pipeline    
{
    stages        
{
        stage
('first')  {
             steps        
{
                script      
{
                   
def user = "user1"
                   
}

                echo
"Calling ansible"

                ansiblePlaybook
(playbook: '/app/test.yml' extraVars: [ app_ip: "10.0.0.12,10.0.0.13" ])

               
}
           
}
        stage
('second')   {
                 steps    
{
                  script    
{
                    println
"Second Play"
                   
}
               
}
           
}

       
}
   
}



The above Jenkins pipeline script invokes ansible-playbook however, I do not know how to detect if the ansible play succeeded or failed. In case it failed I wish to call the ex() function and abort the Jenkins pipeline.


Incase ansible-playbook run succeeds then I wish to simply continue and execute stage('second').


Can you please suggest how we can check the condition if ansible run succeeded or failed inside the Jenkins pipeline script?

kalkin powale

unread,
Apr 2, 2020, 11:45:52 AM4/2/20
to Jenkins Users
Hi Shifa,

I don't know if function ansiblePlaybook returns output or not. But, you can write the result of ansiblePlaybook in some text file.
After completion of ansiblePlabook function, checks the status of the result from a text file.

save the result as some variable "retVal".

then in between two-stages execute following command
stage('stg_1'){}
if( retVal == 'Fail') {
   ex()
   currentBuild.result = 'ABORTED'
   return
}
stage('stg_2'){}

Yeikel

unread,
Apr 2, 2020, 12:30:31 PM4/2/20
to jenkins...@googlegroups.com
Also...if it returns an error code back, you could also use that as well

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/37ad0bd5-50d6-4f38-86f3-633687bc3772%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages