Search code examples
ansible

How to Include_tasks with relative path?


I am trying to Include a file which contains tasks, but it fails:

ERROR! Unable to retrieve file contents Could not find or access '/roles/k8s/tasks/Get_volumes.yaml' on the Ansible Controller. If you are using a module and expect the file to exist on the remote, see the remote_src option

Below is my ansible script. As I am going to test only a few tasks, I have included the tasks file which is inside the roles folder. I am not sure if it fails because of the roles folder.

---
- hosts: localhost
  vars:
    local_kubectl: kubectl
    local_array_api_url: https://Mystoragearray:5392/v1
    array_username: admin
    array_password: admin

  tasks: 
    #- import_tasks: /roles/k8s/tasks/GetVolume_token.yaml
    - include_tasks: /roles/k8s/tasks/Get_volumes.yaml

Solution

  • Q: How to include_tasks with relative path?

    A: Place the file relative to the playbook base directory playbook_dir. For example

        - include_tasks: Get_volumes.yaml
    

    is the same as

        - include_tasks: "{{ playbook_dir }}/Get_volumes.yaml"
    

    If the directory /roles is configured in DEFAULT_ROLES_PATH then include_role might be a better option

        - include_role:
            name: k8s
            tasks_from: Get_volumes.yaml