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
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i
6 min read
Spring Boot Interview Questions and Answers Spring Boot is a Java-based framework used to develop stand-alone, production-ready applications with minimal configuration. Introduced by Pivotal in 2014, it simplifies the development of Spring applications by offering embedded servers, auto-configuration, and fast startup. Many top companies, inc
15+ min read