Skip to content
Tutorials Center
My learning

Topics

Lesson 94 of 95 ยท System Administration

Automation with Ansible and Terraform (overview)

Learn what infrastructure as code means, how Terraform creates servers and Ansible configures them, and why idempotence and plan-then-apply matter. With runnable models.

Setting up one server by hand is a nice way to learn. Setting up the tenth the same way, at 2 a.m., from memory, is how outages happen. Infrastructure as code (IaC) means you describe your servers, networks and settings in text files, keep those files in Git, and let a tool make reality match them. You get the same benefits as with application code: review, history, repeatability and the ability to roll back.

This lesson is an overview of the two most common tools. Terraform (and its open-source fork OpenTofu) creates infrastructure: virtual machines, networks, DNS records. Ansible configures machines over SSH: packages, files, users, services. Neither can run in the sandbox editors, since there is no network and no cloud account, so their real configuration files are read-only here. The ideas underneath them, idempotence and plan, then apply, are plain logic, so we rebuild both in Bash and run them. If you can read shell scripts, you will find these tools easier than they look.

Why not just a shell script?

A shell script is imperative: a list of steps. It works the first time, on a clean machine. Run it a second time and it may append a duplicate line, fail because a directory exists, or restart a service for no reason. Run it on a machine in an unknown state and you do not know what will happen.

IaC tools are declarative and idempotent. You state the result you want ("package nginx is installed, this file has this content, the service is running"), and the tool checks the current state and does only what is missing. Running it twice does the same as running it once, and the second run reports "nothing changed".

Provisioning Configuration management
Question answered What infrastructure exists? What is installed and configured on it?
Typical tools Terraform, OpenTofu, Pulumi, CloudFormation Ansible, Puppet, Chef, Salt
Works with Cloud APIs: servers, networks, databases, DNS A running machine, usually over SSH
Remembers A state file of what it created Usually nothing: it inspects the machine each run

A common combination: Terraform creates the servers and passes them first-boot settings (see cloud-init), and Ansible applies the configuration that is too big or too changeable for first boot.

flowchart LR
  G["Git repository<br/>.tf and .yml files"] --> T["Terraform<br/>creates servers, networks"]
  T --> C["cloud-init<br/>first-boot setup"]
  C --> A["Ansible<br/>packages, files, services"]
  A --> S["Running, configured servers"]

One pipeline: code in Git, infrastructure created, machines bootstrapped, then configured.

Idempotence, up close

The word means "applying it again changes nothing". Compare two ways to make sure a setting is present in a config file. The first appends a line every time; the second checks first:

Bash Naive append versus an idempotent ensure_line

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The imperative version leaves three copies of the line after three runs. The declarative version prints changed the first time and ok afterward. Real tools print the same two words (Ansible: ok and changed) and sum them in a recap at the end. A task that always reports changed is a bug in your playbook or module choice, because it hides real changes.

Ansible in practice

Ansible is agentless: it logs in over SSH, copies small Python modules to the target, runs them, and removes them. The target needs Python and an account that can use sudo; you install nothing permanent. You describe machines in an inventory and the work in a playbook written in YAML. All of the following is read-only here.

INI
# inventory.ini
[web]
web-01 ansible_host=203.0.113.10
web-02 ansible_host=203.0.113.11

[web:vars]
ansible_user=deploy

Before writing a playbook, ad hoc commands answer quick questions about the whole group:

CONSOLE
$ ansible web -i inventory.ini -m ansible.builtin.ping          # can I reach and log in?
$ ansible web -i inventory.ini -a "uptime"                      # run one command everywhere
$ ansible web -i inventory.ini -b -m ansible.builtin.apt -a "name=nginx state=present"

A playbook is an ordered list of tasks. Each task calls a module (apt, template, service, user, copy, lineinfile and hundreds more) with arguments that describe the desired state:

YAML
# site.yml
- name: Configure web servers
  hosts: web
  become: true                    # use sudo on the target
  vars:
    http_port: 8080

  tasks:
    - name: Install nginx
      ansible.builtin.apt:
        name: nginx
        state: present
        update_cache: true

    - name: Deploy the site configuration
      ansible.builtin.template:
        src: site.conf.j2         # a Jinja2 template that can use {{ http_port }}
        dest: /etc/nginx/conf.d/site.conf
        mode: "0644"
      notify: Reload nginx        # only runs the handler if this task changed something

    - name: Make sure nginx runs now and at boot
      ansible.builtin.service:
        name: nginx
        state: started
        enabled: true

  handlers:
    - name: Reload nginx
      ansible.builtin.service:
        name: nginx
        state: reloaded

Three ideas are worth remembering. Handlers run once, at the end, and only when a task reported changed, which is how "restart the service only when its config really changed" is done. Variables and templates keep one playbook usable for many environments. Roles package tasks, templates and defaults into reusable folders, which is how teams share configuration.

Always preview before you apply, and read the recap:

CONSOLE
$ ansible-playbook -i inventory.ini site.yml --check --diff    # dry run: show what would change
$ ansible-playbook -i inventory.ini site.yml                   # apply
PLAY RECAP *****************************************************
web-01 : ok=4  changed=2  unreachable=0  failed=0  skipped=0
web-02 : ok=4  changed=2  unreachable=0  failed=0  skipped=0

