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"
  template:
    src: "words.txt"
    dest: "/opt/path"

…And the error:

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

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"
  synchronize:
    mode: "push"
    src: "words.txt"
    dest: "/opt/path"

References

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

This is a preview of Clap Button, a new feedback and analytics tools for Hydejack, built by yours truly. You can try it out on localhost for free, but it will be removed (together with this message) when building with JEKYLL_ENV=production. To use Clap Button on your site, get a subscription
and set clap_button: true in your config file.


© 2021. All rights reserved.