Ansible Playbook Cheat Sheet: Ultimate Guide (2025)

Ansible Playbook Cheat Sheet is an essential resource for system administrators, DevOps engineers, and developers who want to automate IT tasks efficiently. With Ansible Playbooks, you can manage configurations, deploy applications, and orchestrate complex workflows—all in a structured and repeatable manner.

In this guide, we’ll cover everything you need to know about Ansible Playbooks: from basic syntax to advanced tips. Whether you’re just starting or looking for a quick refresher, this cheat sheet will help you execute tasks faster and more reliably.

What is an Ansible Playbook?

An Ansible Playbook is a YAML-based file that defines tasks to be executed on remote systems. Playbooks describe automation jobs in a human-readable way, making them both easy to understand and powerful for managing infrastructure.

Instead of running one-off commands with ansible, playbooks allow you to define repeatable automation processes. They include details like hosts, variables, roles, and task definitions.

For DevOps teams, playbooks are the backbone of Infrastructure as Code (IaC). They ensure consistency, reduce human error, and speed up deployments across environments.

Basic Structure of an Ansible Playbook

Every Ansible Playbook follows a simple YAML format. Here’s the general structure:

---
- name: Playbook Name
  hosts: all
  become: yes
  tasks:
    - name: Install package
      apt:
        name: nginx
        state: present

Key Components

  1. Hosts: Defines which machines the playbook will run on.
  2. Become: Grants elevated privileges (sudo).
  3. Tasks: A list of operations to execute, like installing packages or starting services.

A clean structure ensures readability and maintainability. Always keep indentation consistent to avoid syntax errors.

Ansible Playbook Cheat Sheet: Common Commands

Here are some frequently used commands that every engineer should know:

Running a Playbook

ansible-playbook site.yml

Running a Playbook with Specific Inventory

ansible-playbook -i inventory.ini site.yml

Limit Execution to a Host

ansible-playbook site.yml --limit webserver1

Run with Tags

ansible-playbook site.yml --tags "setup"

Check Mode (Dry Run)

ansible-playbook site.yml --check

These commands allow flexibility and control when executing automation across environments.

Variables in Ansible Playbooks

Variables make playbooks dynamic and reusable. You can define them directly in the playbook, in inventory files, or as external variable files.

Example Variable Usage

vars:
  app_name: myapp
  app_port: 8080

tasks:
  - name: Install app
    apt:
      name: "{{ app_name }}"
      state: present

With variables, you avoid hardcoding values, making it easier to manage environments like development, staging, and production.

Handlers in Ansible Playbooks

Handlers are special tasks that run only when notified. They are typically used for restarting services after configuration changes.

Example of Handlers

tasks:
  - name: Update nginx config
    template:
      src: nginx.conf.j2
      dest: /etc/nginx/nginx.conf
    notify: Restart nginx

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

Handlers prevent unnecessary restarts and ensure services are updated only when changes occur.

Ansible Playbook Cheat Sheet: Loops

Loops help repeat tasks efficiently.

Example: Loop with Items

tasks:
  - name: Install multiple packages
    apt:
      name: "{{ item }}"
      state: present
    loop:
      - git
      - curl
      - vim

Loops save time when you need to perform the same action across multiple items, such as installing packages or creating users.

Roles in Ansible Playbooks

Roles are a way to organize playbooks into reusable components. A role typically contains tasks, handlers, variables, templates, and files.

Role Directory Structure

roles/
  └── webserver/
      ├── tasks/
      ├── handlers/
      ├── templates/
      ├── vars/
      └── defaults/

By using roles, large projects remain modular, easier to manage, and scalable.

Ansible Playbook Cheat Sheet: Conditionals

Conditionals allow you to run tasks only when certain conditions are met.

Example Conditional

tasks:
  - name: Install Apache only on Ubuntu
    apt:
      name: apache2
      state: present
    when: ansible_os_family == "Debian"

This ensures tasks run only when specific requirements are satisfied, making playbooks smarter and more adaptable.

Error Handling in Ansible Playbooks

Handling errors gracefully is critical for production environments.

Example: Ignore Errors

tasks:
  - name: Attempt to remove a package
    apt:
      name: non_existent_package
      state: absent
    ignore_errors: yes

Example: Fail Fast

tasks:
  - name: Ensure required variable is set
    fail:
      msg: "app_port must be defined"
    when: app_port is not defined

Error handling ensures playbooks remain robust and predictable.

Advanced Ansible Playbook Tips

Here are some best practices to make your playbooks more efficient:

  1. Use YAML Linting: Run yamllint to catch syntax errors early.
  2. Leverage Vault for Secrets: Store sensitive data securely with ansible-vault.
  3. Use Tags Wisely: Apply tags to group related tasks for selective execution.
  4. Version Control: Keep playbooks in Git for collaboration and rollback.
  5. Documentation: Add descriptive names and comments for better readability.

Following these practices keeps your automation clean, secure, and scalable.

Example Ansible Playbook: Complete Web Server Setup

Here’s a sample playbook that installs and configures a web server:

---
- name: Setup Web Server
  hosts: webservers
  become: yes

  vars:
    http_port: 80

  tasks:
    - name: Install Nginx
      apt:
        name: nginx
        state: present

    - name: Start Nginx
      service:
        name: nginx
        state: started
        enabled: yes

    - name: Deploy index.html
      copy:
        src: index.html
        dest: /var/www/html/index.html

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

This playbook ensures a fully functional web server is deployed in just a few lines of code.

FAQ: Ansible Playbook Cheat Sheet

1. What is the difference between Ansible Playbook and ad-hoc commands?

Ad-hoc commands are for one-time tasks, while playbooks are reusable automation scripts that define consistent workflows across environments.

2. How do I debug an Ansible Playbook?

Use the -vvv option with ansible-playbook to increase verbosity and trace issues effectively.

3. Can I use variables across multiple playbooks?

Yes, you can define variables in external files, inventories, or group_vars, making them accessible across multiple playbooks.

4. How do handlers improve Ansible Playbooks?

Handlers execute only when notified, which avoids unnecessary service restarts and ensures efficient configuration management.

5. Is Ansible Playbook suitable for production environments?

Absolutely. Ansible Playbooks are widely used in production for configuration management, continuous deployment, and orchestration due to their reliability and scalability.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top