2015-12-15 85 views
5

Ansible Best Practices描述了每個角色包含具有該規則所需的所有文件的文件目錄。Ansible在角色之間共享文件

在我的情況下,我有不同的角色共享相同的文件。但是我不能在每個角色中創建這些文件的副本,因爲這些文件不會有任何來源,並且如果編輯發生在其中一個角色中,對每個角色進行此更改都會變得繁瑣。

我做的一個解決方案是創建另一個文件夾並使用絕對路徑或相對路徑引用它。 這是做這件事的最好方法嗎?

我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 
+0

重構出使用了新的角色共同文件,這兩個角色的任務,那麼必須爲這兩個 –

+0

@中元/ main.yml的依賴,如果這個新角色ᴳᵁᴵᴰᴼ兩個規則具有相同的文件,他們之間我可以做到這一點,但如果我有更多的規則,包含一些文件創建角色作爲依賴不會適用。對不起,我對此不夠清楚,我在問題中增加了兩條規則,以使我的觀點清楚 – Nasr

回答

10

你有兩個你可以試試在這裏,以減少重複合理體面的方式。

你可以有一個獨立的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 

然後你就可以引用該文件,就好像它是直接在角色/文件目錄:

- 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 
+1

我真的很喜歡符號鏈接方法,它將使角色免於路徑,並且路徑只會在符號鏈接中設置一次。 – Nasr

+0

我在我的角色中使用很多符號鏈接來共享文件,模板等等。 git可以很好地處理符號鏈接,因此各種文件和模板目錄之間的相對符號鏈接是小菜一碟。 –

5

我的解決辦法是爲共同的東西,創建角色,並將其添加爲依賴關係。

例如,你的劇本是這樣的

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-2的角色只是部署的文件,需要與所有角色,他們有它作爲meta/依賴夾。

-2

簡答題(應該有一個簡明扼要的回答加分)O的問題:

沒有

更好的是使用git的子模塊。來源:

http://random-notes-chris.blogspot.co.uk/2014/02/git-submodules-for-ansible-roles.html

Every role lives in its own git repo. An example is Vincent Rischmann's ansible-role-java. 
Every app (or project) has its own git repo for its Ansible configuration (inventory definition and playbooks etc.). 
The app repo has a roles sub directory. Actual roles (e.g. the java role) are then added as git submodules. 

git submodule add https://github.com/vrischmann/ansible-role-java.git roles/java