- Chef 教程
- Chef - 首頁
- Chef - 概述
- Chef - 架構
- Chef - 版本控制系統設定
- Chef - 工作站設定
- Chef - 客戶端設定
- Chef - 測試廚房設定
- Chef - Knife 設定
- Chef - Solo 設定
- Chef - Cookbook
- Chef - Cookbook 依賴關係
- Chef - 角色
- Chef - 環境
- Chef - Chef-Client 作為守護程序
- Chef - Chef-Shell
- Chef - 測試 Cookbook
- Chef - Foodcritic
- Chef - ChefSpec
- 使用測試廚房測試 Cookbook
- Chef - 節點
- Chef - Chef-Client 執行
- 高階 Chef
- 動態配置菜譜
- Chef - 模板
- Chef - 使用 Chef DSL 的純 Ruby
- Chef - 使用菜譜的 Ruby Gems
- Chef - 庫
- Chef - 定義
- Chef - 環境變數
- Chef - 資料包
- Chef - 資料包指令碼
- Chef - 跨平臺 Cookbook
- Chef - 資源
- 輕量級資源提供程式
- Chef - 藍圖
- Chef - 檔案和包
- Chef - 社群 Cookbook
- Chef 有用資源
- Chef - 快速指南
- Chef - 有用資源
- Chef - 討論
Chef - 跨平臺 Cookbook
跨平臺 Cookbook 是指那些採用其將要執行的底層環境的 Cookbook。Chef 提供了許多功能,有助於編寫能夠在任何將要部署的作業系統上執行的跨平臺 Cookbook。這有助於開發人員編寫一個完全可操作的 Cookbook。
為了做到這一點,我們需要一個 Cookbook。在我們的例子中,它將是 test_cookbook 和一個執行列表,其中包含 Cookbook 定義。
工作方法
檢索節點平臺詳細資訊並在 Cookbook 中執行條件邏輯取決於平臺。在我們的例子中,我們將針對 Ubuntu 進行測試。
步驟 1 - 如果節點是 Ubuntu,則記錄一條訊息。
vipin@laptop:~/chef-repo $ subl cookbooks/test_cookbook/recipes/default.rb
Log.info("Running on ubuntu") if node.platform['ubuntu']
步驟 2 - 將 Cookbook 上傳到 Chef 伺服器。
vipin@laptop:~/chef-repo $ subl cookbooks/test_cookbook/recipes/default.rb Uploading my_cookbook [0.1.0] Uploaded 1 cookbook.
步驟 3 - 在節點上執行 Chef 客戶端。
user@server:~$ sudo chef-client ...TRUNCATED OUTPUT... [2013-03-03T20:07:39+00:00] INFO: Running on Ubuntu ...TRUNCATED OUTPUT...
或者,如果有人對特定平臺不感興趣,而只需要知道正在使用哪個宣告性平臺,則可以使用以下語句。
Log.info("Running on a debian derivative") if
platform_family?('debian')
上傳修改後的 Cookbook 並執行 Ubuntu 節點上的 Chef 客戶端將顯示以下結果。
[2013-03-03T20:16:14+00:00] INFO: Running on a debian derivative
指令碼工作流程
在上述命令中,Ohai 將發現節點作業系統的當前狀態並將其作為平臺屬性與節點物件一起儲存。
node['platform']
或者,您可以使用方法樣式語法 -
node.platform
設定特定於平臺的值
為了設定特定於平臺的值,chef 提供了便利方法 value_for_platform 和 value_for_platform_family。它們可以用來避免複雜的 case 語句,而使用簡單的雜湊表。
示例 Cookbook
execute "start-runsvdir" do
command value_for_platform(
"debian" => { "default" => "runsvdir-start" },
"ubuntu" => { "default" => "start runsvdir" },
"gentoo" => { "default" => "/etc/init.d/runit-start start" }
)
action :nothing
end
在上面的例子中,命令是根據定義特定於作業系統的。
- 對於 Debian,"runsvdir-start" 將起作用
- 對於 Ubuntu,"start runsvdir" 將起作用
- 對於 Gentoo,"/etc/init.d/runit-start" 將起作用
廣告