Ansible - Get playbook directory using the playbook_dir variable
by
Jeremy Canfield |
Updated: December 18 2023
| Ansible articles
Let's say you've a playbook named foo.yml that exists in the /usr/local directory, like this.
/usr/local/foo.yml
The playbook_dir variable will contain the directory that contains the playbook. Here is how you would print the playbook_dir variable.
---
- hosts: localhost
tasks:
- debug:
var: playbook_dir
...
Which should return the following.
ok: [localhost] => {
"playbook_dir": "/usr/local"
}
Let's say you have the following structure.
├── /usr/local
│ ├── main.yml
│ ├── roles
│ │ ├── foo
│ │ ├── ├── tasks
│ │ ├── ├── ├── main.yml
│ │ ├── ├── files
│ │ ├── ├── ├── example.txt
Here is an example of how playbook_dir could be used.
---
- hosts: all
tasks:
- copy:
path: "{{ playbook_dir }}/roles/foo/files/example.txt"
dest: /usr/local/example.txt
...
Did you find this article helpful?
If so, consider buying me a coffee over at