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.

Watch Video

Watch "Ansible playbook_dir: Get Current Playbook Path (Magic Variable Guide)" on YouTube

What You'll Learn

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

Read the full written article: Ansible playbook_dir: Get Current Playbook Path (Magic Variable Guide)

Topics Covered

Related Video Tutorials