Ansible Pilot

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

How to display the current execution Ansible Playbook file system path using the "playbook_dir" Magic Variables for debugging and simplifying your automation journey.

July 22, 2022
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

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.

The Best Resources For Ansible

Video Course

Books

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, Website and 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 200+ Ansible code

Want to keep this project going? Please donate

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

Subscribe not to miss any new releases

FREE Top 10 Best Practices

Top 10 Best Practices of Ansible Automation: save time, reduce errors and stress