Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy files with ansible relatively to the role?

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.

like image 249
sorin Avatar asked Feb 18 '16 16:02

sorin


People also ask

How do you copy multiple files into remote nodes by Ansible in a task?

You can use the copy module in your Ansible playbook. This module can copy one or more files to the remote system. But you need to use the item keyword in your playbook for multiple files as shown below.

Does Ansible copy overwrite?

By default, the ansible copy module does a force copy to the destination and overwrites the existing file when present.


2 Answers

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

- copy:     src: foo.conf     dest: /etc/foo.conf 

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

- copy:     src: "{{ role_path }}/files/foo.conf"     dest: /etc/foo.conf 
like image 53
udondan Avatar answered Sep 28 '22 09:09

udondan


You wouldn't need to specify the path of the file to copy, as long as it is stored in files directory.

Here's how your role should look like:

my-awesome-role ├───files │       my-awesome-file └───tasks         main.yml 

And here's the way to call copy in the tasks/main.yml:

- copy:     src: my-awesome-file     dest: '{{ some_destination }}' 
like image 23
avi.elkharrat Avatar answered Sep 28 '22 09:09

avi.elkharrat