Back to Blog
Cloud & DevOpsIntermediate

Infrastructure as Code: Terraform vs Ansible

Simha Infobiz
May 29, 2024
6 min read

Infrastructure as Code (IaC) treats your server configuration just like software code. It's versioned, tested, and reproducible. No more clicking buttons in the AWS console.

Terraform: The Architect (Provisioning)

Terraform talks to the cloud APIs (AWS, Azure, GCP) to create the hardware.

  • Declarative: You say "I want 3 servers," not "Create server 1, then create server 2..."
  • State: Terraform keeps a state file (terraform.tfstate) mapping your code to real-world resources.
  • Drift Detection: If someone manually deletes a server in the AWS console, Terraform notices the "drift" and recreates it to match your code.

Ansible: The Mechanic (Configuration)

Ansible logs into the servers Terraform created and installs software.

  • Agentless: It works over standard SSH. You don't need to install any agents on the target servers.
  • Idempotency: You can run the same script 100 times, and it won't break anything. It only makes changes if necessary (e.g., "Ensure Nginx is installed" - if it's already there, do nothing).

The Power Combo

Use Terraform to build the VPC, Load Balancers, and Databases. Use Ansible to configure the OS, security patches, and application runtime.

Key Takeaway

Treat your infrastructure like cattle, not pets. If a server acts up, don't fix it; kill it and provision a new one using your code. This is the only way to manage scale.

IaCTerraformAnsible
Share: