What news from AWS re:Invent last week will have the most impact on you?
Amazon Q, an AI chatbot for explaining how AWS works.
Super-fast S3 Express storage.
New Graviton 4 processor instances.
Emily Freeman leaving AWS.
I don't use AWS, so none of this will affect me.
Software Development

Terraform vs. Ansible: Which Is Best for You?

While Ansible and Terraform are similar, they have several differences. Choose based on where your team needs help – orchestration or configuration.
Sep 12th, 2022 1:00am by
Featued image for: Terraform vs. Ansible: Which Is Best for You?
Feature image via Unsplash.

Derek Morgan
Derek is a developer advocate at Spacelift. He has worked in many environments, ranging from an international managed-hosting provider handling “actual metal” to managed service providers to major training corporations. He has also started a manufacturing technology provider using Kubernetes and other cutting-edge tools. Derek also owns a training company,

Organizations are moving toward multicloud infrastructure for better speed, security and reliability. The manual management of large-scale infrastructure with multiple cloud providers is not feasible.

That’s why businesses use Infrastructure-as-Code (IaC) services. It enables organizations to define the infrastructure in code.

The coded infrastructure is easy to produce, maintain, scale and replicate. It simplifies the provisioning, configuration and deployment of infrastructure.

Among the many IaC tools available, Ansible and Terraform are quite popular with many organizations. While Ansible and Terraform have similar functionalities, they have several fundamental differences. We will look closely at these differences in this article.

What Is Terraform?

Terraform is an open source IaC tool that manages and provisions infrastructure across multiple cloud providers, including AWS, Azure, Linode and Oracle. It can connect different infrastructure hosts using APIs to achieve complex management scenarios. The functionality can be extended using providers and community-built modules due to Terraform’s modularity.

What Is Ansible?

Ansible is an open source IT-automation tool designed to automate provisioning, configuration management, application deployment and manual IT processes. Though it can be used for provisioning, Ansible shines in configuring existing infrastructure and automating everyday infrastructure tasks.

Difference between Terraform and Ansible:

Terraform and Ansible are capable of managing the infrastructure and critical tasks associated with it. Many developers use them side by side, as one is good with provisioning and the other with configuration.

But when you have to pick only one for the operation, you must analyze the differences between Terraform and Ansible:

  1. Orchestration vs. configuration
  2. HCL vs. YAML
  3. Declarative vs. procedural
  4. Mutable vs. immutable
  5. State management
  6. Cloud vs. on premises

1. Orchestration vs. Configuration Management

Orchestration is the process of creating the infrastructure and connecting multiple cloud providers to build virtual machines, network components and databases. The process keeps the infrastructure in the state necessary to develop and run the application.

Configuration management includes installing the software on the infrastructure, performing automated, routine IT tasks and patching/replacing the damaged software.

Terraform has a wide range of solutions to deploy and maintain scalable infrastructures. The immutable code, declarative scripts, state management and community-built templates make Terraform a flexible provisioning service.

Ansible focuses on the configuration side of activities. The mutable logic and procedural script are helpful to quickly update the applications and keep the dependencies in a functional state.

Both Terraform and Ansible can do orchestration and configuration. However, Terraform is an orchestration tool, and Ansible leans toward configuration.

2. HCL vs. YAML

Terraform users define the resources with HashiCorp Configuration Language (HCL). It is an interpreted language designed to be used for infrastructure deployment.

Ansible uses YAML syntax, which is a data-serialization language designed for configuration management.

3. Declarative vs. Procedural

A machine can take instructions in two ways:

  • Procedural: Step-by-step instructions are given to the machine to perform the tasks. The system follows the checklist from top to bottom to achieve the target.
  • Declarative: The result is defined, and the machine can take any approach to get the result.

Terraform’s language, HCL, follows the declarative logic. Developers define their infrastructure and use the terraform apply command to take the infrastructure to the desired state. The system reads the state file and adds, removes and modifies the resource accordingly.

Ansible is procedural, meaning the script, known as an Ansible Playbook, will be executed top to bottom. It is like providing a blueprint containing the serialized steps to perform an action.

4. Mutable vs. Immutable

Immutable infrastructure means a component of infrastructure cannot be changed and replaced. If you have to change one element, you must build the new infrastructure with the updated element and replace the old one with it.

Modifying a single component within the infrastructure poses a compatibility risk. Creating a separate one, testing it for reliability and then using it in place of the old one is safe.

Terraform builds immutable infrastructure.

But it’s a time- and resource-consuming process and cannot be used for frequent tasks, like changing web servers or updating firewalls. That’s why Ansible is more suited for configuration, as it is mutable and can repair/change the dependencies quickly, without recreating everything from the ground.

5. State Management

The Terraform state files store the updated information of infrastructure resources. Terraform compares the state file data with the existing resources and modifies the infrastructure resources as needed. Any drift and changes to the infrastructure are saved to the file. You can query the Terraform state files anytime to learn the current infrastructure components.

Ansible does not track the existing configuration state. The changes intended are executed automatically on all the target machines. It maintains the existing state according to the changes introduced, but you have to query the current state to know the execution details.

When it comes to state management, Terraform provides a comprehensive solution.

6. Cloud vs. On Premises

Ansible does not differentiate between the configuration of local and cloud infrastructure. Once the provisioning is complete, you can configure the components using Ansible.

Terraform deals with APIs to use cloud providers’ resources to set up an environment. Hence, its functionality is limited in the local infrastructure. Ansible is a better choice if the infrastructure is set up locally.

Terraform and Ansible with Spacelift

Wouldn’t it be great if you could use Terraform for orchestration and Ansible for configuration?

Spacelift makes it possible and more manageable. Spacelift has historically been capable of managing the rest of the ecosystem with Terraform, Pulumi, AWS CloudFormation and more, and now it is gaining another large piece of the infrastructure puzzle with Ansible support.

Spacelift is a sophisticated, continuous integration and deployment (CI/CD) platform for Infrastructure-as-Code. It’s designed and implemented by longtime DevOps practitioners based on previous experience with large-scale installations.

You can go from zero to fully managing your cloud resources within less than a minute, with no prerequisites. It integrates nicely with the large players in the field — notably GitHub and AWS.

Terraform vs. Ansible: Which One to Select?

Terraform is better for creating and maintaining cloud resources. The always-updated state file keeps the infrastructure information relevant, making replication easier. Writing declarative code using HCL is straightforward. The community-made modules provide customizable templates for provisioning.

Ansible is a good option for managing configurations. The mutable configuration allows the users to change buggy elements quickly. Ansible can configure the local infrastructure too. Ansible uses YAML, which is specifically designed to create configuration files.

The better option, however, is using them both through Spacelift.

But if you can only select one, check your infrastructure and find where the team needs help — orchestration or configuration. Then choose the IaC service accordingly.

Group Created with Sketch.
TNS owner Insight Partners is an investor in: Pragma, Spacelift, Docker.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.