Introduction
When I started learning Cloud Computing, I was confused by the sheer number of tools. I knew Terraform was for "Infrastructure as Code." I knew Ansible was for "Configuration Management."
But then I saw people creating AWS EC2 instances using Ansible... and I saw people running shell scripts using Terraform. I asked myself: If they can both do the same things, why do we need both?
After digging into the documentation and building a few labs, I realized that while there is overlap, they have completely different philosophies. Here is what I learned about the battle between Provisioning and Configuration.
The Core Difference: Builder vs. Interior Designer
The best way to visualize the difference is to imagine building a house.
- Terraform is the Builder (Provisioning) Terraform is designed to create the infrastructure from scratch.
- It pours the concrete foundation.
- It builds the walls.
It installs the plumbing and electricity.
In Cloud terms: It creates your VPC, Subnets, EC2 Instances, and Databases.Ansible is the Interior Designer (Configuration)
Ansible is designed to setup the house once it exists.It paints the walls.
It installs the furniture.
It makes sure the TV is plugged in.
In Cloud terms: It installs Nginx, updates software patches, creates user accounts, and deploys your application code.
The "State" Debate: Why not just use Ansible for everything?
This was my biggest question. Since Ansible has modules to create EC2 instances, why bother learning Terraform?
The answer lies in one file: terraform.tfstate.
Terraform has "Memory" (Stateful)
When Terraform creates a server, it writes down the details in a State File. It remembers exactly what it built. If you delete a server from your Terraform code and run it again, Terraform looks at its memory (State file), sees that the server shouldn't exist anymore, and destroys it.
Ansible is "Forgetful" (Stateless)
Ansible doesn't have a memory of what it did last time. It just follows your current instructions list. If you remove the "Create Server" task from your Ansible code, Ansible doesn't delete the server. It just ignores it. This leads to "Configuration Drift"—where you have "ghost" servers running that you forgot about, costing you money.
How they work together
In the real world, you rarely pick just one. A standard DevOps pipeline looks like this:
- Terraform builds the empty servers and networking.
- Terraform calls Ansible automatically.
- Ansible connects to those new servers and installs the application.
Conclusion
You can use a hammer to drive a screw, but it's going to be messy.
- Use Terraform to build the house.
- Use Ansible to make it a home.
I have currently started learning terraform and in the future i will be learning ansible also, if you have any suggestions or tips you can comment below.
Top comments (0)