In modern DevOps practices, automating infrastructure provisioning and configuration management is crucial. Terraform and Ansible are two powerful tools that together can streamline these processes. Terraform allows you to define and provision infrastructure resources declaratively. Ansible, on the other hand, is a configuration management tool that automates application deployment, configuration management, and other IT tasks.
In this guide, we’ll provision an AWS EC2 instance using Terraform and configure the EC2 instance with Ansible.
Table of Contents
Terraform and Ansible Integration Overview
The integration of Terraform and Ansible can be approached in two ways:
- Terraform to Provision Infrastructure, Ansible to Configure: Terraform handles the provisioning of infrastructure resources, and Ansible is used to configure those resources.
- Ansible to Call Terraform: Ansible playbooks can include tasks to run Terraform commands, allowing for a single control plane.
In this guide, we’ll focus on the first approach, which is more common and straightforward.
Prerequisites
Before we begin, ensure you have the following installed on your system:
- Terraform
- Ansible
- AWS CLI (for AWS examples)
- SSH keys (for connecting to AWS servers)
Provision an AWS EC2 instance using Terraform
1. Create a new directory for your Terraform configuration and navigate into it.
# mkdir terraform-ansible-demo && cd terraform-ansible-demo
2. Initialize Terraform to download the necessary provider plugins.
# terraform init
3. Create a file named main.tf and add the following configuration:
provider "aws" {
region = "us-west-2"
}
resource "aws_instance" "example" {
ami = "ami-080e1f13689e07408" # Replace with a valid AMI ID
instance_type = "t2.micro"
tags = {
Name = "Terraform-Ansible-Demo"
}
provisioner "local-exec" {
command = "echo ${self.public_ip} > ip_address.txt"
}
}
4. Run the following command to apply the configuration and provision the EC2 instance:
# terraform apply
Terraform will perform the following actions:
Configure the EC2 instance with Ansible
After provisioning the EC2 instance, we’ll use Ansible to configure it.
1. Create a file named inventory.ini and add the following content, replacing 54.123.45.67 with the IP address from ip_address.txt.
[webserver]
54.123.45.67 ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/id_rsa
2. Create a file named playbook.yml and add the following content:
---
- name: Configure EC2 instance
hosts: webserver
become: yes
tasks:
- name: Update and upgrade apt packages
apt:
update_cache: yes
upgrade: dist
- name: Install Nginx
apt:
name: nginx
state: present
- name: Start Nginx service
service:
name: nginx
state: started
3. Execute the Ansible playbook using the following command:
# ansible-playbook -i inventory.ini playbook.yml
Output.
Conclusion
In this guide, we explained how to provision an AWS EC2 instance with Terraform and configure it using Ansible. Using Terraform and Ansible together provides a powerful combination for automating infrastructure provisioning and configuration management. Terraform handles the creation of infrastructure resources, while Ansible takes care of the detailed configuration and application deployment. By integrating these tools, you can create a seamless, automated workflow that enhances efficiency and reduces the risk of human error.
FAQs
1. Can I use Ansible within a Terraform plan?
Yes, you can use the null_resource in Terraform with a local-exec or remote-exec provisioner to run Ansible playbooks during the Terraform deployment process.
2. What is the typical workflow for using Terraform and Ansible together?
Typically, Terraform is used first to provision infrastructure, followed by Ansible to handle application deployment and configuration.