Terraform vs Ansible

Comparing Two Powerful Infrastructure Automation Tools

Introduction to Terraform

Overview

What is Terraform

Terraform was developed by HashiCorp and released in 2014 as an infrastructure as code (IaC) tool. It enables users to define both cloud and on-premises resources in human-readable configuration files that can be versioned, reused, and shared. Terraform uses a declarative language to describe the desired infrastructure state, and it automatically creates an execution plan that shows what changes will be made before applying them. It's designed to manage complex infrastructure across multiple cloud providers and services.

Declarative Infrastructure as Code


terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
    }
  }
}

provider "aws" {
  alias   = "user"
  region  = var.region
  profile = var.profile
}
resource "aws_s3_bucket" "example" {
  provider      = aws.user
  bucket        = var.bucket_name
  acl           = var.acl_value
  force_destroy = "false"
}

resource "aws_s3_bucket_object" "object2" {
  for_each = fileset("files/", "*")
  bucket   = aws_s3_bucket.example.bucket
  key      = "new_objects"
  source   = "files/${each.value}"
  etag     = filemd5("files/${each.value}")
}

State Management

Box with shadows

Provider Ecosystem

Tower and Logos

What are the trade offs?

Advantages

  1. Multi-Cloud Management: Terraform excels at managing infrastructure across multiple cloud providers simultaneously, using a consistent workflow and language regardless of the target environment.
  2. Immutable Infrastructure: Terraform promotes an immutable infrastructure approach where resources are replaced rather than modified in-place, leading to more consistent, predictable, and reproducible infrastructure.
  3. Infrastructure Versioning: Terraform configuration files can be stored in version control systems, allowing teams to track changes, collaborate effectively, and roll back to previous infrastructure states when needed.
  4. Dependency Management: Terraform automatically handles resource dependencies, ensuring that resources are created, updated, or destroyed in the correct order without manual intervention.
  5. Plan and Apply Workflow: Terraform's plan stage shows exactly what changes will be made before applying them, reducing the risk of unexpected changes and providing an opportunity for review and approval.

Disadvantages

  1. Limited Configuration Management: While excellent at provisioning infrastructure, Terraform has limited capabilities for configuring the internal state of resources like operating systems or application settings.
  2. State File Management Complexity: Managing Terraform state files in team environments requires careful planning and often additional tooling to prevent conflicts and ensure secure access.
  3. Steep Learning Curve: Learning Terraform's declarative approach and HCL syntax can be challenging for teams more familiar with procedural programming or script-based automation.
  4. Handling Existing Infrastructure: Importing existing infrastructure into Terraform management can be cumbersome and sometimes requires manual intervention or custom scripting.
  5. Limited Support for Ad-hoc Tasks: Terraform is not well-suited for one-off or ad-hoc operations that don't fit into its declarative model of describing desired infrastructure states.

Introduction to Ansible

Overview

What is Ansible

Ansible was originally created by Michael DeHaan in 2012 and was later acquired by Red Hat in 2015. It's designed as an agentless automation platform that uses simple YAML syntax for configuration management, application deployment, and task automation. Unlike many configuration management tools, Ansible doesn't require any special software to be installed on nodes it manages, relying instead on SSH for Linux/Unix systems and WinRM for Windows systems. This agentless architecture makes Ansible particularly easy to deploy and use in existing environments.

Agentless Architecture

Build model

YAML-Based Playbooks


---
- name: Install and configure Apache web server
  hosts: webservers # Targets the group named "webservers" from your inventory
  become: yes # Runs tasks with root privileges (e.g., sudo)

  tasks:
    - name: Ensure Apache is installed
      ansible.builtin.apt: # Uses the apt module for Debian/Ubuntu systems
        name: apache2
        state: present # Ensures the package is installed

    - name: Deploy custom Apache configuration file
      ansible.builtin.template:
        src: templates/apache.conf.j2 # Path to your template file
        dest: /etc/apache2/sites-available/000-default.conf # Destination on the remote server
        owner: root
        group: root
        mode: '0644'
      notify: Restart Apache # Calls the "Restart Apache" handler if the config file changes

  handlers:
    - name: Restart Apache
      ansible.builtin.service:
        name: apache2
        state: restarted

Extensive Module Library

Connect dots with check marks

What are the trade offs?

