Difference Between Ansible and Terraform

Table of contents

No heading

No headings in the article.

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.