Chef 简明教程
Chef - Cross-Platform for Cookbooks
跨平台食谱是指采用其运行基础环境的食谱。Chef 提供了许多功能,有助于编写能够在将要部署的任何操作系统上运行的跨平台食谱。这有助于开发人员编写完全可操作的食谱。
Cross-Platform cookbooks are those cookbooks which adopt an underlying environment on which it is going to run. Chef provides a host of features, which helps in writing crossplatform cookbooks capable of running on any OS, on which it is going to get deployed. This helps a developer to write a completely operational cookbook.
为了执行此操作,我们需要一份食谱。在我们的示例中,它将是 test_cookbook 和一个包含食谱定义的运行列表。
In order to do this, we need to have a cookbook. In our case it will be test_cookbook and a run list which will have the cookbook definition in it.
Working Method
检索节点平台详细信息并在我们的食谱中执行条件逻辑取决于平台。在我们的示例中,我们将在 Ubuntu 上对此进行测试。
Retrieving the nodes platform detail and executing the conditional logic in our cookbook depends on the platform. In our case, we will test it for Ubuntu.
Step 1 − 如果节点为 Ubuntu,则记录一条消息。
Step 1 − Log a message if the node is Ubuntu.
vipin@laptop:~/chef-repo $ subl cookbooks/test_cookbook/recipes/default.rb
Log.info("Running on ubuntu") if node.platform['ubuntu']
Step 2 − 将食谱上传到 Chef 服务器。
Step 2 − Upload the cookbook to Chef server.
vipin@laptop:~/chef-repo $ subl cookbooks/test_cookbook/recipes/default.rb
Uploading my_cookbook [0.1.0]
Uploaded 1 cookbook.
Step 3 − 在节点上运行 Chef 客户端。
Step 3 − Run the Chef client on the node.
user@server:~$ sudo chef-client
...TRUNCATED OUTPUT...
[2013-03-03T20:07:39+00:00] INFO: Running on Ubuntu
...TRUNCATED OUTPUT...
如果无需关注特定平台,而只需知道正在使用哪种声明式平台,则可以使用以下语句。
Alternatively, if one is not interested in a specific platform but only needs to know which declarative one is using, the following statement can be used.
Log.info("Running on a debian derivative") if
platform_family?('debian')
在经过修改的食谱上传到 Chef 服务器并在 Ubuntu 节点上运行 Chef 客户端后,将显示以下结果。
Uploading the modified cookbook and running Chef client on Ubuntu node will show the following result.
[2013-03-03T20:16:14+00:00] INFO: Running on a debian
derivative
Workflow of Scripts
在以上命令中,Ohai 将发现节点操作系统的当前状态并将其作为平台属性存储在节点对象中。
In the above command, Ohai will discover the current status of the node’s operating system and store it as a platform attribute with the node object.
node['platform']
或者,您可以使用 -method 样式语法−
Or, you can use method style syntax −
node.platform
Setting Platform Specific Values
为了设定平台特定值,Chef 提供了便捷方法 value_for_platform 和 value_for_platform_family。可以使用它们来避免复杂的 case 语句,而使用简单的散列。
In order to set platform specific values chef offers convenience methods value_for_platform and value_for_platform_family. They can be used to avoid complex case statement and use a simple hash instead.
Example 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
在以上示例中,命令被定义为操作系统特定。
In the above example, the command is OS specific as defined.
-
For Debian, "runsvdir-start" will work
-
For Ubuntu, "start runsvdir" will work
-
For Gentoo, "/etc/init.d/runit-start" will work