不可接受的最佳做法描述了每个角色都包含有此规则所需的所有文件的文件目录。
在我的例子中,我有不同的角色共享相同的文件。但是,我不能在每个角色中复制这些文件,因为这些文件没有一个来源,如果其中一个文件被编辑,那么对每个角色进行这种更改将变得非常乏味。
我做的一个解决方案是创建另一个文件夹并使用绝对路径或相对路径引用它。,这是最好的方法吗?
我的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发布于 2015-12-15 13:39:45
你有两个相当不错的方法,你可以尝试在这里减少重复。
您可以有一个单独的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然后,您将在具有相对文件位置的任务中引用角色/文件文件夹的位置:
- 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或者,如果要使用指向文件夹的相对路径,则可以通过以下方式进行符号链接:
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然后,您可以直接引用该文件,就好像它在角色/ file目录中一样:
- 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发布于 2015-12-22 12:40:34
我的解决方案是为常见内容创建角色,并将它们作为依赖项添加。
例如,您的游戏手册将如下所示
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/因此,roles/common-1和roles/common-2是只部署文件的角色,所有需要这些文件的角色都将其作为meta/文件夹中的依赖项。
发布于 2019-08-16 09:58:01
Ansible实际上支持项目范围内的公共文件夹。不需要创建自定义命名文件夹.参见和在另一个问题全局模板文件夹?中的回答
tasks/根级别的ansible/文件夹也被接受。
顺便提一句,我找不到关于这种目录结构layout#目录-布局的任何正式文档。
https://stackoverflow.com/questions/34287465
复制相似问题