IaC Terraform Ansible Vagrant Pulumi DevOps

Infrastructure as Code (IaC) Configuration Guide: Terraform, Ansible, and More

A comprehensive guide on IaC configuration templates, including terraform main.tf, ansible playbooks, and Pulumi config for cloud and local environments.

2026-04-11

Infrastructure as Code (IaC) Configuration Guide

Infrastructure as Code (IaC) has revolutionized how we manage and provision computing resources. Instead of manual configuration, we use code to define our environment, ensuring consistency, scalability, and reproducibility. This guide provides essential templates for the most popular IaC tools: Terraform, Ansible, Vagrant, and Pulumi.

1. Terraform Configuration Templates

Terraform is the industry standard for provisioning cloud infrastructure. It uses HCL (HashiCorp Configuration Language) to define resources.

Terraform main.tf Template

The main.tf file is where you define your primary resources. A typical terraform main.tf template includes resource blocks for instances, networks, and storage.

# terraform main.tf template example
resource "aws_instance" "web_server" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"

  tags = {
    Name = "Web-Server-01"
  }
}

resource "aws_vpc" "main_vpc" {
  cidr_block = "10.0.0.0/16"
}

Terraform Provider Config

The terraform provider config block tells Terraform which cloud provider (AWS, Azure, GCP, etc.) to use and how to authenticate.

# terraform provider config
provider "aws" {
  region = "us-east-1"
  # Authentication is usually handled via environment variables or AWS CLI profile
}

provider "google" {
  project = "my-project-id"
  region  = "us-central1"
}

Terraform Backend Config

The terraform backend config determines where Terraform stores its state file. Using a remote backend like S3 or GCS is critical for team collaboration.

# terraform backend config
terraform {
  backend "s3" {
    bucket         = "my-terraform-state-bucket"
    key            = "prod/terraform.tfstate"
    region         = "us-east-1"
    encrypt        = true
    dynamodb_table = "terraform-lock"
  }
}

2. Ansible Playbooks and Inventory

While Terraform provisions infrastructure, Ansible excels at configuration management and application deployment.

Ansible Playbook Template

An ansible playbook template defines the tasks to be executed on a set of hosts.

---
# ansible playbook template
- name: Configure Web Servers
  hosts: webservers
  become: yes
  tasks:
    - name: Ensure Apache is installed
      apt:
        name: apache2
        state: present

    - name: Start Apache service
      service:
        name: apache2
        state: started
        enabled: yes

    - name: Copy index.html
      copy:
        src: files/index.html
        dest: /var/www/html/index.html

Ansible Inventory File

The ansible inventory file lists the hosts and groups of hosts that Ansible manages.

# ansible inventory file (INI format)
[webservers]
192.168.1.10
192.168.1.11

[dbservers]
db.example.com ansible_user=admin

[all:vars]
ansible_python_interpreter=/usr/bin/python3

3. Vagrant and Pulumi Configuration

For local development and modern programmatic IaC, Vagrant and Pulumi are essential.

Vagrantfile Template and Example

A Vagrantfile template is used to define and configure virtual development environments.

# Vagrantfile example
Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/bionic64"
  config.vm.network "forwarded_port", guest: 80, host: 8080
  
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "2048"
    vb.cpus = 2
  end

  config.vm.provision "shell", inline: <<-SHELL
    apt-get update
    apt-get install -y nginx
  SHELL
end

Pulumi Config

Pulumi config allows you to manage infrastructure using familiar programming languages like TypeScript, Python, or Go.

// Pulumi config example (TypeScript)
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

const size = "t2.micro";
const ami = aws.ec2.getAmi({
    filters: [{ name: "name", values: ["amzn-ami-hvm-*"] }],
    owners: ["137112412989"], // This is the owner ID for Amazon Linux
    mostRecent: true,
}).then(ami => ami.id);

const server = new aws.ec2.Instance("web-server", {
    instanceType: size,
    ami: ami,
});

export const publicIp = server.publicIp;

4. Frequently Asked Questions (FAQ)

Q: Why do I get the "provider configuration not found" error in Terraform?

A: This usually happens if you haven't run terraform init after adding a new provider, or if the provider block is missing in your .tf files. Ensure you have the required_providers block if you're using specific versions.

Q: How do I fix "ansible unreachable" errors?

A: Check your ansible inventory file to ensure the IP addresses/hostnames are correct. Verify SSH connectivity manually and ensure your SSH key is added to the agent or specified in the inventory.

Q: What is the difference between Terraform and Pulumi?

A: Terraform uses HCL, a domain-specific language, while Pulumi uses standard programming languages. Pulumi offers more flexibility for complex logic, but Terraform has a larger ecosystem and community.

Q: When should I use Vagrant?

A: Use Vagrant when you need a consistent, reproducible local development environment that mimics your production setup (e.g., using VMs on your laptop).

Conclusion

Mastering IaC configuration templates is key to building modern, resilient infrastructure. Whether you are using Terraform for provisioning, Ansible for configuration, or Pulumi for code-driven cloud management, these templates provide a solid starting point for your DevOps journey.

Need help with formatting your JSON or YAML files? Visit Tool3M for a collection of powerful developer tools.