AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 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 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", 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)

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.

Ansible playbook_dir: Get Current Playbook Path (Magic Variable Guide)

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.

See also: Ansible Magic Variables: Complete Reference with Examples

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

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_dir

execution

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-pilot

after execution

$ pwd
/Users/lberton/prj/github/ansible-pilot

code with ❤️ in GitHub

Conclusion

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: true

Include 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: true

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:

ansible-playbook site.yml -e "playbook_name=site.yml"

Related Articles

Ansible inventory complete reference

Category: troubleshooting

Watch the video: Ansible playbook_dir: Get Current Playbook Path (Magic Variable Guide) — Video Tutorial

Browse all Ansible tutorials · AnsiblePilot Home