Using Ansible in Jenkins Pipelines
Last Updated :
14 May, 2024
In the fast-paced software development scene, DevOps practices have become significant for associations expecting to convey excellent software quickly and dependably. DevOps underscores collaborations, automation, and continuous integration and delivery (CI/CD) to smooth out the software development lifecycle.
Two vital tools in the DevOps toolkit are Ansible and Jenkins. Ansible is an open-source automation tool known for its simplicity and versatility in organizing IT infrastructure and application deployment. Then again, Jenkins is a broadly utilized automation server that works with CI/CD pipelines, empowering groups to automate building, testing, and deploying software.
This article explores the integration of Ansible with Jenkins Pipelines, providing an extensive manual for understanding the ideas, terminologies, and practical execution steps included. From defining Ansible playbooks to configuring Jenkins Pipelines, readers will acquire experiences in how to use these instruments to automate infrastructure provisioning, configuration management, and application deployment.
Primary Terminologies
Ansible
- Ansible is an open-source automation tool that works on IT orchestration, configuration management, and application deployment. It works over SSH and not require agents installed on remote systems. Ansible uses YAML-based playbooks to define automation tasks.
Jenkins
- Jenkins is a well-known open-source automation server utilized for building, testing, and deploying software. It upholds CI/CD pipelines, empowering the automation of the software delivery process. Jenkins gives an electronic point of interaction to configuring and monitoring automation tasks.
Jenkins Pipeline
- Jenkins Pipeline is a set-up of plugins that allow defining continuous delivery pipelines as code. It gives a domain-specific language (DSL) to communicate whole CI/CD pipelines as code, empowering easy versioning, sharing, and reuse. Jenkins Pipeline can be scripted utilizing Groovy language structure inside a Jenkinsfile
Ansible Playbooks
- Ansible Playbooks are YAML file containing a set of tasks to be executed on remote hosts. They define the ideal condition of the system and are idempotent, ensuring consistent system configurations. Ansible Playbooks use modules to communicate with remote systems and manage configurations.
Jenkinsfile
- Jenkinsfile is a text file that defines the whole Jenkins Pipeline as code. It very well may be put away in version control close to the application code, enabling pipeline configuration as a feature of the application's source code. Jenkinsfile takes into account defining stages, steps, and post-build actions inside the pipeline.
Step-by-Step Process to use Ansible in Jenkins Pipeline
Step 1: Launch EC2 Instance
First go to AWS Console and login by using your credentials or create new account
Now go to EC2 dashboard and Launch twoto

Now connect with terminal

Step 2: Install Ansible and Jenkins
Install ansible and Jenkins in our local machine by using following commands
To install Jenkins packages follow below commands
sudo wget -O /etc/yum.repos.d/jenkins.repo \https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
Install java because jenkins run times is java so we need to install java
sudo yum -y install java-17*
-(1).png)
Now install jenkins by using following command,
sudo yum -y install jenkins

After completion of jenkins installation, now start and enable the jenkins and also check status of the jenkins by using following commands
sudo systemctl start Jenkins
sudo systemctl enable Jenkins
sudo systemctl status Jenkins

Now copy Public IP of your instance and Browse it along with Port 8080, because jenkins runs on port 8080.

Unlock jenkins by using administration password, administration password was shown in while check in status of the jenkins or either we can see the password in below path
/var/lib/jenkins/secrets/initialPassword

Official page of jenkins

Now install Ansible by using following command
sudo amazon-linux-extras install ansible2
-(1)-(1)-(1)-(1).png)
Step-3: Install Ansible Plugin
- Go to jenkins Dashboard --> Manage Jenkins --> Available Plugins --> Ansible

Step 4: Configure Ansible in Jenkins
- Now configure ansible in jenkins
- Go to Jenkins dashboard --> Manage Jenkins --> Tool Configuration
- Now add ansible installation by providing path to ansible executable

Step 5: Add Credentials
- For ansible credentials are required for this
- Go to manage Jenkins --> Credentials --> Add credentials

Step 6: Create Playbook
- Now create playbook by using YAML file
<filename.yml>
- hosts: all
become: True
tasks:
- name: Install packages
yum:
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/
Step 7: Define Jenkins Pipeline
- Now define jenkins pipeline
- Go to jenkins dashboard and click on New item and choose pipeline

- Give description to your project in pipeline script and write pipeline script
pipeline {
agent any
stages {
stage("SCM checkout") {
steps {
git "https://github.com/Sada-Siva-Reddt/blog1.git"
}
}
stage("Execute Ansible") {
steps {
ansiblePlaybook credentialsId: 'private-key',
disableHostKeyChecking: true,
installation: 'Ansible',
inventory: 'dev.inv',
playbook: 'apache.yml'
}
}
}
}


