summaryrefslogtreecommitdiffstats
path: root/src/ansiblelint/rules/no_relative_paths.md
blob: 568a1455aaf6cb09c9bd95a8de10ac3286c0fe8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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 the `copy`
  module.
- Use the `templates/` folder in the playbook or role directory for the
  `template` module.

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](https://docs.ansible.com/ansible/latest/playbook_guide/playbook_pathing.html#task-paths)
in the Ansible documentation for more information.

## Problematic Code

```yaml
---
- 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"
```

```yaml
- 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"
```

## Correct Code

```yaml
---
- name: Example playbook
  hosts: all
  tasks:
    - name: Template a file to /etc/file.conf
      ansible.builtin.template:
        src: foo.j2 # <- Uses a path from inside templates/ directory.
        dest: /etc/file.conf
        owner: bin
        group: wheel
        mode: "0644"
```

```yaml
- name: Example playbook
  hosts: all
  vars:
    source_path: foo.j2 # <- Uses a path from inside files/ directory.
  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"
```