如何复制文件与相关角色相关?

我在一个角色中有一个复制任务,我希望 src的位置是相对于角色本身的,而不是调用角色的剧本。

我如何使这个工作和使用来自 myrole/tasks内部任务的 myfrole/files文件,我不想把角色名称作为路径的一部分,因为它没有多大意义。如果我这样做,它将打破,如果我重复的作用。

103809 次浏览

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

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 }}'