Step 8: Verify
Now go to EC2 dashboard copy Public IP of slave node and browse it.

Conclusion
Integration of Ansible with Jenkins Pipelines addresses a critical progression in DevOps practices, giving a consistent solution for automating and enhancing software delivery pipelines, this integrations outfits the qualities of both Ansible and Jenkins, enabling associations to accomplish more prominent effectiveness, dependability, and nimbleness in their improvement work processes.
Ansible's powerful automation abilities, worked with by its declarative YAML-based playbooks, smooth out tasks, for example, infrastructure provisioning, configuration management, and application deployment. By classifying these processes, Ansible ensure consistency across conditions, minimizes manual mediation, and decreases the risk of configuration or deployment errors.
Besides, the synergy energy among Ansible and Jenkins upgrades versatility, allowing teams to adjust their automation work processes to accommodate developing project requirements and developing responsibilities, with automation driving the software delivery process, teams can accomplish quicker time-to-market, further develop unwavering quality, and respond all the more successfully to changing business needs.
Fundamentally, the integration of Ansible with Jenkins Pipelines enables organizations to embrace automation as a center precept of their DevOps practices, by utilizing these tools actually, teams can smooth out their work processes, reduce manual effort, and spotlight on delivering high-quality software with speed and certainty.
Similar Reads
How to use AWS Credentials in Jenkins Pipeline
Jenkins, an industry-standard automation server, assumes a pivotal role in present-day software development practices, especially in continuous integration and continuous delivery (CI/CD) work processes. As associations progressively embrace distributed computing, coordination with cloud services li
8 min read
Understanding Jenkins CI/CD Pipeline And Its Stages
Jenkins is an open-source automation server that enables developers to reliably build, test, and deploy applications. It supports continuous integration and continuous delivery (CI/CD) workflows that allow teams to frequently deliver high-quality software. Jenkins is extremely popular, with over 100
15 min read
How To Install PIP Using Ansible ?
Nowadays the IT culture is evolving with Agile practices and DevOps Culture, on idea of making the business plans, and features quickly to market and making users available. In this software development workflow configuring software and updating the patches to each system manually or through automat
6 min read
What Is Jenkins Declarative Pipeline?
A Jenkins Declarative Pipeline is a structured and simplified approach to defining your continuous integration and continuous delivery (CI/CD) pipelines in Jenkins. Unlike Scripted Pipelines, which provide more flexibility but are harder to maintain, Declarative Pipelines offer a clean, human-readab
5 min read
How to Make a CI-CD Pipeline in Jenkins?
Pre-requisites: Jenkins DevOps professionals mostly work with pipelines because pipelines can automate the processes like building, testing, and deploying the application. Doing manually by UI takes lots of time and effort which will affect productivity. With the help of Continuous Integration / Con
5 min read
Implementing CI/CD Pipelines with Docker and Jenkins
Modern software development has CI and CD pipelines as its core practices, fostering the delivery of code faster, more reliably, and with fewer errors. CI/CD pipelines generally automate the process of building, testing, and deployment to ease the development process, so that the lifecycle becomes s
6 min read
Jenkins and GIT Integration using SSH Key
Integrating Jenkins with Git using SSH keys is a powerful way to automate your Continuous Integration (CI) and Continuous Deployment (CD) pipelines while ensuring secure access to your Git repositories. In this article, we'll guide you through the process of setting up Jenkins to work seamlessly wit
3 min read
Deploying An Application Using Jenkinsfile
In the IT world, initially, there were silos between the development and testing teams that created delays in moving the application to user availability. DevOps came into the picture with a culture practice that brings automation with a lot of tools. Jenkins is one of the popular CI/CD tools. Jenki
5 min read
Setting Up A CI/CD Pipeline For PHP Applications Using Jenkins
In contemporary software development, Continuous Integration and Continuous Deployment (CI/CD) pipelines have arisen as key devices for smoothing out the deployment cycle and ensuring the quick conveyance of high-quality applications. This guide centers around designing a CI/CD pipeline custom-fitte
6 min read
How To Send Email Notification Using Jenkins ?
Jenkins notifications function as messages or alerts that notify users of significant occurrences, conditions, or modifications in CI/CD pipelines. They are essential in guaranteeing transparency and visibility throughout the development process and informing stakeholders. By following the below ste
4 min read