How to run Ansible playbook from Jenkins pipeline job

jenkins logo
Reading Time: 4 minutes

In this blog, We will learn how to run ansible using Jenkins job. First, we need to know what is Jenkins?? Basically, Jenkins is a CI/CD tool. Jenkins is an open-source that is written in Java. Continuous Integration & Continuous Delivery are integral parts of DevOps. Therefore Jenkins is quite a famous tool. As they are used for integrating multiple stages of the methodology. We can see many CI/CD tools in the market but Jenkins is the most popular tool which is a JAVA-based tool. 
Now, What is Ansible?? Ansible is an open-source automation tool that configuration management, automates provisioning,  application deployment, orchestration, and lots of other manual IT processes. Any users can use Ansible to install certain software, automate daily tasks, provision infrastructure, and share automation across the entire organization.

How to configure Jenkins with ansible

First, We need to install Jenkins and Ansible on the host system or server. The system can be any local system or AWS instance or others. Now you can follow the below steps:

Step-1

After the installation, We can access the Jenkins Using an IP address or localhost with port 8080. Port 8080 is the default port for Jenkins.

Step-2

Now, We should install the Ansible plugin on Jenkins which makes Jenkins interact with ansible. Follow the below steps to install the ansible plugin:

Dashboard >> Manage Jenkins >> Manage  Plugin

Step-3

Now create a job to run the pipeline. We just leave all options as default and write the script for running the ansible. Follow the below steps:

Dashboard >> new Item >> give name and create

We have one more option to generate pipeline syntax. Therefore we will use that option to create an ansible step. Here we will pass the playbook and inventory file then we will leave every step default and then generate the pipeline syntax and here we need to pass the credential to connect with the remote machine.

Here we have to define a few values as you can see in below image:

ID: It can be any name.

Username: It is the user name of the remote machine

Description: We can give any description which defines your purpose.

Private key: Here we need to pass the private key of our remote machine ( Basically, I am using an AWS instance. Therefore When we create an instance on AWS. At the same time, We will get the pem file which has the private key for our instance.)

As we created a credential. We will add this in the script generator.

Now copy that script and paste it into the main script then save.

pipeline {
    agent any
    stages {
        stage ("SCM checkout") {
            steps {
                git "https://github.com/muzakkirsaifi123/Blog1.git"
                
            }
        }
        stage(" execute Ansible") {
           steps {
                ansiblePlaybook credentialsId: 'private-key', disableHostKeyChecking: true, installation: 'Ansible', inventory: 'dev.inv', playbook: 'apache.yml'
            }    
        }    
    }
}

As we have written a script to run ansible. I have defined all the configurations in a repository. The repository is located on GitHub. I have pulled that repository. In this repository. I have defined the inventory file and playbook for remote users:

# apache.yml
---
- hosts: webservers 
  become: True
  tasks:
    - name: Install packages
      apt:
        name: "nginx"
        state: "present"
    - name: Start nginx server
      service:
        name: nginx
        state: started
        enabled: True
    - name: Deploy static website
      copy:
        src: index.html # We have define a html page 
        dest: /var/www/html/
...
# We will install nginx and run the service then we will replace our own html oage with default page.

#dev.inv

[webservers]
172.31.26.23 ansible_user=ec2-user 

# this is the inventory file for remote server where we want to install nginx.

Step-4

Now, we just need to build the job but check the remote server to verify the installation.

Conclusion :

I have tried to explain all processes in just 4 steps. Therefore You can follow the below links to know more.

Jenkins: https://www.jenkins.io/doc/

Ansible: https://blog.knoldus.com/getting-started-with-ansible-2/

knoldus


Written by 

Mohd Muzakkir Saifi is a Software Consultant at Knoldus Software. He loves to take deep dives into cloud technologies & different tools. His hobbies are playing gymnastics and traveling.

1 thought on “How to run Ansible playbook from Jenkins pipeline job5 min read

Comments are closed.