Ansible Best Practices描述了每个角色都包含具有此规则所需的所有文件的文件目录.
在我的情况下,我有不同的角色,共享相同的文件.但我不能在每个角色中复制这些文件,因为这些文件没有任何一个来源,如果其中一个文件发生编辑,对每个角色进行此更改将变得很繁琐.
我做的一个解决方案是创建另一个文件夹并使用绝对或相对路径引用它.这是最好的方式吗?
我的ansible目录看起来像这样
play.yml
roles/
web/
tasks/
files/
common-1
common-2
other-multiple-files
role-2/
tasks/
files/
common-1
common-2
other-multiple-files
role-3/
tasks/
files/
common-2
role-4/
tasks/
files/
common-1
Run Code Online (Sandbox Code Playgroud)
yda*_*coR 21
你可以尝试两种相当不错的方法来减少重复.
您可以拥有一个单独的shared-files目录,作为您的角色文件夹的兄弟,如下所示:
play.yml
roles/
web/
tasks/
files/
other-multiple-files
role-2/
tasks/
files/
other-multiple-files
role-3/
tasks/
role-4/
tasks/
shared-files/
common-1
common-2
Run Code Online (Sandbox Code Playgroud)
然后,您将在任务中使用角色/文件夹所在的相对文件位置引用它:
- name: copy common-1
copy:
src: ../../common-1
dest: /path/to/dest/common-1
- name: copy role specific file
src: other-multiple-files
dest: /path/to/dest/other-multiple-files
Run Code Online (Sandbox Code Playgroud)
或者,要使用文件夹的相对路径,您可以像这样对符号进行符号链接:
play.yml
roles/
web/
tasks/
files/
common-1 -> ../../common-1
common-2 -> ../../common-2
other-multiple-files
role-2/
tasks/
files/
common-1 -> ../../common-1
common-2 -> ../../common-2
other-multiple-files
role-3/
tasks/
files/
common-2 -> ../../common-2
role-4/
tasks/
files/
common-1 -> ../../common-1
shared-files/
common-1
common-2
Run Code Online (Sandbox Code Playgroud)
然后,您可以直接引用该文件,就好像它位于角色/ files目录中一样:
- name: copy common-1
copy:
src: common-1
dest: /path/to/dest/common-1
- name: copy role specific file
src: other-multiple-files
dest: /path/to/dest/other-multiple-files
Run Code Online (Sandbox Code Playgroud)
use*_*522 12
我的解决方案是为常见的东西创建角色并将它们添加为依赖项.
例如,你的剧本看起来像这样
play.yml
roles/
common-1/
files/
common-1
common-2/
files/
common-2
web/
meta/
common-1
common-2
tasks/
files/
other-multiple-files
role-2/
meta/
common-1
common-2
tasks/
files/
other-multiple-files
role-3/
meta/
common-2
tasks/
role-4/
meta/
common-1
tasks/
Run Code Online (Sandbox Code Playgroud)
所以,roles/common-1和roles/common-2是角色刚刚部署的文件,需要与所有角色,他们有它作为一个依赖meta/文件夹中.