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
Building with Docker Using Jenkins Pipelines
Continuous Integration and Continuous Delivery (CI/CD) are two essential practices in today's modern development landscape for delivering software efficiently. The most potent combination to implement a smooth CI/CD workflow is Docker and Jenkins. Docker packages an application and its dependencies
7 min read
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
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: JenkinsDevOps 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 / Cont
5 min read
Implementing CI/CD Pipelines with Docker and Jenkins
Continuous Integration (CI) and Continuous Deployment (CD) have become the backbone of modern software development. They enable teams to deliver code faster, more reliably, and with fewer errors. Automating the process of building, testing, and deploying code allows developers to focus on what matte
7 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
How to Install Git Using Ansible ?
Git is a powerful and broadly utilized version control system that assumes a crucial role in current software development work processes, it gives developers a powerful structure for tracking changes in source code, planning cooperative work, and managing project chronicles proficiently. With its co
6 min read
How to Install rpm Package in Linux Using Ansible ?
Ansible, a strong computing automation tool, expands its capabilities beyond configuration management to package management, including the installation of RPM packages on Linux frameworks.RPM (Red Hat Package Manager) is a package management system executives framework utilized by different Linux di
7 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