Terraform interview question · Most asked
Terraform and Ansible: how do they differ, and do you need both?
Summary
Terraform is declarative provisioning of infrastructure through APIs, tracking state to manage the full lifecycle (create, update, destroy). Ansible is primarily configuration management and orchestration: it connects to machines (SSH, WinRM) or APIs and runs tasks, usually without a state file, best for configuring operating systems and software on existing hosts.
Longer Answer
Common split: Terraform creates networks, instances and managed services; Ansible (or images built with Packer) configures the software on hosts. With immutable infrastructure and containers, much of the configuration-management need moves into image builds, so many teams need Ansible less. Both can technically overlap, but using each for its strength avoids fighting the tool.
Key Points
- Terraform: declarative provisioning with state.
- Ansible: configuration and orchestration.
- Often used together.
- Immutable images reduce config management needs.
More most-asked Terraform questions
- How do you handle secrets in Terraform?
- How would you run Terraform in CI/CD?
- What is Terraform state, why is it needed, and where should it be stored?
- count or for_each: when do you use each?
- Walk through terraform init, plan and apply. What does each do?
- What is Terraform, and how does it work at a high level?
- What are Terraform modules, and how do you design good ones?
- A terraform plan wants to destroy and recreate a database. What do you do?