Review Ansible file path resolution

Verify Ansible’s file-search context so the intended templates and files are deployed consistently.

Description

For local src files, template and copy resolve relative paths on the control node. The role, task file and playbook determine the search path independently of the shell’s working directory.

Potential impact

Changing where a task is included can deploy the wrong configuration or stop deployment when the source is missing.

Remediation

Keep sources in a managed role directory or specify a known base path. Inspect ansible_search_path and verify which file the task selects in the deployment environment. Restrict who can write to the source files and their directories.

Examples

Ensure /etc/app already exists on the target and the account running the task can write files there.

Before

yaml
---
- name: 설정 파일 배포
  hosts: localhost
  tasks:
    - name: 템플릿 복사
      ansible.builtin.template:
        src: ../templates/app.conf.j2
        dest: /etc/app/app.conf
        mode: "0644"

    - name: 정적 파일 복사
      ansible.builtin.copy:
        src: ../files/banner.txt
        dest: /etc/app/banner.txt
        mode: "0644"

After

yaml
---
- name: 설정 파일 배포
  hosts: localhost
  tasks:
    - name: 템플릿 복사
      ansible.builtin.template:
        src: /opt/ansible/templates/app.conf.j2
        dest: /etc/app/app.conf
        mode: "0644"

    - name: 정적 파일 복사
      ansible.builtin.copy:
        src: /opt/ansible/files/banner.txt
        dest: /etc/app/banner.txt
        mode: "0644"

The second excerpt uses absolute paths under /opt/ansible. Provide the same trusted source files at those paths in each execution environment.

References