Chef - Chef-Shell

编写 Chef Cookbook(食谱)总是很难。由于上传Cookbook(食谱)到 Chef 服务器、配置 vagrant VM、检查Cookbook(食谱)失败的原因、反复检查等反馈周期较长,因此编写Cookbook(食谱)变得更加困难。如果我们可以在一次性完成所有这些繁重工作之前尝试测试一些部分或Cookbook(食谱),那么编写Cookbook(食谱)会更加容易。

Chef 附带 Chef-Shell,它本质上是与 Chef 的交互式 Ruby 会话。在 Chef-Shell 中,我们可以创建 −

  • 属性
  • 编写Cookbook(食谱)
  • 初始化 Chef 运行

它用于在将Cookbook(食谱)上传到 Chef 服务器并在节点上执行完整的Cookbook(食谱)之前,动态评估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 > attributed_mode

步骤 3 − 设置属性值。

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

    • "Chef Cookbook"

  • chef:attributes >退出

    • :attributes

  • chef >

步骤 4 − 切换到Cookbook(食谱)模式。

  • 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) 会话开始,该会话增强了一些特定功能。

  • 它提供诸如 attribute_mode 和 interactive_mode 等模式。

  • 它有助于编写命令,这些命令写在菜谱或烹饪书中。

  • 它以交互模式运行所有内容。

我们可以在三种不同的模式下运行 Chef-Shell:独立模式、客户端模式单独模式

  • 独立模式 − 这是默认模式。未加载任何烹饪书,运行列表为空。

  • 客户端模式 −在这里,chef-shell 充当 chef-client。

  • Solo 模式 − 在这里,chef-shell 充当 chef-solo 客户端。