Ansible vs Terraform

Infrastructure automation and configuration management are two essential practices in modern IT operations, particularly in the DevOps & Cloud.

Ansible vs Terraform
Image is subject to copyright!

Imagine, As your business grows bigger, setting up and configuring things like servers, databases, and networks becomes a real pain if you do it by hand. Spinning up new servers one by one, copying and pasting commands, and keeping track of everything is super time-consuming and mistakes happen easily. Keeping all your servers configured the same way, installing updates, and deploying new code versions is a nightmare without help.

That's where tools like Terraform and Ansible come to the rescue. Terraform is awesome at setting up your infrastructure like servers, networks, and databases. Instead of doing everything manually, you write code that describes what you want, and Terraform handles it for you. Need 10 new servers? Terraform gets it done with just a few commands.

Ansible is the superhero for configuring those servers. You create instructions called playbooks that tell Ansible how to set up each server. Install software? Check. Deploy your code? Check. Update everything? Easy peasy. Ansible makes sure all your servers are configured consistently.

But it gets even better! With Terraform and Ansible, you have your entire infrastructure and configurations defined as code. This makes it super easy to see what changed, roll back to an earlier version if needed, and prove you're following regulations.

IaC: Infrastructure as Code.

6 Reasons: Why Do We Need Infrastructure Automation and Configuration Management (IaC) Tools?

  1. Eliminating Manual Errors
  2. Increasing Speed and Efficiency
  3. Ensuring Consistency and Standardization
  4. Improving Security and Compliance
  5. Facilitating Collaboration and Version Control
  6. Simplifying Disaster Recovery and Rollbacks

Infrastructure automation refers to the process of using software tools and scripts to automate the provisioning, configuration, and management of IT infrastructure resources, such as virtual machines, networks, load balancers, and storage systems.

Configuration management is the practice of maintaining the desired state and configuration of IT systems and services throughout their lifecycle. It involves tracking, managing, and updating the configuration items (CIs) that make up an IT system, such as software, hardware, and network devices.

How Companies Are Saving Millions by Migrating Away from AWS to Bare Metal Servers?
Many startups initially launch on AWS or other public clouds because it allows rapid scaling without upfront investments. But as these companies grow, the operating costs steadily rise.

Terraform: The Infrastructure Provisioning Tool

Terraform is a declarative infrastructure provisioning tool that shines when it comes to spinning up resources across multiple cloud providers.

Terraform's strengths lie in:

1. Declarative Approach

Terraform's declarative nature means you define your desired infrastructure state, and it figures out the necessary steps to achieve that state. This approach simplifies complex provisioning scenarios and ensures consistent, repeatable deployments.

2. State Management and Lifecycle Management

Terraform maintains a state file that tracks the resources it has provisioned. This state file enables seamless lifecycle management, allowing you to modify or delete resources without manual intervention. If you remove a resource from your configuration, Terraform will automatically delete it during the next application.

3. Idempotency

Terraform is idempotent, meaning you can run it multiple times without unintended changes to your infrastructure. If no changes are required, Terraform won't perform any actions, saving you time and effort.

DevOps vs SRE vs Platform Engineering - Explained
At small companies, engineers often wear multiple hats, juggling a mix of responsibilities. Large companies have specialized teams with clearly defined roles in DevOps, SRE, and Platform Engineering.

Ansible: The Configuration Management Tool

Ansible is a powerful configuration management tool that excels at automating application deployments, system configurations, and ongoing maintenance tasks.

Ansible strengths are:

1. Hybrid Approach

Ansible combines declarative resources with procedural execution, providing a flexible approach to configuration management. While resources are defined in a declarative manner, they are executed procedurally.

2. Application Deployment

Ansible simplifies the process of deploying applications by automating tasks like installing dependencies, copying files, and starting services.

3. Agentless

Unlike some other configuration management tools, Ansible doesn't require an agent to be installed on the managed nodes. This agentless architecture makes it easy to manage diverse environments without additional overhead.

System Design vs Software Architecture : What You Need to Know
System design and software architecture are two sides of same coin, both integral to the process of creating a software system. But, they’re not the same thing.

