

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 範例 6：建立檔案
<a name="cookbooks-101-basics-files"></a>

**重要**  
 AWS OpsWorks Stacks 此服務已於 2024 年 5 月 26 日終止，並已針對新客戶和現有客戶停用。我們強烈建議客戶盡快將其工作負載遷移至其他解決方案。如果您對遷移有任何疑問，請透過 [AWS re：Post](https://repost.aws/) 或透過 [AWS Premium Support](https://aws.amazon.com/support) 聯絡 AWS 支援 團隊。

在您建立目錄之後，您通常需要在目錄中放入組態檔案、資料檔案等等。本主題顯示在執行個體上安裝檔案的兩種方式。

**Topics**
+ [從技術指南安裝檔案](#cookbooks-101-basics-files-cookbook_file)
+ [從範本建立檔案](#cookbooks-101-basics-files-template)

## 從技術指南安裝檔案
<a name="cookbooks-101-basics-files-cookbook_file"></a>

在執行個體上安裝檔案的最簡單方法是使用 [https://docs.chef.io/chef/resources.html#cookbook-file](https://docs.chef.io/chef/resources.html#cookbook-file) 資源，將檔案從技術指南複製到 Linux 和 Windows 系統之執行個體上的指定位置。此範例會擴展[範例 3：建立目錄](cookbooks-101-basics-directories.md)中的配方，在目錄建立之後將資料檔案新增到 `/srv/www/shared`。下列的原始配方提供您參考。

```
directory "/srv/www/shared" do
  mode 0755
  owner 'root'
  group 'root'
  recursive true
  action :create
end
```

**設定技術指南**

1. 在 `opsworks_cookbooks` 目錄中建立名為 `createfile` 的目錄，導覽至它。

1. 將 `metadata.rb` 檔案新增至具有以下內容的 `createfile`。

   ```
   name "createfile"
   version "0.1.0"
   ```

1. 初始化及設定 Test Kitchen (如[範例 1：安裝套件](cookbooks-101-basics-packages.md)中所述)，並從 `platforms` 清單中移除 CentOS。

1. 將 `recipes` 子目錄新增至 `createfile`。

要安裝的檔案包含下列 JSON 資料。

```
{
  "my_name" : "myname",
  "your_name" : "yourname",
  "a_number" : 42,
  "a_boolean" : true
}
```

**設定資料檔案**

1. 將 `files` 子目錄新增至 `createfile`，`default` 子目錄新增至 `files`。所有使用 `cookbook_file` 安裝的檔案都必須位在 `files` 的子目錄中，例如本範例中的 `files/default`。
**注意**  
如果您想要為不同的系統指定不同的檔案，您可以將每個系統專屬的檔案放在以系統為名的子資料夾中，例如 `files/ubuntu`。`cookbook_file` 資源會複製適當的系統專屬檔案 (如果有的話)，否則使用 `default` 檔案。如需詳細資訊，請參閱 [cookbook\$1file](https://docs.chef.io/chef/resources.html#cookbook-file)。

1. 使用上述範例中的 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` 還不存在時才安裝。

**執行配方**

1. 執行 `kitchen destroy` 從全新的執行個體開始。

1. 建立包含前述配方的 `default.rb` 檔案，並儲存至 `recipes`。

1. 執行 `kitchen converge`，然後登入執行個體，驗證 `/srv/www/shared` 是否包含 `example_data.json`。

## 從範本建立檔案
<a name="cookbooks-101-basics-files-template"></a>

`cookbook_file` 資源適合某些用途，但只會安裝技術指南中已有的檔案。[https://docs.chef.io/chef/resources.html#template](https://docs.chef.io/chef/resources.html#template) 透過以動態方式從範本建立檔案，讓您用更有彈性的方式在 Windows 或 Linux 執行個體上安裝檔案。然後，您可以在執行時間判斷檔案的詳細資訊，視需要變更它們。例如，當您啟動執行個體時，您可能希望組態檔案有特定的設定，稍後當您在堆疊中新增更多執行個體時，修改此設定。

本範例修改 `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
```

**設定技術指南**

1. 刪除 `createfile` 技術指南的 `files` 目錄及其內容。

1. 將 `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\$1name" 值是表示技術指南屬性檔案中其中一個屬性值的預留位置。
+ 如前文所述，`<%=@...%>` 表示範本資源中定義的變數值。

**建立範本檔案**

1. 將 `templates` 子目錄新增至 `createfile` 技術指南，`default` 子目錄新增至 `templates`。
**注意**  
`templates` 目錄的作用如同 `files` 目錄。您可以將系統專屬範本放入以系統為名的子目錄，例如 `ubuntu`。`template` 資源會使用適當的系統專屬範本 (如果有的話)，否則使用 `default` 範本。

1. 建立名為 `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_if` *guard* 屬性指示資源只有當 `['createfile']['install_file']` 設為 `true` 時才建立檔案。

**執行配方**

1. 執行 `kitchen destroy` 從全新的執行個體開始。

1. 以前述範例取代 `recipes/default.rb` 中的程式碼。

1. 執行 `kitchen converge`，然後登入執行個體，驗證檔案是否位於 `/srv/www/shared` 且具有正確的內容。

完成後，請執行 `kitchen destroy` 關機執行個體。下一節使用新的技術指南。