- Chef 教程
- Chef - 首頁
- Chef - 概述
- Chef - 架構
- Chef - 版本控制系統設定
- Chef - 工作站設定
- Chef - 客戶端設定
- Chef - Test Kitchen 設定
- Chef - Knife 設定
- Chef - Solo 設定
- Chef - Cookbook
- Chef - Cookbook 依賴關係
- Chef - 角色
- Chef - 環境
- Chef - Chef-Client 作為守護程序
- Chef - Chef-Shell
- Chef - 測試 Cookbook
- Chef - Foodcritic
- Chef - ChefSpec
- 使用 Test Kitchen 測試 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 - 輕量級資源提供程式
輕量級資源提供程式 (LWRP) 提供了一種擴充套件可用資源列表的選項,透過擴充套件其功能,並允許 Chef 使用者建立自定義資源。
透過建立自定義資源,可以更簡單地編寫 Cookbook,因為可以使用 Chef DSL 建立豐富的自定義資源,這有助於使配方程式碼更具表現力。
在 Chef 社群中,許多自定義資源都是使用 LWRP 實現的。有很多 LWRP 的實際示例,例如 iptables_rules 和 apt_repository。
工作方法
確保擁有名為 Testing_resource 的 Cookbook 和包含 Testing_resource Cookbook 的節點執行列表。
構建 LWRP
步驟 1 - 在 Testing_resource Cookbook 中建立一個自定義資源。
vipin@laptop:~/chef-repo $ subl cookbooks/Testing_resource/resources/default.rb actions :create, :remove attribute :title, kind_of: String, default: "World" attribute :path, kind_of: String, default: "/tmp/greeting.txt"
步驟 2 - 為 Tesing_resource Cookbook 中的資源建立一個提供程式。
vipin@laptop:~/chef-repo $ subl cookbooks/Testing_resource/provider/default.rb
action :create do
log "Adding '#{new_resource.name}' greeting as #{new_resource.
path}"
file new_resource.path do
content "#{new_resource.name}, #{new_resource.title}!"
action :create
end
action :remove do
Chef::Log.info "Removing '#{new_resource.name}' greeting #{new_resource.path}"
file new_resource.path do
action :delete
end
end
步驟 3 - 透過編輯 Testing_resource 預設配方來使用新的資源。
vipin@laptop:~/chef-repo $ subl cookbooks/Tesing_resource/recipes/default.rb greeting "Ohai" do title "Chef" action :create end
步驟 4 - 將修改後的 Cookbook 上傳到 Chef 伺服器。
vipin@laptop:~/chef-repo $ knife cookbook upload greeting Uploading greeting [0.1.0]
步驟 5 - 在節點上執行 Chef-Client。
vipin@server:~$ sudo chef-client ...TRUNCATED OUTPUT... 2013-06-28T21:32:54+00:00] INFO: Processing greeting[Ohai] action create (greeting::default line 9) [2013-06-28T21:32:54+00:00] INFO: Adding 'Ohai' greeting as /tmp/ greeting.txt [2013-06-28T21:32:54+00:00] INFO: Processing file[/tmp/greeting. txt] action create (/srv/chef/file_store/cookbooks/greeting/ providers/default.rb line 7) [2013-06-28T21:32:54+00:00] INFO: entered create [2013-06-28T21:32:54+00:00] INFO: file[/tmp/greeting.txt] created file /tmp/greeting.txt ...TRUNCATED OUTPUT...
步驟 6 - 驗證生成的文件內容。
user@server:~$ cat /tmp/greeting.txt Ohai, Chef!
工作流程指令碼
LWRP 存在於 Cookbook 中。自定義資源位於 Cookbook 內部,將在 Cookbook 名稱下可用。在工作流程中,首先我們定義定義,然後將屬性傳遞給將在 Cookbook 中使用的資源。最後,我們在配方中使用這些操作和屬性。
廣告