Chef 简明教程
Chef - Scripts for Data Bags
在某些条件下,无法将服务器置于 Chef 的完全控制之下。在这种情况下,可能需要从脚本中访问 Chef 数据包中的值。为了做到这一点,需要将数据包的值存储在 JSON 文件中,并让添加的脚本访问这些值。
In certain conditions, it is not possible to put the server under the full control of Chef. In such cases, one might need to access values in Chef data bags from scripts. In order to do this, one needs to store data bag values in a JSON file and let the added script access those values.
为此,需要有一个食谱手册。在我们的例子中,我们将像之前一样使用 test_cookbook,并且在其中包含 test_cookbook 定义的节点的运行列表。
For this, one needs to have a cookbook. In our case we would use test_cookbook as earlier and should have the run list of the node including test_cookbook definition in it.
Working Method
Step 1 - 创建一个数据包。
Step 1 − Create a data bag.
vipin@laptop:~/chef-repo $ mkdir data_bags/servers
vipin@laptop:~/chef-repo $ knife data bag create servers
Created data_bag[servers]
Step 2 - 创建一个数据包项。
Step 2 − Create a data bag item.
vipin@laptop:~/chef-repo $ subl data_bags/servers/Storage.json {
"id": "storage",
"host": "10.0.0.12"
}
Step 3 - 更新数据包项。
Step 3 − Update the data bag item.
vipin@laptop:~/chef-repo $ subl data_bags/servers/Storage.json {
"id": "storage",
"host": "10.0.0.12"
}
Using in Cookbook
Step 1 - 需要创建包含数据包值的 JSON 文件,以便使用上面的 cookbook,外部脚本可以访问这些值。
Step 1 − Need to create a JSON file containing data bag values using the above cookbook so that external scripts can access those values.
vipin@laptop:~/chef-repo $ subl cookbooks/test_cookbook/recipes/default.rb
file "/etc/backup_config.json" do
owner "root"
group "root"
mode 0644
content data_bag_item('servers', 'backup')['host'].to_json
end
Step 2 - 将 test_cookbook 上传到 Chef 服务器。
Step 2 − Upload test_cookbook to Chef server.
vipin@laptop:~/chef-repo $ knife cookbook upload test_cookbook
Uploading my_cookbook [0.1.0]
Step 3 − 在节点上运行 Chef 客户端。
Step 3 − Run the Chef client on the node.
user@server:~$ sudo chef-client
...TRUNCATED OUTPUT...
[2013-03-14T20:30:33+00:00] INFO: Processing
file[/etc/backup_config.json] action create
(my_cookbook::default line 9)
[2013-03-14T20:30:34+00:00] INFO: entered create
[2013-03-14T20:30:34+00:00] INFO:
file[/etc/backup_config.json] owner changed to 0
[2013-03-14T20:30:34+00:00] INFO:
file[/etc/backup_config.json] group changed to 0
[2013-03-14T20:30:34+00:00] INFO:
file[/etc/backup_config.json] mode changed to 644
[2013-03-14T20:30:34+00:00] INFO:
file[/etc/backup_config.json] created file
/etc/backup_config.json
...TRUNCATED OUTPUT...
Step 4 - 验证生成 JSON 文件的内容。
Step 4 − Validating the content of the generated JSON file.
user@server:~$ cat /etc/backup_config.json
"10.0.0.12"
Workflow of Scripts
在上面的命令中,我们用来在 /etc 目录内创建 JSON 文件的文件资源是在默认 cookbook 中定义的。它使用 data_bag_item 方法直接从数据包获取文件内容。我们从数据包项访问主机值,并将其转换为 JSON。文件资源使用 JSON 转换的值作为其内容,并将其写入磁盘。
In the above command, the file resource that we have used which creates JSON file inside the /etc directory is defined in the default cookbook. It gets the file content directly from the data bag using the data_bag_item method. We access the host values from the data bag item and convert it to JSON. The file resource uses the JSON-converted values as its content and writes it to the disk.