How to copy files with ansible relatively to the role?

Question:

I have a copy task inside a role and I was expecting that the src location would be relative to the role itself, not the playbook that calls the roles.

How do I make this work and use the files from myfrole/files from a task inside myrole/tasks, I don’t want to include the role name as part of the path as it does not make much sense. If I do it will break if I duplicate the role.

Answer:

If you do not provide any path at all, just the filename, Ansible will pick it automatically from the files directory of the role.


Additionally, since Ansible 1.8, there is the variable role_path which you could use in your copy task.

Leave a Reply