Difference Between Ansible and Terraform

Surya BanothSurya Banoth
3 min read

Table of contents

Ansible and Terraform are both tools used for Infrastructure as Code (IaC), but they serve different purposes and are used in different stages of the infrastructure lifecycle.

Ansible is a configuration management tool that is used to automate the configuration and management of software and systems. It is primarily used for tasks such as installing software, configuring servers, and deploying applications.

Terraform, on the other hand, is a tool for provisioning infrastructure resources. It is used to automate the deployment of infrastructure resources such as servers, databases, and network components across different cloud providers.

In summary, Ansible is used to automate software and system configuration, while Terraform is used to automate the provisioning of infrastructure resources.

Example Ansible Script:

Here's an example of Ansible script that installs the Apache web server on a Debian-based Linux system:

This script includes a playbook that specifies the tasks to be executed on the host(s) specified by the hosts field. The become field is used to specify that the playbook should be executed with elevated privileges (usually as the root user).

The tasks section includes two tasks. The first task uses the apt module to install the apache2 package. The second task uses the service module to start the Apache service and enable it to start automatically at boot time.

To use this script, you would need to install Ansible, create an inventory file specifying the target hosts, and run the ansible-playbook command with the playbook file as an argument. For example, if the playbook file is named apache.yml and the inventory file is named hosts.ini, you could run the following command.

Example Terraform Script:

Example script of Terraform that creates a virtual machine in Microsoft Azure:

javaCopy codeprovider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "example-resource-group"
  location = "West US"
}

resource "azurerm_virtual_network" "example" {
  name                = "example-vnet"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
}

resource "azurerm_subnet" "example" {
  name                 = "example-subnet"
  address_prefix       = "10.0.1.0/24"
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name
}

resource "azurerm_network_interface" "example" {
  name                = "example-nic"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

  ip_configuration {
    name                          = "example-config"
    subnet_id                     = azurerm_subnet.example.id
    private_ip_address_allocation = "Dynamic"
  }
}

resource "azurerm_virtual_machine" "example" {
  name                  = "example-vm"
  location              = azurerm_resource_group.example.location
  resource_group_name   = azurerm_resource_group.example.name
  network_interface_ids = [azurerm_network_interface.example.id]
  vm_size               = "Standard_D2s_v3"

  storage_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "18.04-LTS"
    version   = "latest"
  }

  storage_os_disk {
    name              = "example-osdisk"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Standard_LRS"
  }

  os_profile {
    computer_name  = "example-vm"
    admin_username = "adminuser"
    admin_password = "AdminPassword123!"
  }

  os_profile_linux_config {
    disable_password_authentication = false
  }
}

This script includes a provider block that specifies the Azure provider, and several resource blocks that create a resource group, a virtual network, a subnet, a network interface, and a virtual machine. The resources are created in the West US region and use Ubuntu Server 18.04 LTS as the operating system.

To use this script, you would need to install Terraform, create an Azure account, and configure your Azure credentials. You can then run the terraform init, terraform plan, and terraform apply commands to initialize Terraform, preview the changes that will be made, and apply the changes respectively.

Thank you for visiting my blog.

0
Subscribe to my newsletter

Read articles from Surya Banoth directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Surya Banoth
Surya Banoth

Experienced Leader with around 15 years of experience & expertize in the areas of Release Management, DevOps, Digital Transformation, Software Testing and Pega & Java development. A champion in handling complex and critical releases of a top Telecom organization in MENA with huge subscriber base. My strength is handling multiple tasks competently and working well under enormous pressure. I’m certain that I can adapt to any new changes and also I would describe myself as a team player. • Strong work experience in Release management, DevOps, CI/CD, Monitoring, Build, Change, Configuration & Release Engineering Processes. • Extensive experience in managing the releases of 100+ Digital & Complex applications in a challenging environment. • Adept in setting up Continuous Integration & Continuous Deployment using Jenkins & Azure Pipelines. • Good exposure on both Azure & AWS cloud technologies. • Much needed Experience in other SDLC areas like software development and software testing and automation. My Technical Skills : Release Management | Change Management | DevOps | CICD | Azure | AWS | Jenkins | Docker | Kubernetes | Scrum | Agile | Java | Pega | Python | Linux | Shell scripting | HTML | Jira | RQM | RTC | Oracle | SQL | Software Testing | Test Management My certifications are: 1) ITIL Foundation, ITIL RCA, ITIL SOA Certified Professional (ITIL 3x) 2) Azure Administrator & Azure DevOps Expert Certified (Azure 2x) 3) PMP and CSM® certified professional in Project Management. 4) Java & Pega CSA, CSSA Certified. 5) ISTQB Certified Test professional.