Ansible playbook_dir: Get Current Playbook Path (Magic Variable Guide)
By Luca Berton · Published 2024-01-01 · Category: troubleshooting
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.
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
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
execution
idempotency
before execution
after execution
Conclusion
Now you know how to use the "playbook_dir" Ansible Magic Variable in Ansible Playbook.
All Ansible Magic Variables for Paths
| Variable | Description | Example Value | |----------|-------------|---------------| | playbook_dir | Directory of the playbook being executed | /home/user/ansible | | role_path | Current role's directory (inside roles only) | /home/user/ansible/roles/webserver | | inventory_dir | Directory of the inventory file | /home/user/ansible/inventory | | inventory_file | Full path to the inventory file | /home/user/ansible/inventory/hosts | | ansible_config_file | Path to the active ansible.cfg | /home/user/ansible/ansible.cfg |
Practical Examples
Reference files relative to playbook
Include vars file relative to playbook
Use in templates
Dynamic file discovery
Other Useful Magic Variables
| Variable | Description | |----------|-------------| | inventory_hostname | Current host from inventory | | inventory_hostname_short | Short hostname | | group_names | Groups current host belongs to | | groups | All groups and their members | | ansible_play_hosts | All hosts in current play | | ansible_play_batch | Current batch of hosts (with serial) | | ansible_check_mode | True if running with --check | | ansible_version | Ansible version info dict | | ansible_run_tags | Tags specified with --tags | | ansible_skip_tags | Tags specified with --skip-tags |
FAQ
What's the difference between playbook_dir and role_path? • playbook_dir: Always the directory where the main playbook lives • role_path: The current role's directory (only available inside a role)
If your playbook is at /opt/ansible/site.yml and it includes roles/webserver, then inside that role: playbook_dir = /opt/ansible, role_path = /opt/ansible/roles/webserver.
Can I use playbook_dir in ansible.cfg?
No — ansible.cfg is parsed before playbook execution, so magic variables aren't available there.
How do I get the playbook filename (not just directory)?
There's no built-in magic variable for the playbook filename. You can pass it as an extra var:
Related Articles • Ansible Inventory Guide
Category: troubleshooting
Watch the video: Ansible playbook_dir: Get Current Playbook Path (Magic Variable Guide) — Video Tutorial