$ ansible-playbook -i inventory.ini site.yml                   # apply again
web-01 : ok=4  changed=0  unreachable=0  failed=0  skipped=0   # idempotent: nothing to do

(The numbers are illustrative.) Secrets such as passwords and API keys never go into a playbook in plain text: use ansible-vault to encrypt them, or fetch them from a secrets manager.

Terraform in practice

Terraform files are written in HCL, a small declarative language. You describe resources; Terraform compares your description to its state (a record of what it already created) and to the real world through a provider plugin for each platform (AWS, Google Cloud, Azure, Hetzner, Cloudflare, GitHub and thousands of others). Again, read-only:

HCL
# main.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

variable "region" {
  type    = string
  default = "eu-central-1"
}

variable "ami_id" {
  type        = string
  description = "Machine image to boot"
}

provider "aws" {
  region = var.region
}

resource "aws_instance" "web" {
  ami           = var.ami_id
  instance_type = "t3.micro"
  user_data     = file("cloud-init.yaml")      # first-boot configuration

  tags = {
    Name = "web-01"
  }
}

output "web_ip" {
  value = aws_instance.web.public_ip
}

The workflow is the same for every project:

CONSOLE
$ terraform init       # download providers, set up the working directory
$ terraform fmt        # format the files
$ terraform validate   # check syntax and references
$ terraform plan       # compare code, state and reality; change nothing
$ terraform apply      # show the plan again and, after you type "yes", do it
$ terraform destroy    # remove everything this configuration created

plan is the heart of the tool. It prints every change with a symbol: + create, ~ change in place, - destroy, and -/+ replace (destroy, then create). It ends with a summary line such as Plan: 1 to add, 0 to change, 0 to destroy. Read the plan before every apply. A one-word edit can turn a harmless change into a replacement of your database.

Because Terraform trusts its state file, the file matters: terraform.tfstate can contain secrets in plain text, so never commit it. Teams store it in a remote backend with locking (an object-storage bucket or a managed service) so two people cannot apply at the same time. Also pin provider versions, so an update on the internet does not silently change your next plan.

The next example models the plan-and-apply loop with files as "resources". The desired state is a text list, the "real world" is a directory, and the program computes the difference.

Bash A tiny plan and apply, with drift

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Read the second half carefully: Terraform does not care how reality drifted. It compares and reports. That is the benefit, and also the rule it imposes: changes go through the code. A hand-edit on the server is not just undocumented, it is the next thing a tool will overwrite or complain about.

Practical advice

  • Start small. Automate the thing you do most often (a new user, a web server), not your whole company, and run it on a test machine first.
  • Review the dry run. ansible-playbook --check --diff and terraform plan exist so that people look at them. In a team, require a review of the plan.
  • Keep secrets out of Git. Use vaults or a secrets manager; never put keys in variables files that are committed.
  • Pin versions of providers, modules and Ansible collections, and keep a lock file in version control.
  • One source of truth. If a tool owns a resource, nobody edits it by hand. If a rare emergency fix is unavoidable, record it and bring the code back in line afterward.
  • Clean up. Test environments cost money while they exist; terraform destroy is the cheap way to end an experiment.
  • Know when a script is enough. A one-off migration or a small personal server may not need a framework. Reach for IaC when you repeat the work, share it, or need to explain it later. The shell skills from this course (see Designing Maintainable Scripts and Exit Codes, set -e and Error Handling) are the foundation these tools stand on, and you will still write small scripts inside tasks and user_data.

There are many alternatives with the same ideas: Pulumi writes infrastructure in a general-purpose language, Puppet, Chef and Salt use agents on each machine, and image builders such as Packer bake a finished machine image so servers start already configured. The vocabulary (desired state, idempotence, plan, drift) transfers directly.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does it mean that an operation is idempotent?

    An idempotent task checks the current state and only acts when something differs, so repeating it is safe and the second run reports no changes.
  2. 2. Which tool is mainly used to create servers, networks and DNS records through cloud APIs?

    Terraform talks to provider APIs and tracks what it created in a state file. Ansible is mostly used to configure machines that already exist.
  3. 3. In an Ansible playbook, when does a handler such as 'Reload nginx' run?

    Handlers are for follow-up actions. A task with notify triggers the handler only if the task changed something, and the handler then runs once.
  4. 4. Why should you never commit terraform.tfstate to a public Git repository?

    State records resource attributes, which can include passwords or keys. Remote backends add access control and locking.
  5. 5. What is the best habit before running terraform apply?

    The plan lists every create, change and destroy. A replacement or destroy you did not intend is much cheaper to catch in the plan than after the apply.

Summary

  • Infrastructure as code keeps servers and settings in version-controlled text files, so they can be reviewed, repeated and rolled back.
  • Good automation is declarative and idempotent: describe the end state, change only what differs, and report ok versus changed.
  • Terraform/OpenTofu provisions infrastructure through providers and a state file; the loop is init, plan, apply, destroy, and the plan must be read.
  • Ansible configures machines over SSH with inventories, playbooks, modules, templates and handlers; use --check --diff before applying.
  • Combine them: Terraform creates the machine, cloud-init bootstraps it, Ansible configures it.
  • Keep secrets and state out of public repositories, pin versions, change things only through the code, and delete test environments when you finish.

Next: Linux Command Reference