Deploy Ansible Playbook on Ubuntu Linux using Jenkins

Introduction

Ansible is an open-source automation tool that simplifies the process of managing and orchestrating IT infrastructure. It provides a simple, declarative language to describe system configurations, and through its agentless architecture, it can manage a wide range of systems, including servers, network devices, and cloud resources, Ansible operates over SSH or WinRM (Windows Remote Management) connections, allowing it to manage remote systems without requiring any additional software to be installed on them. This agentless approach simplifies the setup and maintenance of Ansible, as well as reduces the overhead on managed systems.

System Requirements

Ansible has modest hardware requirements, and it can run on a wide range of systems. Here are the general hardware recommendations for running Ansible:

  • Processor: A modern multi-core processor is recommended for better performance. Ansible doesn’t require a specific processor type and can work well on both Intel and AMD processors.
  • Memory (RAM): The amount of memory required depends on the scale of your infrastructure and the complexity of the tasks you plan to execute. As a general guideline, having at least 2GB of RAM is recommended for running Ansible. If you are managing larger environments, more memory will be beneficial.
  • Storage: Ansible itself doesn’t require significant storage space, as it is a lightweight software. However, you’ll need sufficient disk space to store your Ansible playbooks, inventory files, and any other associated files. The storage requirements largely depend on the size and number of playbooks and other artifacts you plan to manage.
  • Network: Ansible primarily operates over SSH connections or WinRM for Windows systems. Therefore, a reliable network connection is essential for communication between the Ansible control node and the managed nodes.

It’s worth noting that the above requirements are for the Ansible control node, which is the system from where you manage and execute Ansible playbooks. The managed nodes, which are the systems being configured or managed by Ansible, have their own hardware requirements that depend on the specific operating systems and applications running on them.

Overall, Ansible is designed to be resource-efficient and can run on a wide range of systems, including physical servers, virtual machines, and even cloud instances.

In this post, We will show you how to deploy ansible playbook on ubuntu machine using Jenkins automation server.

To perform this tutorials, We must have the following recourses.

1.up and running Jenkins server if not use the given link to install Jenkins on ubuntu 22.04 LTS

2. GitHub repository with Ansible playbook along with GitHub account token, If not use the given link to get these.

Step 1: Installing Ansible

To install Ansible on Ubuntu 20.04 LTS, you can follow these steps, We can install Ansible on Jenkins it self or we can install Jenkins slave machine and from pipeline we can call the Jenkins slave machine which has ansible.

sudo apt-get update

Install Ansible using the apt package manager:

sudo apt-get install ansible -y

After the installation is complete, you can verify the installation by checking the Ansible version:

ansible --version

Here we can see that Ansible version and other configuration path but the most important executable location = /usr/bin/ansible We will use the Ansible executable path with Jenkins’s Ansible plugin.

Step 3: Installing Ansible Plugin

To install Jenkins’s Ansible plugin we need to follow the given steps.

Go to the Jenkins Home Page

Click on Manage Jenkins button form left side.

Click on Plugins section

Click on Available plugin

Search for Ansible plugin

Select the Ansible plugin

Click on Install without restart button.

If everything is going fine So then we will get status like this.

Step 4: Configure Ansible Path

Once the Ansible plugin got installed So then we need to follow the given steps, In order to configure Ansible executable path /usr/bin/.

Go to Jenkins Home page

Click on Manage Jenkins

Click on tools

Go to Ansible sub section

Click on Add Ansible

Define the name like Ansible or Ansible-Version

Define the /usr/bin in Path to ansible executables directory

Click on apply and save button.

Step 5: Generate GitHub Token

GitHub repository with Ansible playbook along with GitHub account token, If not use the given link to get these.

Step 6: Create Credentials

We need to create 2 creds for GitHub repository cloning using Jenkins and SSH connection with our Ansible target machine like ubuntu to deploy LAMP server.

To create GitHub account creds.

Go to the Jenkins home page.

Click on Manage Jenkins.

Click on Credentials section.

Click on System.

Click on Global credentials (unrestricted).

Click on Add Credentials button.

Select Kindusername with password.

Save your username, token as a password and id.

Click on create button.

If everything is going fine So then you should get details like this.

To create SSH creds.


Go to the Jenkins home page.

Click on Manage Jenkins.

Click on Credentials section.

Click on System.

Click on Global credentials (unrestricted).

Click on Add Credentials button.

Select KindSSH Username with private key.

Save your ID, Username and Private key

Click on create button.

If everything is going fine So then you should get details like this.

Step 7: Update PublicIP

We need to update the PublicIP / PrivateIP in the inventory file and our inventory reside in git repository on root position, you can do that same as per your directory structure.

Step 8: Creating Jenkins Pipeline

We are good to create Jenkins pipeline using Ansible with our pipeline, We will create given stages.

State 1 Git Checkout

State 2 Deploy Ansible-playbook

Go to the Jenkins home page and click on Create a job

Now enter the suitable job name.

Select the pipeline.

Click on Ok button.

Go to the pipeline script and paste the following pipelines code.

pipeline {
    agent any

    stages {
        stage('Checkout') {
            steps {
                git branch: 'main', credentialsId: 'github', url: 'https://github.com/aftab70/jenkins_ansible.git'
            }
        }
        stage('Deploy Playbook') {
            steps {
                ansiblePlaybook credentialsId: 'SSH', disableHostKeyChecking: true, installation: 'Ansible', inventory: 'inventory.txt', playbook: 'apache-playbook.yml'
            }
        }        
    }
}

You can update the git repository, Ansible inventory file, ansible playbook path with their creds.

Click on apply and save and click on build now button.

Click on build number to see console output.

here we can see that all the defined ansible running one by one.

After successful deployment of ansible playbook status we should get like this.

Conclusion

We have done successful created CI CD pipeline to deploy the ansible playbook using Jenkins on ubuntu 22.04 linux, Still you are having any issue, So please leave a comment below.

Author

Deploy Ansible Playbook on Ubuntu Linux using Jenkins

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top