Chef cookbook可以从chef市场下载:supermarket.chef.io
我们将下载一个安装Apache的cookbook。
下载安装Apache的cookbook
cd chef-repo
knife cookbook site download learn_chef_httpd
输出
[root@qikegu chef-repo]# knife cookbook site download learn_chef_httpd
WARN: knife cookbook site download has been deprecated in favor of knife supermarket download. In Chef Infra Client 16 (April 2020) this will result in an error!
Downloading learn_chef_httpd from Supermarket at version 0.2.0 to /root/chef-repo/learn_chef_httpd-0.2.0.tar.gz
ERROR: Error connecting to https://s3.amazonaws.com/community-files.opscode.com/cookbook_versions/tarballs/10385/original/learn_chef_httpd.tgz?1426725483, retry 1/5
Cookbook saved: /root/chef-repo/learn_chef_httpd-0.2.0.tar.gz
提取压缩包
执行
tar -xvf learn_chef_httpd-0.2.0.tar.gz
输出
[root@qikegu chef-repo]# tar -xvf learn_chef_httpd-0.2.0.tar.gz
learn_chef_httpd/
learn_chef_httpd/.kitchen.yml
learn_chef_httpd/Berksfile
learn_chef_httpd/chefignore
learn_chef_httpd/metadata.json
learn_chef_httpd/metadata.rb
learn_chef_httpd/README.md
learn_chef_httpd/recipes/
learn_chef_httpd/templates/
learn_chef_httpd/templates/default/
learn_chef_httpd/templates/default/index.html.erb
learn_chef_httpd/recipes/default.rb
[root@qikegu chef-repo]# cd /root/chef-repo/learn_chef_httpd/recipes
查看 Recipe
这个cookbook下的相关文件多会被自动创建,无需做修改。让我们查看一下recipe目录下的Recipe description。
执行
cd /root/chef-repo/learn_chef_httpd/recipes
cat default.rb
输出
# Cookbook Name:: learn_chef_httpd
# Recipe:: default
#
# Copyright (C) 2014
#
#
#
package 'httpd'
service 'httpd' do
action [:enable, :start]
end
template '/var/www/html/index.html' do
source 'index.html.erb'
end
service 'iptables' do
action :stop
end
附录
创建自己的receipe
我们也可以创建自己的receipe,关于receipe的格式,可参考官方文档。
创建一个receipe文件vim qikegu.rb
:
[root@qikegu chef-repo]# vim qikegu.rb
内容如下,意思是添加一个文件/etc/qikegu
,内容是’Welcome to Chef’
file '/etc/qikegu' do
content 'Welcome to Chef'
end
创建cookbook
可以创建自己的cookbook。
切换到/root/chef-repo/cookbooks
,执行:
chef generate cookbook qikegu_config
输出:
[root@qikegu cookbooks]# chef generate cookbook qikegu_config
+---------------------------------------------+
Chef License Acceptance
Before you can continue, 1 product license
must be accepted. View the license at
https://www.chef.io/end-user-license-agreement/
License that need accepting:
* Chef Development Kit
Do you accept the 1 product license (yes/no)?
> Prompt timed out. Use non-interactive flags or enter an answer within 60 seconds.
If you do not accept this license you will
not be able to use Chef products.
...
查看生成的文件:
[root@qikegu cookbooks]# tree qikegu_config
qikegu_config
├── CHANGELOG.md
├── chefignore
├── kitchen.yml
├── LICENSE
├── metadata.rb
├── Policyfile.rb
├── README.md
├── recipes
│ ├── default.rb
├── spec
│ ├── spec_helper.rb
│ └── unit
│ └── recipes
│ └── default_spec.rb
└── test
└── integration
└── default
└── default_test.rb
可以把创建的receipe文件qikegu.rb
移到该cookbook的recipes目录下:
[root@qikegu cookbooks]# tree qikegu_config
qikegu_config
├── CHANGELOG.md
├── chefignore
├── kitchen.yml
├── LICENSE
├── metadata.rb
├── Policyfile.rb
├── README.md
├── recipes
│ ├── default.rb
│ └── qikegu.rb
├── spec
│ ├── spec_helper.rb
│ └── unit
│ └── recipes
│ └── default_spec.rb
└── test
└── integration
└── default
└── default_test.rb