Introduction
Modern DevOps practices rely heavily on automation tools to streamline infrastructure provisioning and configuration management. Jenkins, Terraform, and Ansible are widely used tools that work seamlessly to accomplish these tasks. In this guide, we demonstrate how to orchestrate the process using Jenkins, provision the infrastructure with Terraform, and finalize configurations with Ansible. We also integrate GitHub securely using a GitHub App Token.
---
1. Workflow Overview
The workflow involves three primary tools:
- Jenkins: CI/CD orchestrator that triggers and manages the process.
- Terraform: Automates infrastructure provisioning (e.g., servers, networks).
- Ansible: Configures the provisioned infrastructure (e.g., installing software, managing services).
Key Benefits
- Automation: Eliminates manual effort, reducing errors.
- Scalability: Easily adaptable to different environments.
- Security: Uses GitHub App Tokens for secure repository access.
---
2. Setting Up Jenkins
Installation
1. Download and install Jenkins from [the official Jenkins site](https://www.jenkins.io/download/).
2. Configure Jenkins with necessary plugins:
- Terraform
- Ansible
- GitHub Integration
Configure GitHub Token in Jenkins
1. Create a GitHub App Token:
- Go to Settings > Developer Settings > Personal Access Tokens > Fine-grained Tokens.
- Generate a token with read-only access to repositories.
2. Add the token to Jenkins:
- Navigate to Manage Jenkins > Credentials > Global Credentials.
- Add the GitHub token with an appropriate ID (e.g., github-token).
---
3. Writing the Terraform Configuration
Example Terraform Script
Here’s a sample configuration to create an AWS EC2 instance:
``hcl
provider "aws" {
region = "us-west-2"
}
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "MyTerraformInstance"
}
}
`
Store this Terraform script in a GitHub repository (e.g., infrastructure/terraform).
---
4. Writing Ansible Playbooks
Example Playbook
The playbook below installs Apache on the provisioned server:
`yaml
---
- name: Configure Web Server
hosts: all
become: yes
tasks:
- name: Install Apache
apt:
name: apache2
state: present
- name: Start Apache Service
service:
name: apache2
state: started
`
Store this playbook in the same GitHub repository under infrastructure/ansible.
---
5. Creating the Jenkins Pipeline
Pipeline Script
Below is the Jenkins pipeline script:
``groovy
pipeline {
agent any
environment {
GITHUB_TOKEN = credentials('github-token') // Use the GitHub token stored in Jenkins
}
stages {
stage('Clone Repository') {
steps {
script {
sh 'git clone https://${GITHUB_TOKEN}@github.c