When Ansible parses a playbook, there are certain assumptions that can be made about the relative paths of items referenced by the statements in a playbook. In most cases, paths for things such as variable files to include, task files to include, playbook files to include, files to copy, templates to render, and scripts to execute, are all relative to the directory where the file referencing them resides. Let's explore this with an example playbook and directory listing to show where the files are:
- The directory structure is as follows:
. ├── a_vars_file.yaml ├── mastery-hosts ├── relative.yaml └── tasks ├── a.yaml └── b.yaml
- The content of a_vars_file.yaml is as follows:
--- something: "better than nothing"
- The content of relative.yaml is as follows:
--- - name: relative path play hosts: localhost gather_facts: false
vars_files: - a_vars_file.yaml
tasks: - name: who am I debug: msg: "I am mastery task" - name: var from file debug: var: something - include: tasks/a.yaml
- The content of tasks/a.yaml is as follows:
--- - name: where am I debug: msg: "I am task a" - include: b.yaml
- The content of tasks/b.yaml is as follows:
---
- name: who am I
debug:
msg: "I am task b"
Execution of the playbook is shown as follows:
We can clearly see the relative references to paths and how they are relative to the file referencing them. When using roles, there are some additional relative path assumptions; however, we'll cover that in detail in a later chapter.