Infrastructure as codeIntermediate5h

Ansible basics.

Configuration management with idempotent playbooks.

What is Ansible?

Ansible configures existing machines — installing packages, editing files, starting services — by running playbooks over SSH. Where Terraform provisions infrastructure, Ansible configures what runs on it. It needs no agent on the target, just SSH access.

Why it matters

Plenty of systems are not containerized, and someone has to configure servers consistently. Ansible makes that repeatable and reviewable instead of a pile of ad-hoc SSH commands. It is a common companion to Terraform and a frequent requirement for operations roles.

What to learn

  • Inventory: the hosts you manage
  • Playbooks, plays, and tasks
  • Modules for common operations
  • Idempotency and why it is the core idea
  • Variables, templates, and facts
  • Roles for organizing reusable configuration
  • Running against groups of hosts

Common pitfall

Writing tasks that are not idempotent — appending a line every run, or always restarting a service. Ansible is meant to be safe to run repeatedly, converging to the desired state. Use the proper modules (which check before acting) instead of raw shell commands, so a second run changes nothing.

Resources

Primary (free):

Practice

Write a playbook that, on a target machine, installs a package, writes a config file from a template, and ensures a service is running. Run it twice and confirm the second run reports no changes. Done when re-running is a no-op because everything already matches.

Outcomes

  • Define an inventory and run a playbook over SSH.
  • Write tasks using modules instead of raw shell.
  • Ensure playbooks are idempotent.
  • Organize reusable configuration into roles.
Back to DevOps roadmap