Skip to content

BaniprasadMangaraj/terraform-ansible-dev

Repository files navigation

πŸš€ Ansible Automation Mastery: Infrastructure as Code with Terraform & Ansible

A Comprehensive Guide to Setting Up Ansible on AWS EC2 with Terraform Provisioning


πŸ“Œ Table of Contents

  1. Introduction to Ansible
  2. Why Use Ansible?
  3. Local Installation Guide
  4. Lab Setup: Terraform + AWS EC2
  5. Ansible Installation & Configuration
  6. Ansible Inventory & Host Management
  7. Verification & Testing
  8. Next Steps & Advanced Automation
  9. Visual Workflow Diagram

πŸ€– Introduction to Ansible

Ansible is an open-source automation engine that simplifies:
βœ… Configuration Management (Consistent server setups)
βœ… Application Deployment (CI/CD pipelines)
βœ… Orchestration (Multi-tier workflows)
βœ… Security & Compliance (Automated hardening)

πŸ”Ή Agentless β†’ Uses SSH/WinRM (No extra software on nodes).
πŸ”Ή Idempotent β†’ Safe to rerun (No unintended side effects).
πŸ”Ή YAML-Based β†’ Easy-to-write playbooks.


⚑ Why Use Ansible?

Feature Traditional Approach Ansible Approach
Deployment Speed Manual, Slow Automated, Fast
Error-Prone? High (Human mistakes) Low (Consistent automation)
Scalability Difficult (Manual configs) Easy (Reusable playbooks)
Security Inconsistent Enforced via automation

πŸ’‘ Use Case: Automate web server deployments, database setups, cloud provisioning, and security patches with minimal effort.

How Ansible Works

graph LR
    A[Control Node] -->|SSH| B[Managed Node 1]
    A -->|SSH| C[Managed Node 2]
    A -->|SSH| D[Managed Node 3]
Loading

πŸ’» Local Installation Guide

Linux Installation

# Ubuntu/Debian
sudo apt update
sudo apt install ansible -y

# RHEL/CentOS
sudo yum install ansible -y

# Verify
ansible --version

macOS Installation

# Using Homebrew
brew install ansible

# Verify
ansible --version

Windows (WSL) Installation

  1. Install WSL:
    wsl --install
  2. Install Ubuntu from Microsoft Store
  3. Open WSL and run:
    sudo apt update && sudo apt install ansible -y

πŸ›  Lab Setup: Terraform + AWS EC2

πŸ”Ή Step 1: Terraform for EC2 Provisioning

We’ll deploy 4 instances (1 Ansible Master + 3 Managed Nodes).


main.tf

# Ansible Master Node
resource "aws_instance" "ansible_master" {
  ami           = "ami-0c55b159cbfafe1f0" # Ubuntu 20.04
  instance_type = "t2.micro"
  key_name      = "terra-key-ansible"
  tags = { Name = "Ansible-Control-Node" }
}

# Managed Nodes (3x)
resource "aws_instance" "managed_nodes" {
  count         = 3
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
  key_name      = "terra-key-ansible"
  tags = { Name = "Managed-Node-${count.index + 1}" }
}

πŸ”Έ Apply Terraform

terraform init
terraform plan
terraform apply -auto-approve

image

πŸ“₯ Ansible Installation & Configuration

πŸ”Ή Step 2: Install Ansible on Master Node

# Update & Install Ansible
sudo apt update && sudo apt install -y ansible

# Verify Installation
ansible --version

βœ… Expected Output:

ansible [core 2.12.x]
  config file = /etc/ansible/ansible.cfg
  python version = 3.8.10

image


πŸ“‚ Ansible Inventory & Host Management

πŸ”Ή Step 3: Configure Hosts & SSH Keys

mkdir -p ~/keys && chmod 700 ~/keys
vim ~/keys/terra-key-ansible.pem  # Paste your private key
chmod 400 ~/keys/terra-key-ansible.pem

πŸ”Ή Step 4: Modify /etc/ansible/hosts

[web_servers]
web1 ansible_host=<IP1>
web2 ansible_host=<IP2>
web3 ansible_host=<IP3>

[web_servers:vars]
ansible_user=ubuntu
ansible_python_interpreter=/usr/bin/python3
ansible_ssh_private_key_file=/home/ubuntu/keys/terra-key-ansible.pem

image


βœ” Verification & Testing

πŸ”Ή Check Inventory

ansible-inventory --list --yaml

Output:

all:
  children:
    web_servers:
      hosts:
        web1: { ansible_host: 10.0.1.10 }
        web2: { ansible_host: 10.0.1.11 }
        web3: { ansible_host: 10.0.1.12 }

image

πŸ”Ή Test Connectivity

ansible all -m ping

βœ… Success Response:

web1 | SUCCESS => { "ping": "pong" }
web2 | SUCCESS => { "ping": "pong" }
web3 | SUCCESS => { "ping": "pong" }

πŸš€ Next Steps & Advanced Automation

πŸ“Œ Ad-Hoc Commands

ansible all -a "free -h"  # Check memory
ansible all -a "df -h"    # Check disk space

πŸ“Œ Create Your First Playbook (deploy_nginx.yml)

---
- name: Install & Start Nginx
  hosts: web_servers
  tasks:
    - name: Install Nginx
      apt: name=nginx state=present
    - name: Start Nginx
      service: name=nginx state=started

β–Ά Run Playbook:

ansible-playbook deploy_nginx.yml

πŸ“Š Visual Workflow Diagram

graph TD
    A[Terraform Apply] -->|Creates| B[4x EC2 Instances]
    B --> C[Ansible Master]
    B --> D[Managed Node 1]
    B --> E[Managed Node 2]
    B --> F[Managed Node 3]
    C -->|SSH Keys| D
    C -->|Ansible Playbooks| E
    C -->|Automation| F
Loading

πŸ”— Resources

πŸ“š Ansible Documentation
πŸ“š Terraform AWS Provider
πŸ“š Ansible Galaxy (Pre-built Roles)


🎯 Key Takeaways

βœ” Infrastructure as Code (IaC) β†’ Terraform + Ansible = Full Automation
βœ” Agentless & Scalable β†’ Manage 100s of servers with minimal setup.
βœ” YAML Simplicity β†’ No complex scripting needed.

πŸš€ Now go automate everything! πŸš€

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages