run ansible playbook on jenkins pipeline

Nab picture Nab · Apr 20, 2018 · Viewed 21.6k times · Source

I open this topic because I have a issue when I try to run ansiblePlaybook in a Jenkins pipeline. The plugin ansible is install and enable. I have write a Jenkinsfile where i setup the environnement and call my ansible pipeline like you can see below :

enter image description here

Like you can see it I had also verified that my ansible is in the path and that it recognised it. But it can't run it. Below the output with the error:

enter image description here

I also tried to run it like a shell command :

enter image description here

But it can't reach my server the ssh connection fails :

enter image description here

But when I use ansible in a freestyle job it works well so I don't understand why !

Hopefully, someone can help me with this issue and have a solution even for the second issue so I can have a temporary solution. Please, forgive my English if it is bad, it's not my native language.

Regards,

Answer

Nab picture Nab · May 2, 2018

First thanks to you Matt Schuchard and Jimit Raithatha for your answer. I find how to fix my issue. I use ansible plugin and add ansible using the following code :

withEnv(["PATH+ANSIBLE"=${tool 'name of the tool in tool configuration'}])

But withEnv is only suitable for shell commande (sh '...') and doesn't work with a Java call like I did with the ansiblePlaybook. The command ansiblePlaybook has a parameter that allow the user to specify where is the tool that you want to use, we simply add to use the paramater with the name that is specify in the custom tool configuration, for the ssh error I simply didn't configure my sshagent; here an example :

    sshagent (credentials: ['name of the credential']) {
                ansiblePlaybook(
                    credentialsId: id of the credential',
                    inventory: 'inventory file',
                    installation: 'name of the tool specified on the configuration tool screen',
                    limit: 'host where run the playbbok',
                    playbook: 'path to the playbook',
                    extras: ' options and var that you want add for instance verbose mode : -vvv'
                )
            }

Hopefuly people that have the same issue can be helped by this topic!

Regards,