Automating CI/CD Pipeline Setup with Jenkins Using Ansible

Task Overview

The goal of this project is to automate the installation and configuration of Jenkins using Ansible. The setup will include:

Technology Stack

Task Architecture

1. Infrastructure Setup

2. Ansible Playbook for Jenkins Installation

3. Jenkins Configuration Automation

  • Use Ansible modules to:
  • 4. CI/CD Pipeline Configuration

    Step-by-Step Implementation

    Step 1: Setup Infrastructure

    Option 1: Manual Setup

    1. Create an EC2 instance on AWS (Ubuntu 22.04 / Amazon Linux 2).
    2. Update the package manager:
    3. sudo apt update -y
      sudo apt upgrade -y
      
    4. Install required dependencies
    5. sudo apt install -y openjdk-11-jdk git curl ansible
      

    Option 2: Automate with Terraform

    Use a Terraform script to create the EC2 instance and Security Group rules.

    Step 2: Install Jenkins with Ansible

    Create an Ansible Playbook (jenkins-install.yml)

    ---
    - name: Install and Configure Jenkins
      hosts: jenkins
      become: yes
      tasks:
    
        - name: Install Java
          apt:
            name: openjdk-11-jdk
            state: present
    
        - name: Add Jenkins Repository Key
          apt_key:
            url: https://pkg.jenkins.io/debian/jenkins.io.key
            state: present
    
        - name: Add Jenkins Repository
          apt_repository:
            repo: "deb http://pkg.jenkins.io/debian-stable binary/"
            state: present
    
        - name: Install Jenkins
          apt:
            name: jenkins
            state: present
            update_cache: yes
    
        - name: Start and Enable Jenkins
          systemd:
            name: jenkins
            enabled: yes
            state: started
    

    Run the Playbook

    ansible-playbook -i inventory jenkins-install.yml
    

    Step 3. Install Required Plugins

    Create an Ansible task file (jenkins-plugins.yml):

    ---
    - name: Install Jenkins Plugins
      hosts: jenkins
      become: yes
      tasks:
        - name: Install required Jenkins plugins
          jenkins_plugin:
            name: "{{ item }}"
            state: present
          with_items:
            - git
            - job-dsl
            - pipeline
            - ansicolor
            - docker-plugin
            - ansible

    Run the Playbook:

    ansible-playbook -i inventory jenkins-plugins.yml

    Step 4. Automate Job Creation

    Use Jenkins Job DSL Plugin to automate job creation.

    Create a Job DSL File (seed-job.groovy):

    pipelineJob('Sample-Pipeline') {
      definition {
        cps {
          script("""
            pipeline {
              agent any
              stages {
                stage('Clone Repo') {
                  steps {
                    git 'https://github.com/user/repo.git'
                  }
                }
                stage('Build') {
                  steps {
                    sh 'mvn clean install'
                  }
                }
              }
            }
          """.stripIndent())
        }
      }
    }

    Step 5. Configure Jenkins Pipeline Job with Ansible

    Create an Ansible Playbook for Job Configuration (jenkins-job.yml):

    ---
    - name: Create Jenkins Job
      hosts: jenkins
      tasks:
        - name: Create a new job
          uri:
            url: "http://localhost:8080/createItem?name=Sample-Pipeline"
            method: POST
            user: "admin"
            password: "admin"
            body: "{{ lookup('file', 'seed-job.groovy') }}"
            force_basic_auth: yes
            status_code: 200,201

    Run the Playbook

    ansible-playbook -i inventory jenkins-job.yml
    

    Step 6. Setup Webhook for CI/CD Trigger

    1. Generate API Token in Jenkins.
    2. Configure GitHub Webhook.
    3. Go to GitHub Repo → Settings → Webhooks.
    4. Add Jenkins URL: http://JENKINS_SERVER:8080/github-webhook/.
    5. Select "Just the push event".
    6. Enable Polling for SCM in Jenkins.

    Outcome

    Once the setup is complete:

    Next Steps

    This Task provides a fully automated Jenkins CI/CD pipeline using Ansible, enabling fast, reliable, and repeatable deployments! 🚀