Ansible vs Terraform

Ansible vs Terraform
Category Ansible Terraform
Primary Focus Configuration management, application deployment, orchestration Infrastructure as Code (IaC)
Language YAML (Playbooks) HCL (Configuration files)
Agent Agentless (connects via SSH) Agent-based (Terraform agent)
Resource Management Manages existing resources Creates and manages resources
Change Handling Idempotent; handles changes in config Versioning and drift detection
Providers Limited to supported modules Extensive list of providers
Cloud Support Good cloud support Excellent cloud support
Use Cases Application Deployment, Configuration Management, Orchestration Infrastructure Provisioning, Resource Management
What is the Difference Between Snapshot & Image in AWS EC2?
Differentiating between ‘Snapshot’ and ‘Image’: their purposes for backups and launching instances on AWS EC2.

Using Terraform and Ansible Together

While Terraform and Ansible have distinct strengths, they can work together seamlessly to provide a complete infrastructure automation solution.

Terraform and Ansible Working Flowchart
Here are two common approaches:

First Approach: Start with Terraform for Provisioning, then Use Ansible for Configuration Management

In this approach, you begin by defining your infrastructure resources using Terraform. Once the provisioning is complete, you can invoke Ansible from within your Terraform configuration to handle configuration management tasks on the newly created resources.

# Terraform configuration
resource "aws_instance" "web_server" {
  # ... instance details ...

  provisioner "local-exec" {
    command = "ansible-playbook -i '${aws_instance.web_server.public_ip},' playbook.yml"

Second Approach: Start with Ansible and Call Terraform for Initial Provisioning

Alternatively, you can start with an Ansible playbook and call Terraform from within it to handle the initial infrastructure provisioning. Once the resources are ready, Ansible can take over and manage their configuration.

# Ansible playbook
- hosts: localhost
    - name: Provision infrastructure with Terraform
        project_path: '/path/to/terraform/project'
        state: present

    - name: Configure provisioned resources
      # ... configuration management tasks ...
What Makes Load Balancer vs. API Gateway Different and Use Cases ?
Discover the key distinctions between Load Balancer and API Gateway, along with their unique use cases like efficient traffic distribution & integration.

Example: Setting up a Jenkins Environment Using Terraform and Ansible

Now, let's put our knowledge into action by setting up a Jenkins environment using Terraform and Ansible.

1. Terraform for Provisioning an EC2 Instance on AWS

provider "aws" {
  region = var.region

resource "aws_vpc" "main" {
  # ... VPC configuration ...

resource "aws_instance" "jenkins" {
  ami           =
  instance_type = var.instance_type
  # ... other instance details ...

variable "region" {
  default = "us-east-1"

variable "instance_type" {
  default = "t2.micro"

How to Create & Deploy EC2 Instance Using Terraform?

2. Ansible for Deploying Docker and Running Jenkins Container

Once the EC2 instance is provisioned, Ansible takes over to deploy Docker and run the Jenkins container.

Here's what the Ansible playbook looks like:

- hosts: jenkins_hosts
  become: yes
    - name: Install prerequisites
          - apt-transport-https
          - ca-certificates
          - curl
          - software-properties-common
        state: latest

    - name: Add Docker GPG key
        state: present

    # ... additional tasks to install Docker ...

    - name: Pull Jenkins Docker image
        name: jenkins/jenkins:lts
        source: pull

    - name: Run Jenkins container
        name: jenkins
        image: jenkins/jenkins:lts
          - "8080:8080"


We start by defining our infrastructure resources using Terraform. Then, we run terraform apply to provision the required resources. Once the infrastructure is provisioned, we invoke an Ansible playbook to configure and manage the resources as needed.

Terraform and Ansible are not competitors; they are complementary tools that, when used together, can unlock the full potential of infrastructure automation.

How to Create & Deploy EC2 Instance Using Terraform?
Learn how to create and deploy AWS EC2 instances using Terraform for efficient cloud infrastructure management.

Subscribe to firstfinger

Don’t miss out on the latest posts. Sign up now to get access to the library of members-only posts.
[email protected]