When you develop your Ansible playbooks, there comes a point when you’ll move your tasks to other files. For example to a tasks list, another playbook or a role. You might also want to reorganize your playbooks for better visibility.

To make all of this easier, it is possible to use absolute paths to reference other files.

To do that, set the following variables for the all group:

ansible_repo_path: "{{ ansible_config_file[:-12] }}" # Get current repo path : remove trailing /ansible.cfg

files_path: "{{ ansible_repo_path }}/assets/files"
templates_path: "{{ ansible_repo_path }}/assets/templates"
tasks_path: "{{ ansible_repo_path }}/assets/tasks"

vaults_path: "{{ ansible_repo_path }}/inventory/vaults"

Then, after creating the file assets/files/foo.conf, you can reference it in your playbooks like this:

- name: Copy file
  ansible.builtin.copy:
    src: "{{ files_path }}/foo.conf"
    dest: /etc/foo.conf

/!\ This use the fact that the file ansible.cfg exists at the root of your repository. If this file doesn’t exists, this method won’t work.