Ansible Tutorial · Playbooks · Lesson 6 of 20

Ansible Playbook Basics

Write and run a YAML playbook with tasks.

Members can save this page, take notes and track progress. See membership · Sign in

What is a playbook?

A playbook is a YAML file containing one or more plays. Each play targets a group of hosts and runs a list of tasks in order. Playbooks are the main way to use Ansible for anything beyond a quick ad-hoc command.

Anatomy of a play

# site.yml
---
- name: Configure web servers
  hosts: webservers
  become: true

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

    - name: Start nginx
      ansible.builtin.service:
        name: nginx
        state: started
        enabled: true

Required fields

  • hosts: which inventory group (or pattern) this play targets.
  • tasks: an ordered list of module calls.

Optional fields

  • name: a human-readable label (shown in output).
  • become: run with sudo.
  • vars: define variables for this play.
  • handlers: tasks triggered by notify.

Running a playbook

ansible-playbook -i inventory.yml site.yml

Idempotency

If you run the same playbook twice, tasks that already match the desired state report ok (green) instead of changed (yellow). This is idempotency — it is safe to re-run any playbook at any time.

Check mode (dry run)

ansible-playbook -i inventory.yml site.yml --check --diff

--check simulates the run without making changes. --diff shows what would change in files.

Verbosity

Add -v for more detail, up to -vvvv:

Flag Detail level
-v Task results
-vv Input parameters
-vvv Connection and command details
-vvvv Full SSH debugging

Multiple plays in one playbook

A playbook can contain several plays, each targeting different host groups:

---
- name: Configure web servers
  hosts: webservers
  become: true
  tasks:
    - name: Install nginx
      ansible.builtin.apt:
        name: nginx
        state: present

- name: Configure databases
  hosts: databases
  become: true
  tasks:
    - name: Install postgresql
      ansible.builtin.apt:
        name: postgresql
        state: present

Plays execute in order, top to bottom.

Run a minimal playbook
cat > /tmp/site.yml << 'EOF'
---
- name: Test playbook
  hosts: localhost
  connection: local
  gather_facts: false
  tasks:
    - name: Say hello
      ansible.builtin.debug:
        msg: "Hello from Ansible!"
EOF
ansible-playbook /tmp/site.yml

Output

PLAY [Test playbook] ***********************************************************

TASK [Say hello] **************************************************************
ok: [localhost] => {
    "msg": "Hello from Ansible!"
}

PLAY RECAP ********************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Run this on your computer — see “Get Started” for setup.

Dry run with check mode
ansible-playbook /tmp/site.yml --check

Output

PLAY [Test playbook] ***********************************************************

TASK [Say hello] **************************************************************
ok: [localhost] => {
    "msg": "Hello from Ansible!"
}

PLAY RECAP ********************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Run this on your computer — see “Get Started” for setup.

Test yourself

What does the --check flag do?

  1. Checks YAML syntax only
  2. Simulates the run without making changes
  3. Checks SSH connectivity
  4. Runs the playbook twice to verify
Show answer

Answer: Simulates the run without making changes

--check is a dry-run mode: Ansible reports what would change without actually modifying the managed nodes.

Last reviewed · By the ByteCamel team · How we write content

Was this page helpful?

Report a mistake