Open In App

Using Ansible in Jenkins Pipelines

Last Updated : 14 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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

2024-03-24-09_58_40-WhatsApp

Now connect with terminal

2

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-(1)-(1)

Now install jenkins by using following command,

sudo yum -y install jenkins
2

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
3

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

4

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
5

Official page of jenkins

6

Now install Ansible by using following command

sudo amazon-linux-extras install ansible2
7-(1)-(1)-(1)-(1)-(1)

Step-3: Install Ansible Plugin

  • Go to jenkins Dashboard --> Manage Jenkins --> Available Plugins --> Ansible
Screenshot-2024-05-11-113111

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
Screenshot-2024-05-11-161231

Step 5: Add Credentials

  • For ansible credentials are required for this
  • Go to manage Jenkins --> Credentials --> Add credentials
Screenshot-2024-05-11-155941

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
Screenshot-2024-05-11-153341


  • 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'
}
}
}
}
Screenshot-2024-05-11-155824
  • Now run the pipeline
Screenshot-2024-05-11-155614

Step 8: Verify

Now go to EC2 dashboard copy Public IP of slave node and browse it.

Screenshot-2024-05-11-155839

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.


Next Article
Article Tags :

Similar Reads