

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

# 範本
<a name="workingcookbook-installingcustom-components-templates"></a>

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

您可以建立組態檔案並將其放在適當的目錄中，以設定許多套件。您可以在技術指南中加入組態檔案，並將其複製到適當的目錄中，但更靈活的方法是使用配方以透過範本建立組態檔。範本的其中一個優點是，您可以使用屬性來定義範本的值。如此一來，您就可以使用自訂 JSON 覆寫適當的屬性值以修改組態檔案，而無需動用到技術指南。

基本上，範本的內容和結構與其相關聯的檔案相同。下列 `httpd.conf` 即為範例檔。

```
ServerRoot "<%= node[:apache][:dir] %>"
<% if node[:platform] == "debian" || node[:platform] == "ubuntu" -%>
  LockFile /var/lock/apache2/accept.lock
<% else -%>
   LockFile logs/accept.lock
<% end -%>
PidFile <%= node[:apache][:pid_file] %>
Timeout <%= node[:apache][:timeout] %>
KeepAlive <%= node[:apache][:keepalive] %>
MaxKeepAliveRequests <%= node[:apache][:keepaliverequests] %>
KeepAliveTimeout <%= node[:apache][:keepalivetimeout] %>
<IfModule mpm_prefork_module>
    StartServers          <%= node[:apache][:prefork][:startservers] %>
    MinSpareServers       <%= node[:apache][:prefork][:minspareservers] %>
    MaxSpareServers       <%= node[:apache][:prefork][:maxspareservers] %>
    ServerLimit           <%= node[:apache][:prefork][:serverlimit] %>
    MaxClients            <%= node[:apache][:prefork][:maxclients] %>
    MaxRequestsPerChild   <%= node[:apache][:prefork][:maxrequestsperchild] %>
</IfModule>
...
```

下列範例是針對 Ubuntu 執行個體產生的 `httpd.conf` 檔案：

```
ServerRoot "/etc/httpd"
LockFile logs/accept.lock
PidFile /var/run/httpd/httpd.pid
Timeout 120
KeepAlive Off
MaxKeepAliveRequests 100
KeepAliveTimeout 3
<IfModule mpm_prefork_module>
    StartServers          16
    MinSpareServers       16
    MaxSpareServers       32
    ServerLimit           400
    MaxClients            400
    MaxRequestsPerChild   10000
</IfModule>
...
```

範本的許多文字都是從範本複製到 `httpd.conf` 檔案。不過，`<%= ... %>` 內容的處理方式則如下所示：
+ Chef 會將 `<%= node[:attribute][:sub_attribute][:...]%>` 取代為屬性值。

  例如，`StartServers <%= node[:apache][:prefork][:startservers] %>` 會變成 `httpd.conf` 中的 `StartServers 16`。
+ 您可以使用 `<%if-%>, <%else-%>, and <%end-%>` 依條件選取值。

  此範例會根據平台設定不同的 `accept.lock` 檔案路徑。

**注意**  
您並不一定僅限於使用技術指南屬性檔案中的屬性。您可以在執行個體的節點物件中使用任何屬性。例如，由 Chef 工具 (名稱為 [Ohai](https://docs.chef.io/ohai.html)) 產生的屬性也會納入節點物件。如需屬性的詳細資訊，請參閱[覆寫屬性](workingcookbook-attributes.md)。

如需範本的詳細資訊，包括如何採用 Ruby 程式碼，請參閱[關於範本](http://docs.chef.io/templates.html)。