Current ansible-playbook Path - playbook_dir Magic Variable - Ansible Tip and Tricks
How to display the current execution Ansible Playbook file system path using the "playbook_dir" Magic Variables for debugging and simplifying your automation journey.


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 demo 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 Best Resources For Ansible
Certifications
Video Course
Printed Book
eBooks
Ansible by Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Ansible Cookbook: A Comprehensive Guide to Unleashing the Power of Ansible via Best Practices, Troubleshooting, and Linting Rules with Luca Berton
Ansible For Windows By Examples: 50+ Automation Examples For Windows System Administrator And DevOps
Ansible For Linux by Examples: 100+ Automation Examples For Linux System Administrator and DevOps
Ansible Linux Filesystem By Examples: 40+ Automation Examples on Linux File and Directory Operation for Modern IT Infrastructure
Ansible For Security by Examples: 100+ Automation Examples to Automate Security and Verify Compliance for IT Modern Infrastructure
Ansible Tips and Tricks: 10+ Ansible Examples to Save Time and Automate More Tasks
Ansible Linux Users & Groups By Examples: 20+ Automation Examples on Linux Users and Groups Operation for Modern IT Infrastructure
Ansible For PostgreSQL by Examples: 10+ Examples To Automate Your PostgreSQL database
Ansible For Amazon Web Services AWS By Examples: 10+ Examples To Automate Your AWS Modern Infrastructure
Ansible Automation Platform By Example: A step-by-step guide for the most common user scenarios
demo
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
---
- name: playbook directory demo
hosts: all
gather_facts: false
tasks:
- name: print current playbook directory
ansible.builtin.debug:
var: playbook_dir
execution
ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory file_management/file_playbookdir.yml
PLAY [playbook directory demo] ********************************************************************
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
ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory file_management/file_playbookdir.yml
PLAY [playbook directory demo] ********************************************************************
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
$ pwd
/Users/lberton/prj/github/ansible-pilot
after execution
$ pwd
/Users/lberton/prj/github/ansible-pilot
Recap
Now you know how to use the “playbook_dir” Ansible Magic Variable in Ansible Playbook. Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.
Academy
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate