

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

# 使用入門套件設定 Chef 伺服器
<a name="opscm-starterkit"></a>

**重要**  
AWS OpsWorks for Chef Automate 已於 2024 年 5 月 5 日終止服務，並已針對新客戶和現有客戶停用。我們建議現有客戶遷移至 Chef SaaS 或其他解決方案。如果您有任何問題，可以在 [AWS re：Post](https://repost.aws/) 或透過 [AWS Premium Support](https://aws.amazon.com/support) 聯絡 AWS 支援 團隊。

當 Chef 伺服器建立仍在進行時，在 AWS OpsWorks for Chef Automate 主控台中開啟其 Properties (屬性) 頁面。當您第一次使用新的 Chef 伺服器時，Properties (屬性) 頁面會提示您下載兩個必要項目。請下載這些項目後再連線至您的 Chef 伺服器；在新的伺服器上線之後，將無法使用下載按鈕。

![\[AWS OpsWorks for Chef Automate 新的伺服器屬性頁面\]](http://docs.aws.amazon.com/zh_tw/opsworks/latest/userguide/images/opscm_serverpropsdownload.png)

+ **Sign-in credentials for the Chef server. (Chef 伺服器的登入資料。)** 您將使用這些登入資料來登入 Chef Automate 儀表板，您可以在其中使用 Chef Automate 進階功能，例如工作流程和合規掃描。 OpsWorks 不會儲存這些登入資料；這是最後一次可供檢視和下載。如有需要，您可以在登入之後變更這些登入資料隨附的密碼。
+ **Starter Kit. (入門套件。)** 入門套件包含內附範例的讀我檔案、`knife.rb` 組態檔案，以及主要或樞紐使用者的私有金鑰。每次下載入門套件，都會產生新的金鑰對，並重設舊的金鑰。

除了只能與新伺服器搭配使用的登入資料之外，入門套件 .zip 檔案還包含可與任何 AWS OpsWorks for Chef Automate 伺服器搭配使用的 Chef 儲存庫簡單範例。在 Chef 儲存庫中，您可以存放技術指南、角色、組態檔案，以及可透過 Chef 管理您節點的其他成品。我們建議您將此儲存庫存放在版本控制系統中 (例如 Git)，並將其視為來源碼。如需說明如何設定 Git 中追蹤之 Chef 儲存庫的資訊和範例，請參閱 Chef 文件中的 [About the chef-repo](https://docs.chef.io/chef_repo.html)。

## 先決條件
<a name="finish-server-prereqs"></a>

1. 當伺服器建立仍在進行時，下載 Chef 伺服器的登入資料，並將其儲存在安全但方便的位置。

1. 下載入門套件，並將入門套件 .zip 檔案解壓縮至您的工作空間目錄。請勿共享入門套件私有金鑰。如果其他使用者將要管理 Chef 伺服器，請稍後將其新增為 Chef Automate 儀表板的管理員。

1. 在您將用來管理 Chef 伺服器和節點的電腦上下載並安裝 [Chef Workstation](https://downloads.chef.io/products/workstation) （先前稱為 Chef Development Kit 或 Chef DK)。[https://docs.chef.io/knife.html](https://docs.chef.io/knife.html) 公用程式是 Chef Workstation 的一部分。如需說明，請參閱 [Chef 網站上的安裝 Chef 工作站](https://docs.chef.io/workstation/install_workstation/)。

## 探索入門套件內容
<a name="w2ab1b9c28c15c15"></a>

入門套件包含下列內容。
+ `cookbooks/` - 用於您所建立技術指南的目錄。`cookbooks/` 資料夾包含`opsworks-webserver`技術指南，這是包裝函式技術指南，取決於 [Chef Supermarket](https://supermarket.chef.io/cookbooks/nginx) 網站上的`nginx`技術指南。如果`cookbooks/`目錄中沒有技術指南相依性，則 `Policyfile.rb` 預設為 Chef 超級市場作為次要來源。
+ `Policyfile.rb` – 以 Ruby 為基礎的政策檔案，其定義了將成為您節點政策的技術指南、相依性和屬性。
+ `userdata.sh` 和 `userdata.ps1` - 您可以使用使用者資料檔案，在您的 Chef Automate 伺服器啟動之後自動關聯節點。`userdata.sh` 是用於引導以 Linux 為基礎的節點，而 `userdata.ps1` 是用於以 Windows 為基礎的節點。
+ `Berksfile` - 如果您偏好使用 Berkshelf 和 `berks` 命令來上傳技術指南及其相依性，則您可已使用此檔案。在本逐步教學中，我們會使用 `Policyfile.rb` 和 Chef 命令來上傳技術指南、相依性和屬性。
+ `README.md` 是 Markdown 型的檔案，其描述如何使用入門套件來首次設定您的 Chef Automate 伺服器。
+ `.chef` 是一種隱藏目錄，其中包含一個 knife 組態檔案 (`knife.rb`) 和私密驗證金鑰檔案 (.pem)。
  + `.chef/knife.rb` - knife 組態檔案 (`knife.rb`)。已設定 [https://docs.chef.io/config_rb_knife.html](https://docs.chef.io/config_rb_knife.html) 檔案，讓 Chef [https://docs.chef.io/knife.html](https://docs.chef.io/knife.html)的工具操作對 AWS OpsWorks for Chef Automate 伺服器執行。
  + `.chef/ca_certs/opsworks-cm-ca-2020-root.pem` - 憑證授權機構 (CA) 簽署的 SSL 私有金鑰，由 OpsWorks所提供。此金鑰可讓伺服器向您伺服器管理之節點上的 Chef Infra 用戶端代理程式表明身分。

## 設定您的 Chef 儲存庫
<a name="w2ab1b9c28c15c17"></a>

Chef 儲存庫包含數個目錄。入門套件中的每個目錄包含讀我檔案，用於描述目錄的用途，以及如何使用它透過 Chef 來管理您的系統。您可以用兩種方式在 Chef 伺服器上安裝技術指南：執行 `knife` 命令或執行 Chef 命令，將政策檔案 (`Policyfile.rb`) 上傳到負責下載並安裝指定技術指南的伺服器。此逐步解說使用 Chef 命令和 `Policyfile.rb`，在您的伺服器上安裝技術指南。

1. 在您的本機電腦上建立用來存放技術指南的目錄，例如 `chef-repo`。將技術指南、角色和其他檔案新增至此儲存庫後，建議您將其上傳或存放在安全、版本控制的系統中，例如 CodeCommit、Git 或 Amazon S3。

1. 在 `chef-repo` 目錄中，建立以下目錄：
   + `cookbooks/` - 存放技術指南。
   + `roles/` - 以 `.rb` 或 `.json` 格式存放角色。
   + `environments/` - 以 `.rb` 或 `.json` 格式存放環境。

## 使用 Policyfile.rb 取得遠端來源的技術指南
<a name="install-cookbooks-policyfile"></a>

在本節中，您要編輯 `Policyfile.rb` 來指定技術指南，然後執行 Chef 命令，將檔案上傳到您的伺服器和技術指南。

1. 在您的入門套件中查看 `Policyfile.rb`。在預設情況下， `Policyfile.rb` 包含 `opsworks-webserver` 包裝函式技術指南，實際內容會依 Chef Supermarket 網站所提供 [https://supermarket.chef.io/cookbooks/nginx](https://supermarket.chef.io/cookbooks/nginx) 技術指南而定。`nginx` 技術指南會在受管節點上安裝和設定 Web 伺服器。這時也會指定必要的 `chef-client` 技術指南，其將在受管節點上安裝 Chef Infra 用戶端代理程式。

   `Policyfile.rb` 也會指向選用的 Chef Audit 技術指南，其可供您設定節點上的合規掃描。如需為受管節點設定合規掃描和取得合規結果的詳細資訊，請參閱 [中的合規掃描 AWS OpsWorks for Chef Automate](opscm-chefcompliance.md)。如果您不想要現在設定合規掃描和稽核，請刪除 `run_list` 區段中的 `'audit'`，而且不要在檔案結尾指定 `audit` 技術指南屬性。

   ```
   # Policyfile.rb - Describe how you want Chef to build your system.
   #                 
   # For more information about the Policyfile feature, visit                                             
   # https://docs.chef.io/policyfile.html                                                                 
   
   # A name that describes what the system you're building with Chef does.                                
   name 'opsworks-demo-webserver'
   
   # The cookbooks directory is the preferred source for external cookbooks                               
   default_source :chef_repo, "cookbooks/" do |s|                                                         
     s.preferred_for "nginx", "windows", "chef-client", "yum-epel", "seven_zip",  
                     "build-essential", "mingw", "ohai", "audit", "logrotate", "cron"                     
   end
   # Alternative source 
   default_source :supermarket                                                                            
   
   # run_list: chef-client runs these recipes in the order specified.                                 
   run_list  'chef-client',
             'opsworks-webserver',
             'audit'
   # add 'ssh-hardening' to your runlist to fix compliance issues detected by the ssh-baseline profile    
   
   # Specify a custom source for a single cookbook:                                                       
   cookbook 'opsworks-webserver', path: 'cookbooks/opsworks-webserver'                               
   
   # Policyfile defined attributes
   
   # Define audit cookbook attributes
   default["opsworks-demo"]["audit"]["reporter"] = "chef-server-automate"
   default["opsworks-demo"]["audit"]["profiles"] = [
     {
       "name": "DevSec SSH Baseline",
       "compliance": "admin/ssh-baseline"
     }
   ]
   ```

   下面介紹當您現在只想要設定 `nginx` Web 伺服器時，所出現不含 `audit` 技術指南和屬性的 `Policyfile.rb`。

   ```
   # Policyfile.rb - Describe how you want Chef to build your system.
   #
   # For more information on the Policyfile feature, visit
   # https://docs.chef.io/policyfile.html
   
   # A name that describes what the system you're building with Chef does.
   name 'opsworks-demo-webserver'
   
   # Where to find external cookbooks:
   default_source :supermarket
   
   # run_list: chef-client will run these recipes in the order specified.
   run_list  'chef-client',
             'opsworks-webserver'
   
   # Specify a custom source for a single cookbook:
   cookbook 'opsworks-webserver', path: 'cookbooks/opsworks-webserver'
   ```

   如果您變更 `Policyfile.rb`, 請務必儲存檔案。

1. 下載並安裝 `Policyfile.rb` 中所定義的技術指南。

   ```
   chef install
   ```

   所有技術指南的版本都是經由技術指南的 `metadata.rb` 檔案所控制。每次變更技術指南時，您都必須在技術指南的 `metadata.rb` 中提高其版本。

1. 如果您選擇了設定合規掃描，並在政策檔案中保留 `audit` 說明書資訊，則請將政策 `opsworks-demo` 推送到您的伺服器。

   ```
   chef push opsworks-demo
   ```

1. 如果您已完成步驟 3，請驗證您的政策安裝情況。執行下列命令。

   ```
   chef show-policy
   ```

   結果應類似以下內容：

   ```
   opsworks-demo-webserver 
   ======================= 
   * opsworks-demo:  ec0fe46314
   ```

1. 您現在可以將節點新增或引導到您的 Chef Automate 伺服器。您可以遵循[在 中自動新增節點 AWS OpsWorks for Chef Automate](opscm-unattend-assoc.md)中的步驟將節點的關聯自動化，或遵循[個別新增節點](opscm-addnodes-individually.md)中的步驟來逐一新增節點。

## (替代方法) 使用 Berkshelf 取得遠端來源的技術指南
<a name="opscm-berkshelf"></a>

Berkshelf 是用於管理技術指南及其相依性的一種工具。如果您在將技術指南安裝到本機儲存空間時，偏好使用 Berkshelf 而不是 `Policyfile.rb` 進行安裝，請使用本節所明程序，而不要用先前章節內容。您可以指定要搭配您的 Chef 伺服器使用的技術指南和版本，之後再予以上傳。此入門套件包含名為 `Berksfile` 的檔案，其中列出您的技術指南。

1. 若要開始使用，請將 `chef-client` 技術指南新增到已包含的 Berksfile。`chef-client` 技術指南會為每部連接到 Chef Automate 伺服器的節點設定 Chef Infra 代理程式軟體。若要進一步了解此技術指南，請參閱 Chef Supermarket 中的 [Chef Client Cookbook](https://supermarket.chef.io/cookbooks/chef-client)。

1. 使用文字編輯器，將另一個安裝Web 伺服器應用程式的其他技術指南附加到 Berksfile；例如，安裝 Apache Web 伺服器的 `apache2` 技術指南。您的 Berksfile 應該如下所示。

   ```
   source 'https://supermarket.chef.io'
   cookbook 'chef-client'
   cookbook 'apache2'
   ```

1. 在您的本機電腦上下載並安裝技術指南。

   ```
   berks install
   ```

1. 將技術指南上傳至 Chef 伺服器。

   在 Linux 上，執行：

   ```
   SSL_CERT_FILE='.chef/ca_certs/opsworks-cm-ca-2020-root.pem' berks upload
   ```

   在 Windows 上，在 PowerShell 工作階段中執行下列 Chef Workstation 命令。執行命令之前，請務必將 PowerShell 中的執行政策設為 `RemoteSigned`。新增 `chef shell-init` 以讓 Chef Workstation 公用程式命令可供 PowerShell 使用。

   ```
   $env:SSL_CERT_FILE="ca_certs\opsworks-cm-ca-2020-root.pem"
   chef shell-init berks upload
   Remove-Item Env:\SSL_CERT_FILE
   ```

1. 顯示 Chef Automate 伺服器上目前可用的技術指南清單，驗證技術指南的安裝情況。您可以執行下列 `knife` 命令，即可進行新增：

   您已準備好使用 AWS OpsWorks for Chef Automate 伺服器新增要管理的節點。

   ```
   knife cookbook list
   ```

## (選用) 設定 `knife` 以使用自訂網域
<a name="opscm-starterkit-customdomain"></a>

如果您的 Chef Automate 伺服器使用自訂網域，您可能需要新增 (指派您伺服器的憑證鏈的) 根 CA 的 PEM 憑證，或者如果憑證是自我簽署的，則需要新增伺服器 PEM 憑證。`ca_certs` 是 `chef/` 的一個子目錄，其中包含 Chef `knife` 公用程式信任的證書授權機構 (CA)。

如果您未使用自訂網域，或您的自訂憑證是由作業系統信任的根 CA 簽署的，則可以略過本節。否則，請設定 `knife` 信任您的 Chef Automate 伺服器 SSL 憑證，如以下步驟所述。

1. 執行下列命令。

   ```
   knife ssl check
   ```

   如果結果類似於以下內容，請略過此程序的其餘部分，然後前往 [為 Chef 伺服器新增要管理的節點](opscm-addnodes.md)。

   ```
   Connecting to host my-chef-automate-server.my-corp.com:443
             Successfully verified certificates from 'my-chef-automate-server.my-corp.com'
   ```

   如果出現類似下列的錯誤訊息，請繼續下一個步驟。

   ```
   Connecting to host my-chef-automate-server.my-corp.com:443
             ERROR: The SSL certificate of my-chef-automate-server.my-corp.com could not be verified.
             ...
   ```

1. 執行 `knife ssl fetch` 以信任 AWS OpsWorks for Chef Automate 伺服器的憑證。或者，您可以手動將伺服器的根 CA PEM 格式憑證複製到 `knife ssl check` 輸出中的 `trusted_certs_dir` 值的目錄。根據預設，此目錄位於入門套件中的 `.chef/ca_certs/`。輸出應類似以下內容：

   ```
   WARNING: Certificates from my-chef-automate-server.my-corp.com will be fetched and placed in your trusted_cert
             directory (/Users/username/starterkit/.chef/../.chef/ca_certs).
          
             Knife has no means to verify these are the correct certificates. You should
             verify the authenticity of these certificates after downloading.
          
             Adding certificate for my-chef-automate-server in /Users/users/starterkit/.chef/../.chef/ca_certs/servv-aqtswxu20swzkjgz.crt
             Adding certificate for MyCorp_Root_CA in /Users/users/starterkit/.chef/../.chef/ca_certs/MyCorp_Root_CA.crt
   ```

1. 再次執行 `knife ssl check`。輸出應類似以下內容：

   ```
   Connecting to host my-chef-automate-server.my-corp.com:443
             Successfully verified certificates from 'my-chef-automate-server.my-corp.com'
   ```

   您已準備好使用 `knife` 搭配您的 Chef Automate 伺服器。