← Back to Home
πŸ“–

Ansible Playbooks

Describe the full desired state of a server in readable YAML β€” repeatable, shareable, version-controlled.

⏱15 min readπŸ“šDevOps Fundamentals

A playbook is a YAML file that describes the desired state of your servers as an ordered list of tasks. Unlike ad-hoc commands, playbooks are reusable, readable, and version-controlled β€” this is how Ansible is used in real projects.


🎯 Learning Objectives

By the end of this lesson you will:


Anatomy of a Playbook

Here’s a complete playbook that installs and starts nginx on all web servers β€” webserver.yml:

yaml
---
- name: Configure web servers
hosts: web
become: true

tasks:
  - name: Install nginx
    apt:
      name: nginx
      state: present
      update_cache: true

  - name: Ensure nginx is running and enabled
    service:
      name: nginx
      state: started
      enabled: true

Reading it top to bottom:

⚠ YAML is whitespace-sensitive

Like Python, YAML uses indentation for structure β€” always spaces, never tabs. A single misaligned line will cause a parse error. Keep indentation consistent (2 spaces per level is the convention).


Run the Playbook

bash
ansible-playbook -i inventory.ini webserver.yml
bash β€” 80Γ—24
student@devops:~$ansible-playbook -i inventory.ini webserver.yml

Run it again and watch changed drop to 0 β€” everything is already in the desired state. That’s idempotency in action.


Variables and Handlers

Variables keep playbooks flexible:

yaml
- name: Configure web servers
hosts: web
become: true
vars:
  package_name: nginx

tasks:
  - name: Install the web server
    apt:
      name: "{{ package_name }}"
      state: present

Handlers run only when notified by a changed task β€” ideal for restarting a service only when its config actually changed:

yaml
  tasks:
  - name: Copy nginx config
    copy:
      src: nginx.conf
      dest: /etc/nginx/nginx.conf
    notify: Restart nginx

handlers:
  - name: Restart nginx
    service:
      name: nginx
      state: restarted

πŸ’‘ Why handlers matter

Without a handler you’d restart nginx on every run. With one, nginx restarts only when the config file actually changed β€” no needless service disruptions. That’s the idempotent mindset applied to side effects.


πŸ§ͺ Hands-on Lab

πŸ“

Write Your First Playbook

  1. Write site.yml targeting your web group (or localhost) that installs a package (e.g. git or htop).
  2. Add a second task that ensures a service is started.
  3. Run it with ansible-playbook and read the PLAY RECAP.
  4. Run it a second time and confirm changed=0 β€” proving idempotency.

🧠 Knowledge Check

Knowledge Check

In a playbook, when does a handler run?

Knowledge Check

On the second run of an unchanged playbook, what do you expect in the PLAY RECAP?


πŸ’Ό Interview Preparation

Interview Q&A

What makes a good, maintainable Ansible playbook?


πŸŽ‰ You’ve Completed the Core Curriculum

Congratulations β€” you’ve worked through Linux, Git & GitHub, Python, AWS, Azure, and Ansible. You can now operate Linux servers, version and collaborate on code, script automation in Python, deploy across the two largest clouds, and configure fleets of servers with Ansible. That’s a genuinely strong DevOps foundation.

From the homepage roadmap, natural next steps are Docker, Kubernetes, Terraform, and CI/CD β€” all ready to be built in this same format whenever you are.

πŸ’‘ Make it real

Tie it all together in one project: write app code, version it in GitHub, script the setup in Python, provision a server on AWS or Azure, and use an Ansible playbook to configure it. Doing the full loop once teaches more than reading each topic separately.