When an Ansible Role appears to ignore the files/ directory

I’m writing an Ansible Role, and I can’t figure out why in the world the template module isn’t looking in ./roles/myrole/files/words.txt to locate the file I’m trying to push from the control machine to the remote machine..

In case you want to write a role, running this command will create a nice directory structure for it:

ansible-galaxy init name_of_your_role

Here’s the task:

- name: "copy words.txt to remote machine"
    src: "words.txt"
    dest: "/opt/path"

…And the error:

failed: [REDACTED] (item={u'dest': u'/opt/path', u'src': u'words.txt'}) => changed=false
    dest: /opt/path
    src: words.txt
  msg: |-
    Could not find or access 'words.txt'
    Searched in:

But why?! I’ll tell you why (With my bolding)!

Any copy, script, template or include tasks (in the role) can reference files in roles/x/{files,templates,tasks}/ (dir depends on task) without having to path them relatively or absolutely.

I was originally using the templates module. I had an existing task that transferred templates, so I bundled words.txt files into the same task.

What I really needed to use was the synchronize module:

- name: "copy words.txt to remote machine"
    mode: "push"
    src: "words.txt"
    dest: "/opt/path"


  • https://docs.ansible.com/ansible/2.5/user_guide/playbooks_reuse_roles.html#using-roles

© 2021. All rights reserved.