Chef 简明教程
Chef - Chef-Shell
编写 Chef 烹饪书总是困难的。它让事情变得更加困难,因为它上传到 Chef 服务器、配置流浪 VM、检查它们在那里如何失败、冲洗和重复的漫长反馈周期。如果我们可以在一次完成所有这些繁重的工作之前尝试测试一些碎片或配方,那会更容易。
Writing Chef cookbooks is always hard. It makes it even harder because of long feedback cycle of uploading them to the Chef server, provisioning a vagrant VM, checking how they failed there, rinsing and repeating. It would be easier if we could try to test some pieces or recipes before we do all this heavy lifting at once.
Chef 带有 Chef-Shell,它本质上是 Chef 的交互式 Ruby 会话。在 Chef-Shell 中,我们可以创建 −
Chef comes with Chef-Shell, which is essentially an interactive Ruby session with Chef. In the Chef-Shell, we can create −
-
Attributes
-
Write Recipes
-
Initializing Chef runs
它用于即时评估食谱的各个部分,然后再将其上传到 Chef 服务器并在节点上执行完成的烹饪书。
It is used to evaluate parts of recipes on the fly, before uploading them to Chef server and execute complete cookbooks on the node.
Running Shell
Step 1 − 在独立模式下运行 Chef-Shell。
Step 1 − Run Chef-Shell in a standalone mode.
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 >
Step 2 − 在 Chef-Shell 中切换到属性模式
Step 2 − Switch to attribute mode in the Chef-Shell
-
chef > attributes_mode
Step 3 − 设置属性值。
Step 3 − Setting attribute value.
-
chef:attributes > set[:title] = "Chef Cookbook" "Chef Cookbook"
-
chef:attributes > quit :attributes
-
chef >
Step 4 − 切换到菜谱模式。
Step 4 − Switch to recipe mode.
-
chef > recipe_mode
Step 5 − 创建文件资源。
Step 5 − Create a file resource.
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 >
Step 6 − 开始 Chef 运行,以使用给定的内容创建文件。
Step 6 − Commence Chef run to create the file with the given content.
-
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
How it Works
-
Chef-Shell starts with an Interactive Ruby (IRB) session enhanced with some specific features.
-
It offers modes such as attributes_mode and interactive_mode.
-
It helps in writing commands, which are written inside a recipe or cookbook.
-
It runs everything in an interactive mode.
我们可以在三种不同模式下运行 Chef-Shell: Standalone mode, Client mode 和 Solo mode 。
We can run Chef-Shell in three different modes: Standalone mode, Client mode, and Solo mode.
-
Standalone mode − It is the default mode. No cookbooks are loaded, and the run-list is empty.
-
Client mode − Here, the chef-shell acts as a chef-client.
-
Solo mode − Here, the chef-shell acts as a chef-solo client.