Ansible Pilot

Current ansible-playbook Path - playbook_dir Magic Variable - Ansible Tip and Tricks

Displaying Ansible Playbook Path with Magic Variables for Debugging

Learn to display the current execution Ansible Playbook file system path using the "playbook_dir" Magic Variable for debugging and simplifying your automation journey.
July 22, 2022
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

YouTube Video

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 demo and some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.

Ansible Magic Variables

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.

demo

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 demo
  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 demo] ********************************************************************
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 demo] ********************************************************************
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

Recap

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

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

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Follow me

Subscribe not to miss any new releases