Choosing Between Terraform and Ansible: Practical Examples and Guidance

🧩 Terraform vs Ansible: The Basics

FeatureTerraformAnsible
PurposeInfra provisioning (IaaC)App configuration & deployment
LanguageDeclarative (HCL)Procedural (YAML + Jinja2)
Works withCloud infra (AWS, GCP, Azure, etc.)OS-level (SSH, WinRM)
Agent requirementNoAgentless (via SSH)
IdempotencyBuilt-inManual (but supported)

✅ Use Case 1: When Only Terraform Makes Sense

🧩 Scenario:

You need to spin up 10 EC2 instances, an S3 bucket, and a VPC on AWS. The team's role is limited to infrastructure setup.

💡 Why Terraform?

Terraform is built for infrastructure provisioning. It tracks the state, plans changes, and integrates deeply with AWS. Since the team's role is limited to infrastructure setup, Terraform is ideal as it focuses on provisioning and managing infrastructure efficiently.

✅ Example Code:

hclCopyEditresource "aws_instance" "web" {
  count         = 10
  ami           = "ami-123456"
  instance_type = "t2.micro"
}

🚫 Why Not Ansible?

You can provision with Ansible using cloud modules, but it's not as robust or stateful. Plus, you'll miss out on Terraform’s built-in dependency tracking and execution plan.

✅ Use Case 2: When Only Ansible Makes Sense

🧩 Scenario:

You already have 5 servers running and need to install Nginx with custom config and different env vars on each.

💡 Why Ansible?

This is pure configuration. Ansible shines here — it can apply OS packages, copy files, manage services, and even support dynamic variables per host.

✅ Example Playbook:

yamlCopyEdit- hosts: webservers
  vars:
    nginx_port: 8080
  tasks:
    - name: Install nginx
      apt: name=nginx state=present
    - name: Start nginx
      service: name=nginx state=started

🚫 Why Not Terraform?

Terraform doesn’t natively handle per-host configuration. You could hack it in, but it's not meant for this level of granularity in system setup.


✅ Use Case 3: Terraform + Ansible Together (Best of Both)

🧩 Scenario:

You're building a new app environment:

  • Create EC2 servers on AWS

  • Configure them with Docker, Nginx, and your custom app

💡 Why Use Both?

  • Use Terraform to provision infrastructure (EC2, VPC, security groups)

  • Use Ansible to configure those servers (install packages, manage services)

✅ Terraform Code:

hclCopyEditresource "aws_instance" "app" {
  ami           = "ami-0abc123"
  instance_type = "t2.medium"
}

✅ Ansible Playbook:

yamlCopyEdit- hosts: all
  tasks:
    - name: Install Docker
      apt: name=docker.io state=present
    - name: Run App
      docker_container:
        name: myapp
        image: myapp:latest

🚫 Why Not Just One Tool?

  • Terraform doesn’t do configuration well.

  • Ansible isn’t ideal for spinning up cloud infra.


🧠 Final Thoughts

Use Case TypeTool
Infra provisioning✅ Terraform
App config, software management✅ Ansible
End-to-end infra + app✅ Both
0
Subscribe to my newsletter

Read articles from Priyanshu Sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Priyanshu Sharma
Priyanshu Sharma