AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Using Jenkins to Build Infrastructure with Terraform and Configure with Ansible

By Luca Berton · Published 2024-01-01 · Category: installation

Learn how to automate infrastructure provisioning using Jenkins, Terraform, and Ansible, including secure repository access through a GitHub App Token.

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.

---

See also: AAP 2.6 CI/CD Pipeline Integration: GitOps Workflows with Jenkins, GitLab, and GitHub Actions

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

Download and install Jenkins from the official Jenkins site. Configure Jenkins with necessary plugins: • TerraformAnsibleGitHub Integration

Configure GitHub Token in Jenkins

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. Add the token to Jenkins: • Navigate to Manage Jenkins > Credentials > Global Credentials. • Add the GitHub token with an appropriate ID (e.g., github-token).

---

See also: Ansible vs GitHub Actions: Key Differences & When to Use Each (2026)

3. Writing the Terraform Configuration

Example Terraform Script

Here’s a sample configuration to create an AWS EC2 instance:
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:
---
- 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.

---

See also: Automating Jenkins Installation with Ansible

5. Creating the Jenkins Pipeline

Pipeline Script

Below is the Jenkins pipeline script:
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.com/username/repository.git'
                }
            }
        }
        stage('Terraform Init') {
            steps {
                dir('repository/infrastructure/terraform') {
                    sh 'terraform init'
                }
            }
        }
        stage('Terraform Apply') {
            steps {
                dir('repository/infrastructure/terraform') {
                    sh 'terraform apply -auto-approve'
                }
            }
        }
        stage('Run Ansible Playbook') {
            steps {
                dir('repository/infrastructure/ansible') {
                    sh '''
                    ansible-playbook -i inventory playbook.yml
                    '''
                }
            }
        }
    }
}

Replace username and repository with your GitHub details.

---

6. Running the Pipeline

Triggering the Pipeline

Navigate to the Jenkins job. Click Build Now to trigger the pipeline. Monitor the logs to ensure successful execution of each stage.

Expected Output

• Terraform provisions the infrastructure. • Ansible configures the infrastructure (e.g., installs and starts Apache). • Logs display successful completion messages for each stage.

---

7. Best Practices

Secure Credentials: Always use Jenkins secrets to store sensitive information. • Use Modules and Roles: Modularize Terraform configurations and Ansible playbooks for better maintainability. • Test Locally: Validate Terraform plans and Ansible playbooks locally before running them in Jenkins.

---

Conclusion

By combining Jenkins, Terraform, and Ansible, you can build a robust automation pipeline for infrastructure provisioning and configuration. Leveraging GitHub App Tokens enhances security, while the modularity of the tools ensures scalability and flexibility. Start integrating these tools into your DevOps practices today for efficient and reliable automation!

Related Articles

Ansible Become GuideAnsible Inventory Guidethe Ansible AWS referencebuilding reusable Ansible roles

Category: installation

Browse all Ansible tutorials · AnsiblePilot Home