Chef - Chef-Shell



編寫 Chef cookbook 總是很困難。因為它會使事情變得更困難,因為需要很長的反饋週期才能將它們上傳到 Chef 伺服器,配置一個 vagrant VM,檢查它們在那裡是如何失敗的,然後重複此過程。如果我們能夠在執行所有這些繁重的工作之前嘗試測試一些片段或配方,那將會更容易。

Chef 帶有 Chef-Shell,它本質上是一個帶有 Chef 的互動式 Ruby 會話。在 Chef-Shell 中,我們可以建立 -

  • 屬性
  • 編寫配方
  • 初始化 Chef 執行

它用於在將它們上傳到 Chef 伺服器並執行節點上的完整 cookbook 之前,動態評估配方的一部分。

執行 Shell

步驟 1 - 以獨立模式執行 Chef-Shell。

mma@laptop:~/chef-repo $ chef-shell 
loading configuration: none (standalone chef-shell session) 
Session type: standalone 
Loading...[2017-01-12T20:48:01+01:00] INFO: Run List is [] 
[2017-01-12T20:48:01+01:00] INFO: Run List expands to [] 
done. 
This is chef-shell, the Chef Shell. 
Chef Version: 11.0.0 
http://www.opscode.com/chef 
http://wiki.opscode.com/display/chef/Home 
run `help' for help, `exit' or ^D to quit. 
Ohai2u mma@laptop!  
chef > 

步驟 2 - 在 Chef-Shell 中切換到屬性模式

  • chef > attributes_mode

步驟 3 - 設定屬性值。

  • chef:attributes > set[:title] = "Chef Cookbook"

    • "Chef Cookbook"

  • chef:attributes > quit

    • :attributes

  • chef >

步驟 4 - 切換到配方模式。

  • chef > recipe_mode

步驟 5 - 建立一個檔案資源。

chef:recipe > file "/tmp/book.txt" do 
chef:recipe > content node.title 
chef:recipe ?> end  

=> <file[/tmp/book.txt] @name: "/tmp/book.txt" @noop: nil @ 
before: nil @params: {} @provider: Chef::Provider::File @allowed_ 
actions: [:nothing, :create, :delete, :touch, :create_if_missing] 
@action: "create" @updated: false @updated_by_last_action: false 
@supports: {} @ignore_failure: false @retries: 0 @retry_delay: 
2 @source_line: "(irb#1):1:in `irb_binding'" @elapsed_time: 0 @ 
resource_name: :file @path: "/tmp/book.txt" @backup: 5 @diff: nil 
@cookbook_name: nil @recipe_name: nil @content: "Chef Cookbook">   

chef:recipe > 

步驟 6 - 開始 Chef 執行以建立具有給定內容的檔案。

  • chef:recipe > run_chef

[2017-01-12T21:07:49+01:00] INFO: Processing file[/tmp/book.txt] 
action create ((irb#1) line 1) 
--- /var/folders/1r/_35fx24d0y5g08qs131c33nw0000gn/T/cheftempfile20121212- 
11348-dwp1zs 2012-12-12 21:07:49.000000000 
+0100 
+++ /var/folders/1r/_35fx24d0y5g08qs131c33nw0000gn/T/chefdiff20121212- 
11348-hdzcp1 2012-12-12 21:07:49.000000000 +0100 
@@ -0,0 +1 @@ 
+Chef Cookbook 
\ No newline at end of file 
[2017-01-12T21:07:49+01:00] INFO: entered create 
[2017-01-12T21:07:49+01:00] INFO: file[/tmp/book.txt] created file 
/tmp/book.txt 

工作原理

  • Chef-Shell 從一個互動式 Ruby (IRB) 會話開始,並增強了一些特定功能。

  • 它提供諸如 attributes_mode 和 interactive_mode 之類的模式。

  • 它有助於編寫命令,這些命令寫在配方或 cookbook 中。

  • 它以互動模式執行所有內容。

我們可以以三種不同的模式執行 Chef-Shell:獨立模式、客戶端模式獨立模式

  • 獨立模式 - 這是預設模式。沒有載入 cookbook,執行列表為空。

  • 客戶端模式 - 在這裡,chef-shell 充當 chef-client。

  • 獨立模式 - 在這裡,chef-shell 充當 chef-solo 客戶端。

廣告

© . All rights reserved.