Ansible Playbook Examples: 10 Real-World Playbooks for Beginners
By Luca Berton · Published 2024-01-01 · Category: installation
Learn Ansible with 10 practical playbook examples. From installing packages to deploying web servers, these tested playbooks teach you Ansible step by step.

Ansible Playbook Examples for Beginners
Looking for practical Ansible playbook examples? This guide provides 10 real-world, tested playbooks that you can use immediately. Each example includes a complete playbook, explanation, and expected output.
I'm Luca Berton, and I've been teaching Ansible automation for years. Let me walk you through the most useful playbooks for beginners.
See also: Ansible for Windows: Complete Guide to Windows Automation (2026)
What Is an Ansible Playbook?
An Ansible playbook is a YAML file that defines a set of tasks to execute on remote hosts. Playbooks are the core of Ansible automation — they describe the desired state of your infrastructure.
Basic Playbook Structure
---
- name: My First Playbook
hosts: all
become: true
tasks:
- name: Ensure nginx is installed
ansible.builtin.apt:
name: nginx
state: present
Key elements:
• name: A human-readable description
• hosts: Target machines (from inventory)
• become: Escalate privileges (sudo)
• tasks: List of actions to perform
Example 1: Install a Package
The simplest and most common playbook — installing software:
---
- name: Install packages
hosts: webservers
become: true
tasks:
- name: Install nginx
ansible.builtin.apt:
name: nginx
state: present
update_cache: true
What it does: Installs nginx on all hosts in the webservers group using apt.
See also: Mastering Ansible-Creator: Simplify Your Ansible Collection Development
Example 2: Create a User
---
- name: Create application user
hosts: all
become: true
tasks:
- name: Create user 'deploy'
ansible.builtin.user:
name: deploy
shell: /bin/bash
groups: sudo
append: true
create_home: true
Example 3: Copy Files to Remote Hosts
---
- name: Deploy configuration
hosts: webservers
become: true
tasks:
- name: Copy nginx config
ansible.builtin.copy:
src: ./files/nginx.conf
dest: /etc/nginx/nginx.conf
owner: root
group: root
mode: '0644'
notify: Restart nginx
handlers:
- name: Restart nginx
ansible.builtin.service:
name: nginx
state: restarted
Key concept: The notify directive triggers a handler only when the task makes a change.
See also: Ansible Debugger: Interactive Debug & Troubleshoot Playbooks
Example 4: Use Variables
---
- name: Deploy with variables
hosts: webservers
become: true
vars:
app_port: 8080
app_user: myapp
tasks:
- name: Create application user
ansible.builtin.user:
name: "{{ app_user }}"
- name: Deploy app config
ansible.builtin.template:
src: app.conf.j2
dest: /etc/myapp/config.conf
Example 5: Conditional Execution
---
- name: OS-specific tasks
hosts: all
become: true
tasks:
- name: Install on Debian/Ubuntu
ansible.builtin.apt:
name: httpd
state: present
when: ansible_os_family == "Debian"
- name: Install on RHEL/CentOS
ansible.builtin.dnf:
name: httpd
state: present
when: ansible_os_family == "RedHat"
Example 6: Loop Over Items
---
- name: Install multiple packages
hosts: all
become: true
tasks:
- name: Install packages
ansible.builtin.apt:
name: "{{ item }}"
state: present
loop:
- git
- curl
- htop
- vim
- unzip
Example 7: Use Templates (Jinja2)
---
- name: Deploy templated config
hosts: webservers
become: true
vars:
server_name: mysite.example.com
listen_port: 80
tasks:
- name: Deploy virtual host
ansible.builtin.template:
src: vhost.conf.j2
dest: /etc/nginx/sites-available/{{ server_name }}
Example 8: Manage Services
---
- name: Ensure services are running
hosts: all
become: true
tasks:
- name: Start and enable nginx
ansible.builtin.service:
name: nginx
state: started
enabled: true
- name: Start and enable firewalld
ansible.builtin.service:
name: firewalld
state: started
enabled: true
Example 9: Error Handling with Blocks
---
- name: Error handling example
hosts: all
become: true
tasks:
- name: Handle potential errors
block:
- name: Try to install package
ansible.builtin.apt:
name: my-custom-app
state: present
rescue:
- name: Log failure
ansible.builtin.debug:
msg: "Package installation failed, falling back"
- name: Install alternative
ansible.builtin.apt:
name: my-custom-app-alt
state: present
always:
- name: Always run cleanup
ansible.builtin.debug:
msg: "Task block completed"
Example 10: Full Web Server Deployment
---
- name: Complete web server setup
hosts: webservers
become: true
vars:
domain: example.com
tasks:
- name: Update apt cache
ansible.builtin.apt:
update_cache: true
cache_valid_time: 3600
- name: Install required packages
ansible.builtin.apt:
name:
- nginx
- certbot
- python3-certbot-nginx
state: present
- name: Create web root
ansible.builtin.file:
path: "/var/www/{{ domain }}"
state: directory
owner: www-data
group: www-data
mode: '0755'
- name: Deploy index page
ansible.builtin.copy:
content: "<h1>Welcome to {{ domain }}</h1>"
dest: "/var/www/{{ domain }}/index.html"
- name: Deploy nginx config
ansible.builtin.template:
src: nginx-site.conf.j2
dest: "/etc/nginx/sites-available/{{ domain }}"
notify: Reload nginx
- name: Enable site
ansible.builtin.file:
src: "/etc/nginx/sites-available/{{ domain }}"
dest: "/etc/nginx/sites-enabled/{{ domain }}"
state: link
notify: Reload nginx
handlers:
- name: Reload nginx
ansible.builtin.service:
name: nginx
state: reloaded
Ansible Playbook Syntax Cheat Sheet
| Element | Purpose | Example |
|---------|---------|---------|
| hosts | Target machines | hosts: webservers |
| become | Privilege escalation | become: true |
| vars | Define variables | vars: app_port: 8080 |
| tasks | List of actions | tasks: - name: ... |
| handlers | Triggered actions | handlers: - name: ... |
| when | Conditional | when: ansible_os == "Ubuntu" |
| loop | Iterate | loop: [a, b, c] |
| notify | Trigger handler | notify: Restart nginx |
| register | Save output | register: result |
| template | Jinja2 files | template: src: app.j2 |
How to Run an Ansible Playbook
# Basic execution
ansible-playbook playbook.yml
# With inventory file
ansible-playbook -i inventory.ini playbook.yml
# Dry run (check mode)
ansible-playbook playbook.yml --check --diff
# With extra variables
ansible-playbook playbook.yml -e "app_port=9090"
# Limit to specific hosts
ansible-playbook playbook.yml --limit webserver1
# Verbose output
ansible-playbook playbook.yml -vvv
FAQ
What is the correct syntax for an Ansible playbook?
Ansible playbooks use YAML syntax. They start with--- and contain a list of plays, each with hosts, optional become, and tasks sections.
How do I pass variables to a playbook?
Usevars in the playbook, vars_files for external files, -e flag on the command line, or inventory variables.
What is the difference between a playbook and a role?
A playbook is a standalone YAML file with tasks. A role is a reusable, organized collection of tasks, variables, files, and templates following a specific directory structure.Can I run a playbook in check mode first?
Yes, useansible-playbook playbook.yml --check --diff to see what changes would be made without actually applying them.
Conclusion
These 10 Ansible playbook examples cover the most common automation tasks. Start with the simple ones and gradually adopt more advanced patterns like templates, handlers, and error handling.
For more tutorials, check out all Ansible tutorials on AnsiblePilot.
Related Articles
• Jinja2 filters in Ansible templates • using handlers in Ansible playbooks • fact-based conditionals in Ansible • organizing hosts with Ansible inventory • switching users with Ansible becomeCategory: installation