Bootstrap FreeKB - Ansible - Get playbook directory using the playbook_dir variable
Ansible - Get playbook directory using the playbook_dir variable

Updated:   |  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 Buy Me A Coffee



Comments


Add a Comment


Please enter 333fd3 in the box below so that we can be sure you are a human.