Integrate Ansible with Jenkins
Please visit my website and subscribe to my youtube channel for more articles
In this blog, we will Integrate Ansible with Jenkins
Prerequistie
- Jenkins Server
- Ansible Server installed on Jenkins Server
- Ansible Plugin
- ./ssh/config created for Jenkins user
Steps:
- Go to Jenkins -> Manage Plugins -> Ansible
Configure -
Go to Manage Jenkins -> Global Tool configuration ->Search for Ansible

Create a pipeline job in Jenkins
pipeline {
agent any
stages {
stage('checkout') {
steps {
git branch: 'develop', url: 'yoururl'
}
}
stage('Ansible Init') {
steps {
script {
def tfHome = tool name: 'Ansible'
env.PATH = "${tfHome}:${env.PATH}"
sh 'ansible --version'
}
}
}
stage('Ansible Deploy') {
steps {
dir('dev/ansible')
{
sh 'ansible all -m ping -i hosts'
}
}
}
Commit hosts file and ansible.cfg file with your configuration in your source control.
For ssh config file, you can also put in your source control and it will take all configuration from there. But for the host “test” which is getting invoked through proxy will not work directly. You have to add this same ssh config under the jenkins user as “config” name

Host dev
Hostname 2.3.34.4
User ubuntu
StrictHostKeyChecking no
IdentityFile ~/terraform-deployHost test
Hostname 2.3.4.3
User ubuntu
StrictHostKeyChecking no
IdentityFile ~/terraform-deploy
ProxyCommand ssh -W %h:%p dev
After this, when you trigger Jenkins job it will ping all the host successfully. If you have not added the config file under Jenkins user then it will fail while ping to the test server with the error
"changed": false,
"msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname dev: Name or service not known\r\nssh_exchange_identification: Connection closed by remote host\r\n",
"unreachable": true
}
If all configuration is ok, you will get the below output
Running in /var/lib/jenkins/workspace/TerraformScripts/Ansible_Test/dev/ansible
[Pipeline] {
[Pipeline] sh
[ansible] Running shell script
+ ansible all -m ping -i hosts
dev | SUCCESS => {
"changed": false,
"ping": "pong"
}
test | SUCCESS => {
"changed": false,
"ping": "pong"
}