A Comprehensive Guide to Setting Up Ansible on AWS EC2 with Terraform Provisioning
- Introduction to Ansible
- Why Use Ansible?
- Local Installation Guide
- Lab Setup: Terraform + AWS EC2
- Ansible Installation & Configuration
- Ansible Inventory & Host Management
- Verification & Testing
- Next Steps & Advanced Automation
- Visual Workflow Diagram
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.
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.
graph LR
A[Control Node] -->|SSH| B[Managed Node 1]
A -->|SSH| C[Managed Node 2]
A -->|SSH| D[Managed Node 3]
# Ubuntu/Debian
sudo apt update
sudo apt install ansible -y
# RHEL/CentOS
sudo yum install ansible -y
# Verify
ansible --version
# Using Homebrew
brew install ansible
# Verify
ansible --version
- Install WSL:
wsl --install
- Install Ubuntu from Microsoft Store
- Open WSL and run:
sudo apt update && sudo apt install ansible -y
Weβll deploy 4 instances (1 Ansible Master + 3 Managed Nodes).
# 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}" }
}
terraform init
terraform plan
terraform apply -auto-approve
# 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
mkdir -p ~/keys && chmod 700 ~/keys
vim ~/keys/terra-key-ansible.pem # Paste your private key
chmod 400 ~/keys/terra-key-ansible.pem
[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
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 }
ansible all -m ping
β Success Response:
web1 | SUCCESS => { "ping": "pong" }
web2 | SUCCESS => { "ping": "pong" }
web3 | SUCCESS => { "ping": "pong" }
π 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
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
π Ansible Documentation
π Terraform AWS Provider
π Ansible Galaxy (Pre-built Roles)
β 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! π