All comparisonsInfrastructure as Code

Terraform vs Ansible

Terraform is a declarative provisioning tool: you describe the desired end state (a VPC, a database, a cluster) and it figures out what to create, change, or destroy to get there, tracking everything in a state file. Ansible is primarily a configuration management and automation tool: it runs ordered tasks (install a package, edit a config, restart a service) against machines that already exist, using a push model over SSH with no state file.

Left

Terraform

Right

Ansible

ModelDeclarative, desired-stateProcedural, ordered tasks (playbooks)
State trackingKeeps a state file to diff againstNo state file, re-runs tasks idempotently
Primary jobCreate/destroy cloud resourcesConfigure OS and app settings on existing hosts
ExecutionPlan then applyPush tasks over SSH/WinRM
OverlapCan call provisioners for setupHas cloud modules for provisioning too

Use Terraform when

You need to create, version, and safely change cloud infrastructure itself, networks, compute, managed services.

Use Ansible when

The infrastructure already exists and you need to configure software on it, or run repeatable operational tasks.

The verdict

Many teams use both: Terraform to stand up the servers, Ansible to configure what runs on them. Pick based on whether the job is "create infrastructure" or "configure a machine".

Study this on the roadmap

Terraform & IaC