Terraform Archive Provider - Create ZIP and TAR Files
Use the Terraform archive provider to create ZIP files for Lambda functions, Cloud Functions, and deployments. archive_file data source with source_dir and...
DevOps
Compare Terraform and Ansible for infrastructure automation. Learn when to use each tool, their strengths, and how to combine them for maximum efficiency.
| Criterion | Terraform | Ansible |
|---|---|---|
| Primary purpose | Provision infrastructure | Configure & orchestrate systems |
| Paradigm | Declarative | Procedural (with declarative modules) |
| Language | HCL | YAML |
| State | Stateful | Stateless |
| Best for | Cloud IaC across AWS / Azure / GCP | OS config & app deploys |
Terraform and Ansible are two of the most popular automation tools in the DevOps ecosystem. While they can overlap in functionality, they excel in different areas. Understanding when to use each — and how to combine them — is key to efficient infrastructure management.
| Feature | Terraform | Ansible |
|---|---|---|
| Primary Use | Infrastructure provisioning | Configuration management |
| Language | HCL (HashiCorp Configuration Language) | YAML (Playbooks) |
| Approach | Declarative | Procedural + Declarative |
| State | Maintains state file | Stateless |
| Agent | Agentless | Agentless |
| Idempotency | Built-in | Module-dependent |
Terraform excels at infrastructure provisioning:
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t3.micro"
tags = {
Name = "web-server"
}
}Ansible excels at configuration management:
- name: Configure web server
hosts: web_servers
tasks:
- name: Install nginx
apt:
name: nginx
state: present
- name: Start nginx
service:
name: nginx
state: started
enabled: yesThe most powerful approach combines both tools:
# Terraform creates the instance
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t3.micro"
key_name = "deploy-key"
}
# Output IP for Ansible
output "web_ip" {
value = aws_instance.web.public_ip
}# Ansible configures it
- hosts: "{{ web_ip }}"
tasks:
- name: Deploy application
git:
repo: https://github.com/myapp.git
dest: /opt/myappTerraform tracks state — it knows what exists and what changed. Ansible is stateless — it checks current state on each run.
Terraform advantage: Can detect and fix drift automatically. Ansible advantage: No state file to manage or corrupt.
Ansible uses YAML, which most engineers already know. Terraform uses HCL, which requires learning a new syntax.
Terraform has providers for 3000+ services. Ansible has 7000+ modules covering everything from cloud to network devices.
Choose Terraform when:
Choose Ansible when:
Choose both when:
Related: Fix the Terraform inconsistent dependency lock file error — quick fix for this common issue.
Terraform and Ansible are complementary tools, not competitors. Use Terraform for infrastructure provisioning and Ansible for configuration management. Together, they form a powerful automation pipeline.
Use the Terraform archive provider to create ZIP files for Lambda functions, Cloud Functions, and deployments. archive_file data source with source_dir and...
Automate Terraform with Azure DevOps Pipelines. YAML pipelines, service connections, environment approvals, and Azure backend state configuration.
Automate Terraform with GitHub Actions. Plan on PR, apply on merge, OIDC authentication, environment protection, and drift detection workflows.
Automate Terraform with GitLab CI/CD. Plan on merge requests, apply on main, remote state with HTTP backend, and environment-specific pipelines.