Advantages

  1. Simple Learning Curve: Ansible's YAML-based syntax is easy to read and write, making it accessible to users with various technical backgrounds and reducing the time needed to create effective automation.
  2. Cross-Platform Compatibility: Ansible works across diverse environments including Linux, Windows, network devices, and cloud platforms, allowing teams to use a single tool for different infrastructure components.
  3. Minimal Requirements: The agentless architecture means there's no additional software to install, update, or maintain on managed nodes, simplifying deployment and reducing potential security risks.
  4. Configuration Management: Ansible excels at configuring systems post-deployment, including installing software, managing users, setting up services, and ensuring consistent configurations across environments.
  5. Idempotent Operations: Ansible tasks are designed to be idempotent, meaning they can be run multiple times without causing unintended side effects, making automation more reliable and predictable.

Disadvantages

  1. Limited State Management: Unlike Terraform, Ansible doesn't maintain a state file of managed resources, making it harder to track what has been deployed and detect configuration drift over time.
  2. Performance at Scale: Ansible's SSH-based connection model can face performance challenges when managing thousands of nodes simultaneously, potentially causing slower execution for large-scale deployments.
  3. Procedural Approach: Ansible's procedural programming model requires specifying steps to reach a desired state, which can be more verbose and complex for infrastructure provisioning compared to Terraform's declarative approach.
  4. Error Handling: While improving in recent versions, Ansible's error reporting and handling can sometimes be cryptic or insufficient for troubleshooting complex automation failures.
  5. Limited Cloud Provisioning Capabilities: Although Ansible can provision cloud resources, it lacks some of the sophisticated infrastructure lifecycle management features found in dedicated IaC tools like Terraform.

Terraform vs Ansible: Head-to-Head Comparison

Primary Purpose

Terraform

Primarily designed for infrastructure provisioning and lifecycle management across multiple cloud providers and services.

Ansible

Primarily designed for configuration management, application deployment, and task automation across servers and other resources.

Programming Approach

Terraform

Uses a declarative approach where you specify the desired end state, and Terraform determines how to achieve it. Written in HashiCorp Configuration Language (HCL).

Ansible

Uses a procedural (imperative) approach where you define specific tasks and their sequence. Written in YAML, making it more accessible to non-programmers.

State Management

Terraform

Maintains state files that track the current state of managed infrastructure, enabling planning and change detection before applying updates.

Ansible

Does not maintain persistent state files. Relies on idempotent operations that can be safely run multiple times to achieve and maintain the desired state.

Architecture

Terraform

Uses a client-only architecture with API calls to various providers. State can be stored locally or remotely in backends like S3, Consul, or Terraform Cloud.

Ansible

Uses an agentless architecture that connects to managed nodes via SSH (Linux) or WinRM (Windows), with no software required on target systems beyond Python.

Infrastructure Modifications

Terraform

Promotes immutable infrastructure where resources are replaced rather than modified in-place when changes are needed.

Ansible

Supports mutable infrastructure with in-place modifications to existing resources, though it can also be used in immutable approaches.

Handling Drift

Terraform

Detects drift by comparing current state with state files and can automatically remediate by bringing infrastructure back to the desired state.

Ansible

Can detect and remediate drift through periodic execution of playbooks, but lacks built-in drift detection without additional tooling.

Choosing Between Terraform and Ansible

Terraform is ideal for:

Ansible is ideal for:

Are you looking for a better CI experience?

Start turning complexity into an advantage

Create an account to get started with a 30-day free trial. No credit card required.

Buildkite Pipelines

Platform

  1. Pipelines
  2. Public pipelines
  3. Test Engine
  4. Package Registries
  5. Mobile Delivery Cloud
  6. Pricing

Hosting options

  1. Self-hosted agents
  2. Mac hosted agents
  3. Linux hosted agents

Resources

  1. Docs
  2. Blog
  3. Changelog
  4. Example pipelines
  5. Plugins
  6. Webinars
  7. Case studies
  8. Events
  9. Migration Services
  10. Comparisons

Company

  1. About
  2. Careers
  3. Press
  4. Security
  5. Brand assets
  6. Contact

Solutions

  1. Replace Jenkins
  2. Workflows for AI/ML
  3. Testing at scale
  4. Monorepo mojo
  5. Bazel orchestration

Legal

  1. Terms of Service
  2. Acceptable Use Policy
  3. Privacy Policy
  4. Subprocessors
  5. Service Level Agreement

Support

  1. System status
  2. Forum
© Buildkite Pty Ltd 2025