Ansible

Ansible

Made by DeepSource

Relative path is not needed in role ANS-E4004

Anti-pattern
Major

copy and template do not need to use relative path for src. This removes the need for knowing the location of the root directory.

Bad practice

- name: Copy file with owner and permissions
  copy:
    src: "{{ playbook_dir }}/../../srv/myfiles/foo.conf"
    dest: /etc/foo.conf
    owner: foo
    group: foo
    mode: '0644'

- name: Template a file to /etc/file.conf
  template:
    src: "{{ playbook_dir }}/../../srv/mytemplates/foo.j2"
    dest: /etc/file.conf
    owner: bin
    group: wheel
    mode: '0644'

Recommended

- name: Copy file with owner and permissions
  copy:
    src: /srv/myfiles/foo.conf
    dest: /etc/foo.conf
    owner: foo
    group: foo
    mode: '0644'

- name: Template a file to /etc/file.conf
  template:
    src: /mytemplates/foo.j2
    dest: /etc/file.conf
    owner: bin
    group: wheel
    mode: '0644'