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.

Join 50+ hours of courses in our exclusive community

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. Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my Udemy 300+ Lessons Video Course.

BUY the Complete Udemy 300+ Lessons Video Course

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Patreon Buy me a Pizza