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.

Links

  • [The full list of Ansible Magic Variable](https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html)

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

``yaml

---

  • name: playbook directory Playbook

hosts: all

gather_facts: false

tasks:

- name: print current playbook directory

ansible.builtin.debug:

var: playbook_dir

`

execution

`bash

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

`bash

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

`bash

$ pwd

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

`

after execution

`bash

$ pwd

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

``

[code with ❤️ in GitHub](https://github.com/lucab85/ansible-pilot/)

Conclusion

Now you know how to use the "playbook_dir" Ansible Magic Variable in Ansible Playbook.