本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
範例 6:建立檔案
重要
AWS OpsWorks Stacks 此服務已於 2024 年 5 月 26 日終止,並已針對新客戶和現有客戶停用。我們強烈建議客戶盡快將其工作負載遷移至其他解決方案。如果您對遷移有任何疑問,請透過 AWS re:Post
在您建立目錄之後,您通常需要在目錄中放入組態檔案、資料檔案等等。本主題顯示在執行個體上安裝檔案的兩種方式。
從技術指南安裝檔案
在執行個體上安裝檔案的最簡單方法是使用 cookbook_file/srv/www/shared。下列的原始配方提供您參考。
directory "/srv/www/shared" do mode 0755 owner 'root' group 'root' recursive true action :create end
設定技術指南
-
在
opsworks_cookbooks目錄中建立名為createfile的目錄,導覽至它。 -
將
metadata.rb檔案新增至具有以下內容的createfile。name "createfile" version "0.1.0" -
初始化及設定 Test Kitchen (如範例 1:安裝套件中所述),並從
platforms清單中移除 CentOS。 -
將
recipes子目錄新增至createfile。
要安裝的檔案包含下列 JSON 資料。
{ "my_name" : "myname", "your_name" : "yourname", "a_number" : 42, "a_boolean" : true }
設定資料檔案
-
將
files子目錄新增至createfile,default子目錄新增至files。所有使用cookbook_file安裝的檔案都必須位在files的子目錄中,例如本範例中的files/default。注意
如果您想要為不同的系統指定不同的檔案,您可以將每個系統專屬的檔案放在以系統為名的子資料夾中,例如
files/ubuntu。cookbook_file資源會複製適當的系統專屬檔案 (如果有的話),否則使用default檔案。如需詳細資訊,請參閱 cookbook_file。 -
使用上述範例中的 JSON 建立名為
example_data.json的檔案,然後將它新增至files/default。
下列配方會將 example_data.json 複製到指定的位置。
directory "/srv/www/shared" do mode 0755 owner 'root' group 'root' recursive true action :create end cookbook_file "/srv/www/shared/example_data.json" do source "example_data.json" mode 0644 action :create_if_missing end
在目錄資源建立 /srv/www/shared 之後,cookbook_file 資源會將 example_data.json 複製到該目錄,並且設定檔案的使用者、群組和模式。
注意
cookbook_file 資源引入新的動作:create_if_missing。您也可以使用 create 動作,但這會覆寫現有的檔案。如果您不想覆寫任何內容,請使用 create_if_missing,當 example_data.json 還不存在時才安裝。
執行配方
-
執行
kitchen destroy從全新的執行個體開始。 -
建立包含前述配方的
default.rb檔案,並儲存至recipes。 -
執行
kitchen converge,然後登入執行個體,驗證/srv/www/shared是否包含example_data.json。
從範本建立檔案
cookbook_file 資源適合某些用途,但只會安裝技術指南中已有的檔案。template
本範例修改 createfile 技術指南以使用 template 資源安裝稍為修改的 example_data.json 版本。
已安裝的檔案看起來如下:
{ "my_name" : "myname", "your_name" : "yourname", "a_number" : 42, "a_boolean" : true, "a_string" : "some string", "platform" : "ubuntu" }
Template 資源通常搭配屬性檔案使用,因此範例會使用一個屬性檔案來定義下列值。
default['createfile']['my_name'] = 'myname' default['createfile']['your_name'] = 'yourname' default['createfile']['install_file'] = true
設定技術指南
-
刪除
createfile技術指南的files目錄及其內容。 -
將
attributes子目錄新增至createfile,然後將default.rb檔案新增至包含前述屬性定義的attributes。
範本是一種 .erb 檔案,基本上是最終檔案的複本,某些內容由預留位置表示。當 template 資源建立檔案時,會將範本的內容複製到指定的檔案,以其獲指派的值覆寫預留位置。下列為 example_data.json 範本。
{ "my_name" : "<%= node['createfile']['my_name'] %>", "your_name" : "<%= node['createfile']['your_name'] %>", "a_number" : 42, "a_boolean" : <%= @a_boolean_var %>, "a_string" : "<%= @a_string_var %>", "platform" : "<%= node['platform'] %>" }
這些 <%=...%> 值是預留位置。
-
<%=node[...]%>表示節點屬性值。在此範例中,"your_name" 值是表示技術指南屬性檔案中其中一個屬性值的預留位置。
-
如前文所述,
<%=@...%>表示範本資源中定義的變數值。
建立範本檔案
-
將
templates子目錄新增至createfile技術指南,default子目錄新增至templates。注意
templates目錄的作用如同files目錄。您可以將系統專屬範本放入以系統為名的子目錄,例如ubuntu。template資源會使用適當的系統專屬範本 (如果有的話),否則使用default範本。 -
建立名為
example_data.json.erb的檔案,放入templates/default目錄。範本可使用任意名稱,但通常會在檔案名稱後附加.erb,包括任何副檔名。
下列配方使用 template 資源建立 /srv/www/shared/example_data.json。
directory "/srv/www/shared" do mode 0755 owner 'root' group 'root' recursive true action :create end template "/srv/www/shared/example_data.json" do source "example_data.json.erb" mode 0644 variables( :a_boolean_var => true, :a_string_var => "some string" ) only_if {node['createfile']['install_file']} end
此 template 資源從範本建立 example_data.json,然後將它安裝在 /srv/www/shared。
-
範本名稱
/srv/www/shared/example_data.json,指定已安裝檔案的路徑和名稱。 -
source屬性指定建立檔案所使用的範本。 -
mode屬性指定已安裝檔案的模式。 -
資源會定義兩個變數:
a_boolean_var和a_string_var。當資源建立
example_data.json時,會使用資源中的對應值覆寫範本中的變數預留位置。 -
only_ifguard 屬性指示資源只有當['createfile']['install_file']設為true時才建立檔案。
執行配方
-
執行
kitchen destroy從全新的執行個體開始。 -
以前述範例取代
recipes/default.rb中的程式碼。 -
執行
kitchen converge,然後登入執行個體,驗證檔案是否位於/srv/www/shared且具有正確的內容。
完成後,請執行 kitchen destroy 關機執行個體。下一節使用新的技術指南。