AnsiblePilot — Master Ansible Automation
AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.
Popular Topics
About Luca Berton
Luca Berton is an Ansible automation expert, author of "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.
Ansible playbook_dir: Get Current Playbook Path (Magic Variable Guide) — Video Tutorial
How to use Ansible's playbook_dir magic variable to get the current playbook directory path. Includes role_path, inventory_dir, and all magic variables with examples.
What You'll Learn
- How to display the Current ansible-playbook Path?
- Ansible Magic Variables
- Links
- Playbook
- code
- execution
- idempotency
- before execution
- after execution
- Conclusion
Full Tutorial Content
How to display the Current ansible-playbook Path?
I'm going to show you how to Use the "playbook_dir" Ansible Magic Variable in Ansible Playbook with a live Playbook and some simple Ansible code.
I'm Luca Berton and welcome to today's episode of Ansible Pilot.
Ansible Magic Variables
- playbook_dir
The path to the directory of the playbook that was passed to the ansible-playbook command line
How to Ansible Magic Variables in Ansible Playbook.
The good news is that Ansible provides some internal variables that come out of the box with some information such as running the Ansible version, inventory details, or execution options.
Links
- [The full list of Ansible Magic Variable](https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html)
Playbook
Use "playbook_dir" Ansible Magic Variable in Ansible Playbook
How to display the Current ansible-playbook Path? I'm going to show you how to user the "playbook_dir" Ansible Magic Variable in Ansible Playbook.
Let's see in action some of the most common Ansible Magic Variables in an Ansible Playbook.
code
```yaml
---
- name: playbook directory Playbook
hosts: all
gather_facts: false
tasks:
- name: print current playbook directory
ansible.builtin.debug:
var: playbook_dir
```
execution
```bash
ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory file_management/file_playbookdir.yml
PLAY [playbook directory Playbook] ********************************************************************
TASK [print current playbook directory] ***********************************************************
ok: [demo.example.com] => {
"playbook_dir": "/Users/lberton/prj/github/ansible-pilot/file_management"
}
PLAY RECAP ****************************************************************************************
demo.example.com : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
```
idempotency
```bash
ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory file_management/file_playbookdir.yml
PLAY [playbook directory Playbook] ********************************************************************
TASK [print current playbook directory] ***********************************************************
ok: [demo.example.com] => {
"playbook_dir": "/Users/lberton/prj/github/ansible-pilot/file_management"
}
PLAY RECAP ****************************************************************************************
demo.example.com : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible-pilot $
```
before execution
```bash
$ pwd
/Users/lberton/prj/github/ansible-pilot
```
after execution
```bash
$ pwd
/Users/lberton/prj/github/ansible-pilot
```
[code with ❤️ in GitHub](https://github.com/lucab85/ansible-pilot/)
Conclusion
Now you know how to use the "playbook_dir" Ansible Magic Variable in Ansible Playbook.
All Ansible Magic Variables for
About This Tutorial
- Author: Luca Berton
- Difficulty: Beginner
- Read time: 4 min
- Category: troubleshooting
Read the full written article: Ansible playbook_dir: Get Current Playbook Path (Magic Variable Guide)
Related Video Tutorials
- Ansible Magic Variables: Complete Reference with Examples — Complete reference for Ansible magic variables. Use inventory_hostname, hostvars, groups, play_hosts, ansible_facts, and special variables in playbooks.
- ansible.cfg Configuration File: Complete Settings Guide (2026) — Complete guide to ansible.cfg configuration file. Every section explained — defaults, privilege_escalation, ssh_connection, inventory, galaxy. Plus ansible-config list, dump, view, and init commands with examples.
- Ansible Disable SSH Host Key Checking: Configuration Guide — How to disable SSH host key checking in Ansible. Configure ansible.cfg, environment variables, and per-host settings for lab and production environments.
- How to install Ansible in Oracle Linux 8 - Ansible install — Learn how to install the latest Ansible release on Oracle Linux 8 using the oracle-epel-release-el8 repository with a simple script.
- Ansible Set File Permissions 755: chmod with file Module Guide — How to set file permissions with Ansible file module. Add execute permission (755, 644, 600), manage ownership, and apply permissions recursively.
- How to Add a Disk to a VMware VM Using Ansible Playbook — Learn how to add a disk to a VMware VM using an Ansible playbook. This guide includes the YAML configuration, variables, and execution steps for easy automation.