Automate Infrastructure with Ansible and Terraform on Linux
Manual server and application management can be tedious and error-prone. This article solves this problem by presenting how to use Ansible and Terraform to automate infrastructure provisioning and management in Linux environments.
Introduction to Ansible and Terraform
Ansible and Terraform are powerful tools for infrastructure automation. While Ansible is a configuration automation tool, Terraform is an infrastructure as code (IaC) provisioning tool. Together, they enable the creation of consistent and repeatable environments.
Setting Up the Environment
Before getting started, make sure you have Ansible and Terraform installed on your machine. To install Ansible, use:
sudo apt update
sudo apt install ansible
To install Terraform, download the appropriate binary for your operating system and place it in your PATH.
Using Ansible for Server Configuration
Basic Playbook
An Ansible playbook is a YAML file that defines configuration tasks. Here is a basic example that installs Apache:
# playbook.yml
---
- name: Instalar e configurar Apache
hosts: webservers
become: yes
tasks:
- name: Atualizar repositórios
apt:
update_cache: yes
- name: Instalar Apache
apt:
name: apache2
state: present
- name: Iniciar e habilitar Apache
service:
name: apache2
state: started
enabled: yes
Advanced Playbook
This more advanced playbook installs Apache and configures a virtual host:
# advanced-playbook.yml
---
- name: Configurar site virtual Apache
hosts: webservers
become: yes
vars:
site_name: meusite.com
site_root: /var/www/meusite
tasks:
- name: Instalar Apache
apt:
name: apache2
state: present
- name: Criar diretório do site
file:
path: "{{ site_root }}"
state: directory
- name: Copiar arquivo de configuração do site
template:
src: site.conf.j2
dest: /etc/apache2/sites-available/{{ site_name }}.conf
- name: Habilitar site
command: a2ensite {{ site_name }}.conf
- name: Reiniciar Apache
service:
name: apache2
state: restarted
Using Terraform for Infrastructure Provisioning
Provisioning an EC2 Instance on AWS
This example uses Terraform to provision an EC2 instance on AWS:
# main.tf
provider "aws" {
region = "us-west-2"
}
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "example-instance"
}
}
Provisioning Complex Infrastructure
This example provisions more complex infrastructure with VPC, subnets and security groups:
# main.tf
provider "aws" {
region = "us-west-2"
}
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}
resource "aws_subnet" "public" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
}
resource "aws_security_group" "web" {
vpc_id = aws_vpc.main.id
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
subnet_id = aws_subnet.public.id
vpc_security_group_ids = [aws_security_group.web.id]
associate_public_ip_address = true
}
Why This Matters / When to Use
Infrastructure automation with Ansible and Terraform is crucial for:
- Consistency: Ensures that all environments are configured identically.
- Efficiency: Reduces the time required to provision and configure servers.
- Scalability: Makes provisioning multiple instances quickly and securely easy.
- Maintenance: Centralizes configuration in one place, making updates and fixes easier.
Connection to LPI/CompTIA Certifications
Mastering Ansible and Terraform is essential for those seeking certifications such as LPI Linux Essentials and CompTIA Linux+. These skills are highly valued in the job market and demonstrate your ability to manage complex infrastructures efficiently and securely.
That was just one piece. The course teaches the whole path — from your first command to exam day.