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.

How to display the Current ansible-playbook Path?
See also: Ansible Magic Variables: Complete Reference with Examples
Ansible Magic Variables
- playbook_dir
Links
See also: 10 Proven Methods to Optimize Ansible Playbook Performance
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
---
- name: playbook directory Playbook
hosts: all
gather_facts: false
tasks:
- name: print current playbook directory
ansible.builtin.debug:
var: playbook_direxecution
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
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
$ pwd
/Users/lberton/prj/github/ansible-pilotafter execution
$ pwd
/Users/lberton/prj/github/ansible-pilotConclusion
Now you know how to use the "playbook_dir" Ansible Magic Variable in Ansible Playbook.
See also: ansible.cfg Configuration File: Complete Settings Guide (2026)
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
- name: Copy config from playbook directory
ansible.builtin.copy:
src: "{{ playbook_dir }}/files/app.conf"
dest: /etc/myapp/app.conf
become: trueInclude vars file relative to playbook
- name: Load environment-specific variables
ansible.builtin.include_vars:
file: "{{ playbook_dir }}/vars/{{ env }}.yml"Use in templates
# In a Jinja2 template
# deployment_info.conf.j2
deployed_from={{ playbook_dir }}
deployed_at={{ ansible_date_time.iso8601 }}
deployed_by={{ ansible_user_id }}Dynamic file discovery
- name: Find all config templates
ansible.builtin.find:
paths: "{{ playbook_dir }}/templates/configs/"
patterns: "*.j2"
register: config_templates
delegate_to: localhost
- name: Deploy all configs
ansible.builtin.template:
src: "{{ item.path }}"
dest: "/etc/myapp/{{ item.path | basename | regex_replace('.j2$', '') }}"
loop: "{{ config_templates.files }}"
become: trueOther 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 livesrole_path: The current role's directory (only available inside a role)
/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:
ansible-playbook site.yml -e "playbook_name=site.yml"Related Articles
Category: troubleshooting
Watch the video: Ansible playbook_dir: Get Current Playbook Path (Magic Variable Guide) — Video Tutorial