Integrating Terraform and Ansible: A Powerful Combination for Cloud Infrastructure Management

Sahaya Godson
4 min readSep 16, 2023

--

Ansible and Terraform

In today's rapidly evolving technology landscape, managing cloud infrastructure has become increasingly complex. To address this challenge, developers and system administrators have turned to Infrastructure as Code (IaC) tools to automate the provisioning and configuration of their infrastructure. Two popular tools in this space are Terraform and Ansible. In this comprehensive guide, we will explore how these tools can be integrated to create a streamlined and efficient workflow for managing cloud resources.

Understanding Terraform and Ansible

Terraform and Ansible are both powerful tools that serve distinct but complementary purposes in the realm of infrastructure management. Terraform, developed by HashiCorp, is an IaC tool that enables teams to provision and manage cloud resources. With Terraform, you define the desired state of your infrastructure in code, and Terraform takes care of provisioning and managing the resources to match that state. On the other hand, Ansible, developed by Red Hat, is a configuration management tool that automates the setup and configuration of systems and applications. Ansible allows you to define tasks and playbooks that describe the desired state of your infrastructure, and Ansible ensures that the configuration is applied consistently.

Getting Started: Integrating Terraform with Ansible

Now that we understand the benefits of integrating Terraform with Ansible, let's dive into the practical steps to get started with this integration.

Step 1: Infrastructure Provisioning with Terraform

The first step is to define your infrastructure using Terraform code. This code describes the resources you want to create, such as virtual machines, networks, security groups, etc. Here's an example of Terraform code to provision an Azure virtual machine:

provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "vm" {
name = "vm-rg"
location = "eastus"
}
resource "azurerm_virtual_network" "vm" {
name = "vm-vnet"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.vm.location
resource_group_name = azurerm_resource_group.vm.name
}
resource "azurerm_subnet" "vm" {
name = "vm-subnet"
resource_group_name = azurerm_resource_group.vm.name
virtual_network_name = azurerm_virtual_network.vm.name
address_prefixes = ["10.0.1.0/24"]
}
resource "azurerm_network_security_group" "vm" {
name = "vm-nsg"
location = azurerm_resource_group.vm.location
resource_group_name = azurerm_resource_group.vm.name
}
resource "azurerm_network_interface" "vm" {
name = "vm-nic"
location = azurerm_resource_group.vm.location
resource_group_name = azurerm_resource_group.vm.name
ip_configuration {
name = "vm-ipconfig"
subnet_id = azurerm_subnet.vm.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = null
}
}
resource "azurerm_virtual_machine" "vm" {
name = "myvm"
location = azurerm_resource_group.vm.location
resource_group_name = azurerm_resource_group.vm.name
network_interface_ids = [azurerm_network_interface.vm.id]
vm_size = "Standard_DS1_v2"

storage_os_disk {
name = "myosdisk"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}

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

os_profile {
computer_name = "myvm"
admin_username = "superuser"
admin_password = "passcode344@1"
}

os_profile_linux_config {
disable_password_authentication = false
}

tags = {
environment = "ansible"
}
}

In this example, we are using the Azure provider for Terraform, but you can use other providers depending on your cloud platform of choice.

Step 2: Configuration Management with Ansible

Once the infrastructure is provisioned, we can use Ansible to configure the resources. Ansible playbooks are written in YAML format and describe the tasks to be executed on the target hosts. Here's an example of an Ansible playbook to configure an Nginx web server and new relic on the provisioned virtual machine:

- hosts: myvm
become: yes
tasks:
- name: Install Nginx
apt:
name: nginx
state: present

- name: Configure Nginx
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
owner: root
group: root
mode: 0644

- name: Start Nginx
service:
name: nginx
state: started

- name: Download New Relic Repository Key
get_url:
url: https://download.newrelic.com/548C16BF.gpg
dest: /etc/apt/trusted.gpg.d/newrelic-archive-key.asc

- name: Add New Relic Repository
apt_repository:
repo: 'deb http://download.newrelic.com/debian/ newrelic non-free'
state: present

- name: Install New Relic Infrastructure Agent
apt:
name: newrelic-infra
state: present

- name: Configure New Relic License Key
ini_file:
dest: /etc/newrelic-infra.yml
section: license_key
option: license_key
value: YOUR_LICENSE_KEY

This playbook installs Nginx and new relic , copies a custom configuration file, and starts the Nginx service. Customize this playbook to fit your specific requirements.

Step 3: Integrating Terraform with Ansible

To integrate Terraform with Ansible, we can use the Ansible provisioner in Terraform. The Ansible provisioner allows us to run Ansible playbooks on the provisioned resources. Here's an example of how to incorporate the Ansible provisioner into Terraform:

resource "null_resource" "ansible_provisioner" {
provisioner "local-exec" {
command = "ansible-playbook -i ${self.private_ip}, playbook.yml"
working_dir = "${path.module}/ansible"
}
}

In this example, we use the null_resource in Terraform as a trigger for the Ansible provisioner. The local-exec provisioner runs the ansible-playbook command to execute the playbook against the provisioned infrastructure. Make sure to replace playbook.yml with the name of your Ansible playbook.

Step 4: Running Terraform with Ansible Provisioning

With the integration in place, you can now run Terraform to provision the infrastructure and execute the Ansible provisioner to configure the resources. Here's how you can do it:

  1. Run terraform init to initialize the Terraform working directory and download the required providers.
  2. Run terraform apply to create the infrastructure using Terraform and execute the Ansible provisioner to configure the newly created resources.

Step 5: Verifying the Installation

After the Terraform and Ansible provisioning process is complete, it's essential to verify that the desired configuration is applied to the provisioned resources. You can log in to the provisioned virtual machine and check the installed software, configurations, and services to ensure that everything is set up correctly.

Conclusion

Integrating Terraform with Ansible offers a powerful combination for managing cloud infrastructure. By leveraging the strengths of both tools, you can automate the provisioning and configuration of your infrastructure, ensuring consistency, scalability, and repeatability. This guide has provided an overview of the integration process and practical steps to get started. As you gain more experience with Terraform and Ansible, you can further customize and enhance your infrastructure management workflows. Embrace the power of Infrastructure as Code and take your cloud infrastructure management to the next level with Terraform and Ansible.

Remember, this guide is just the beginning of your journey. Explore the documentation, community resources, and real-world use cases to unlock the full potential of Terraform and Ansible. Happy automating!

--

--

Sahaya Godson
Sahaya Godson

Written by Sahaya Godson

Azure & AWS & GCP ☁️ | DevOps Engineer | Cloud Security | Kubernetes | Python | CC | CISSP

Responses (1)