Tips & Tricks

Jenkins FreeStyle Demo Project (Ansible + Jenkins + Git)

Jenkins FreeStyle Demo Project
Jenkins FreeStyle Demo Project

Jenkins FreeStyle Demo Project (Ansible + Jenkins + Git)

It’s a simple CI/CD Jenkins FreeStyle Demo Project. We associate Ansible and Git in this project and it is intended for the people who are new to CI/CD.

Prerequisites

  • Jenkins
  • Ansible
  • GitHub

Project Description

For this project, we need a GitHub account to keep our codes and an ansible server to deploy the codes to the desired hosts. We also need a Jenkins server to invoke an Ansible playbook which helps to deploy the codes to the desired hosts. Here the project goal is when we make a new push to GitHub code, the updated code needs to be automatically deployed on all the hosts using Ansible.

So Let’s start the project!

To make this project simple, I have installed Jenkins and Ansible on the same server. I have mentioned in my previous post about Jenkins installation. Please see it if you are new to Jenkins. Also, make sure that you have uploaded the public key to the ansible hosts.

Things to be done on Ansible Server:

We need an Ansible Playbook for deploying codes from GitHub to the target servers. I will provide a sample playbook below and save it in your desired location. I have stored the file in /var/project/demo-project.yml. Also, I have stored the host list in the file /var/project/inventory-file

---
- name: "CICD test project"
  hosts: all
  become: yes
  tasks:

    - name: "Installing GIT"
      when: ansible_os_family == "RedHat"
      yum:
        name: git
        state: present

    - name: "Installing GIT on Ubuntu"
      when: ansible_os_family == "Debian"
      apt:
        update_cache: yes
        name: git
        state: present

    - name: "Cloning git repo"
      git:
        repo: <Git_Repository_URL>
        dest: /tmp/repo

    - name: "Creating binary directory"
      file:
        path: /root/bin
        state: directory

    - name: "Copying files to bin directory"
      copy:
        src: "{{ item }}"
        dest: /root/bin/
        remote_src: yes
      with_items:
        - /home/repo/test_file_1
        - /home/repo/test_file_2

 

This playbook clones the contents of the Git repository to the directory /tmp/repo in Ansible/Jenkins server and transfers the contents to the /root/bin directory of remote hosts.

Things to be done on GitHub Repository:

Go to your desired repository and click on settings. Then Webhooks and Add Webhook.

GitHub Repository >> settings >> Webhooks >> Add Webhook

Then you need to give:

Payload URL: http://<Jenkins_Server_IP>:8080/github-webhook/
Content type: Application/json
Which events would you like to trigger this webhook?: Just the push event.
Active: Make sure it is checked

and Add Webhook. Please see the following screenshot for reference.

github

Things to be done on Jenkins Dashboard:

You need to enable the Ansible plugin on Jenkins to establish a connection with Jenkins and Ansible. So let’s check how to install Ansible Plugin on Jenkins.

From Jenkins Dashboard >> Manage Jenkins >> Manage Plugins >> Available >> Search for “Ansible” >> Install without Restart >> Then check the box “Restart Jenkins when installation is complete and no jobs are running”

Please see the image below:

Install ansible plugin

Then Go to Global Tool Configuration >> Ansible >> Name >> Path to ansible executables directory

See the below screenshot for reference:

Global Tool Configuration

Create a Jenkins job

Once you updated the Global tool configuration, you need to create a Jenkins job. The steps are given below:

1. Create a job

2. Enter an item Name
Enter desired project name. Here I am giving Demo Project

3. Freestyle project
Select the project type. I have selected the Freestyle project here. Then click OK.

4. Description
Add some description that relates to your project

5. Source Code Management
In Source Code Management you need to Select Git and add Repository URL (Fill with the desired repository URL)

6. Build Triggers
In the Build Triggers section, you have to select: GitHub hook trigger for GITScm polling

7. Build
In the build section, click on the dropdown of Add Build Step and select Invoke Ansible Playbook. Then you need to give the Playbook path (Fill the playbook path) and File or host list (Fill with inventory file path)

8. Save the project

See the following screenshot:

Create a Jenkins job

Once you saved the project you will get it at the Jenkins dashboard. This project will automatically build every time you make changes to your git repository.

That’s it!

If you like the post Understanding top Command and wish to receive more articles from us, please like our FB page: GrepItOut

Your suggestions and feedback will encourage us and help us to improve further, please feel free to write your comments. For more details on our services, please drop us an e-mail at info@grepitout.com

Topics