Ansible Playbook Examples for Beginners

Updated 2026-06-11

The fastest way to learn Ansible is to read small, real playbooks and run them. Here are a few you'll reach for constantly, with notes on what each part does.

Restart a service across a group

- name: Recover the web tier
  hosts: web
  become: true
  tasks:
    - name: Restart nginx and enable on boot
      ansible.builtin.service:
        name: nginx
        state: restarted
        enabled: true

hosts: web targets a group from your inventory, so one run hits every web node. become: true escalates to root.

Install a package idempotently

- name: Ensure monitoring agent is present
  hosts: all
  become: true
  tasks:
    - name: Install the agent
      ansible.builtin.package:
        name: node-exporter
        state: present

state: present is idempotent — run it ten times and it only changes anything the first time. That's the whole point of Ansible: describe the desired end state, not the steps.

Now run them for real

Reading playbooks is step one; running them against servers that actually need fixing is where it clicks. Practise writing and executing real playbooks on a live fleet with hands-on Ansible practice.

Learn it by doing

Create a free account and practise on a live fleet.

← All guides