no-relative-paths¶
This rule checks for relative paths in the ansible.builtin.copy and
ansible.builtin.template modules.
Relative paths in a task most often direct Ansible to remote files and
directories on managed nodes. In the ansible.builtin.copy and
ansible.builtin.template modules, the src argument refers to local files and
directories on the control node.
The recommended locations to store files are as follows:
- Use the
files/folder in the playbook or role directory for thecopymodule. - Use the
templates/folder in the playbook or role directory for thetemplatemodule.
These folders allow you to omit the path or use a sub-folder when specifying
files with the src argument.
Note
If resources are outside your Ansible playbook or role directory you should use an absolute path with the src argument.
Warning
Do not store resources at the same directory level as your Ansible playbook or tasks files. Doing this can result in disorganized projects and cause user confusion when distinguishing between resources of the same type, such as YAML.
See task paths in the Ansible documentation for more information.
Problematic Code¶
---
- name: Example playbook
hosts: all
tasks:
- name: Template a file to /etc/file.conf
ansible.builtin.template:
src: ../my_templates/foo.j2 # <- Uses a relative path in the src argument.
dest: /etc/file.conf
owner: bin
group: wheel
mode: "0644"
- name: Example playbook
hosts: all
vars:
source_path: ../../my_templates/foo.j2 # <- Sets a variable to a relative path.
tasks:
- name: Copy a file to /etc/file.conf
ansible.builtin.copy:
src: "{{ source_path }}" # <- Uses the variable in the src argument.
dest: /etc/foo.conf
owner: foo
group: foo
mode: "0644"