Chef 简明教程

Chef - Files & Packages

在 Chef 中,创建配置文件和移动软件包是关键组件。Chef 有多种方式来管理相同的。Chef 有多种方式来支持处理文件和软件包。

In Chef, creating configuration files and moving packages are the key components. There are multiple ways how Chef manages the same. There are multiple ways how Chef supports in dealing with the files and software packages.

Installing Packages from Third-Party Repo

Step 1 − 编辑食谱的默认食谱。

Step 1 − Edit the default recipe of the cookbook.

vipin@laptop:~/chef-repo $ subl cookbooks/test_cookbook/recipes/default.rb
include_recipe "apt"
apt_repository "s3tools" do
   uri "http://s3tools.org/repo/deb-all"
   components ["stable/"]
   key "http://s3tools.org/repo/deb-all/stable/s3tools.key"
   action :add
end
package "s3cmd"

Step 2 − 编辑元数据以添加对 apt 食谱的依赖性。

Step 2 − Edit the metadata to add dependency on the apt cookbook.

vipin@laptop:~/chef-repo $ subl cookbooks/my_cookbook/metadata.rb
...
depends "apt"

Step 3 – 上传修改后的配方至 Chef 服务器。

Step 3 − Upload the modified cookbook to the Chef server.

Step 4 — 验证尝试安装的软件包尚未安装。

Step 4 − Validate that the package you are trying to install, is not yet installed.

Step 5 — 验证默认仓库。

Step 5 − Validate the default repo.

Step 6 — 在该节点运行 Chef-Client。

Step 6 − Run Chef-Client on the node.

Step 7 — 验证必需的软件包已安装。

Step 7 − Validate that the required package is installed.

Installing Software from Source

如果需要安装不是特定平台的软件包的软件,则需要编译它。在 Chef 中,可以使用脚本资源来执行此操作。

If one needs to install a piece of software that is not available as a package for a given platform, one needs to compile it oneself. In Chef, we can do this by using the script resource.

Step 1 — 编辑默认配方。

Step 1 − Edit the default recipe.

vipin@laptop:~/chef-repo $ subl cookbooks/my_cookbook/recipes/
default.rb
version = "1.3.9"
bash "install_nginx_from_source" do
   cwd Chef::Config['file_cache_path']
   code ≪-EOH
      wget http://nginx.org/download/nginx-#{version}.tar.gz
      tar zxf nginx-#{version}.tar.gz &&
      cd nginx-#{version} &&
      ./configure && make && make install
   EOH

Step 2 − 将修改后的代码提交到 Chef 服务器。

Step 2 − Upload the modified cookbook to the Chef server.

Step 3 — 在该节点运行 Chef-Client。

Step 3 − Run the Chef-Client on the node.

Step 4 — 验证是否安装了 nginx。

Step 4 − Validate that the nginx is installed.