DEV Community

santhoshnimmala
santhoshnimmala

Posted on

How to integrate Ansible with Terraform on AWS

Hey, my Self Santhosh Nimmala, I am Working with Luxoft as a Principal consultant (leading Cloud and DevOps in TRM space), in coming Articles I will be explaining about DevOps and DevTools with respective to AWS it will also have real world DevOps projects with Code and common DevOps Patterns , in this blog we are going to see how to integrate Ansible with terraform in AWS , we would be using terraform for infra provisioning but for configuring our applications and base packages we will be using config management languages like ansible , now we will see how can we integrate both so that we can have single pipeline , i have developed sample project and pushed to git repo please follow steps below .


As organizations move towards adopting Infrastructure as Code (IaC), they often find themselves using multiple tools for different stages of their development pipeline. Two popular tools in the IaC space are Ansible and Terraform. While they are both useful in their own ways, they can also be integrated to create a more streamlined and efficient development process.

In this article, we will explore the integration of Ansible with Terraform and discuss the benefits of using them together.


What is Ansible?
Ansible is an open-source automation platform that helps automate tasks related to configuration management, application deployment, and orchestration. It uses a simple YAML-based syntax to define tasks, making it easy to read and understand for both developers and operations teams.

Ansible is often used for managing infrastructure at scale, configuring servers, deploying applications, and managing network devices. It can also be used for provisioning resources on cloud platforms like AWS, Azure, and GCP.


What is Terraform?
Terraform is an open-source infrastructure as code tool that allows you to define and provision infrastructure resources in a declarative language. It provides a simple, consistent interface for managing resources across multiple cloud providers, on-premises data centers, and other infrastructure.

Terraform uses a configuration language called HCL (HashiCorp Configuration Language) to define infrastructure resources. With Terraform, you can define resources like virtual machines, networks, storage, and more, and manage their lifecycle through a series of commands.


Why integrate Ansible with Terraform?
While both Ansible and Terraform can be used to provision and manage infrastructure resources, they are designed to solve different problems. Ansible is focused on configuration management, while Terraform is focused on infrastructure provisioning.

By integrating Ansible with Terraform, you can take advantage of the strengths of both tools to create a more streamlined and efficient development process. Ansible can be used to configure the resources provisioned by Terraform, ensuring that they are set up exactly as you need them.

This integration can help to simplify the overall development process, reduce the amount of code you need to write, and make it easier to manage and update your infrastructure resources.


1) clone the code form below link

https://github.com/santhoshnimmala/ansible-terraform-integration

2) you will see below files with infra components .

Image description
here main.tf contains all the terraform code to deploy infra structure
3) this will deploy VPC, Subnets, route tables , security groups , instance and we will use remote exec to execute ansible playbook on the instance .

Image description

4) if you check remote provisioner code


resource "null_resource" "ansible" {
  depends_on = [aws_instance.nginx]

  connection {
    type        = "ssh"
    user        = "ubuntu"
    private_key = file("key1.pem")
    host        = aws_instance.nginx.public_ip
  }
  provisioner "file" {
    source      = "playbook.yml"
    destination = "/tmp/playbook.yml"
           }


  provisioner "remote-exec" {
    inline = [
      "sudo apt-get update",
      "sudo apt-get install -y ansible",
      "cd /tmp/",
      "sudo ansible-playbook playbook.yml"
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

this creates a null resources which will first do a ssh connection to the instance and copy out ansible playbook to /tmp folder and then it will execute series of commands to install ansible and execute ansible-playbook playbook.yml in ec2 instance .

this will install nginx server on the instance if you see playbook.yml you will see below code .

---
- name: Install Nginx
  hosts: localhost
  become: true
  tasks:
    - name: Install Nginx
      apt:
        name: nginx
        state: present
Enter fullscreen mode Exit fullscreen mode

please make sure placing your .pem file in the directory as it is needed for ssh purpose .

5) once everting is done run terraform init it should look like this .

Image description

6) then run plan and apply which should look like this .

Image description

this make sure that nginx successfully installed , please visit the public ip of instance and check


In conclusion, integrating Ansible with Terraform can help to simplify the overall development process, reduce the amount of code you need to write, and make it easier to manage and update your infrastructure resources. Ansible can be used to configure the resources provisioned by Terraform, ensuring that they are set up exactly as you need them. By leveraging the strengths of both tools, you can create a more streamlined and efficient development pipeline, enabling you to focus on delivering high-quality applications to your users.

Top comments (0)