Cookbook

来源:互联网 发布:河南进出口数据 编辑:程序博客网 时间:2024/05/17 02:37

knife cookbook create mycookbook

knife cookbook upload mycookbook

knife cookbook list

knife cookbook download mycookbook -d /root/chef/chef-repo/cookbooks/

knife node run_list add clientone recipe[mycookbook::myrecipe]

在Chef Client端运行chef-client 命令进行自动化部署

webserver.rb
service 'iptables' do
  action :stop
end
package 'httpd'
service 'httpd' do
    action [:start, :enable]
end
template '/var/www/html/index.html' do
  source 'index.html.erb'
end

index.html.erb
<html>
<body>
<h1>hello from <%= node['fqdn'] %></h1>
</body>
</html>

execute “test command” do
        action :run
command “echo ‘this is test command’>test.log”
        cwd “/root”
end
execute – 是用来在目标机器上执行一条命令
command - 用来执行的命令 
cwd - 当前工作路径 
Action 
  nothing – 由其他的 Resource 触发当前的命令执行 
  :run – 执行当前命令 
name – 当前 execute 的名字,但如果 command 属性没有指定的时候,execute 会用 name 作为命令行执行 

directory – 用来管理目录结构 
directory “/tmp/mydir” do
        owner “root”
        group “root”
        mode “0755”
        action :create
end

package – 用来管理安装包,如,rpm、gem 等。相对应的 Provider 是 Chef::Provider::Package。
yum_package – 用来管理安装的 rpm 包。相应的 Provider 是 Chef::Provider::Package::Yum。
yum_pakcage 是对 package 的一个扩展。具体的语规则法和 package 是基本相同的。
package “gnote” do
action :remove
end
Action
:install – 安装一个包。
:upgrade – 升级一个包。
:remove – 删除一个包。
version – 包的安装版本。
arch – 安装包的 arch,比如,ppc64、x86_64。

yum_repository – 用来对 yum 源进行配置和管理 
yum_repository "ftp-repo " do
name "FTP yum repository - Test"
baseurl "ftp://yourid:password @testrepo.com /redhat/yum/6/server/os/x86_64/"
end
description – Yum 源的描述信息。
baseurl – Yum 源的 url 信息。
Action
:create – 创建 Yum 源的配置文件,并配置 Yum 源。
:delete – 删除 Yum 源。

对配置文件的部署 
这个功能主要由 Cookbook 的 template 和 file 组件完成。这两个功能都可以完成把一个配置文件放到目标机器的指定位置。 
创建一个 helloworld.html 和 helloworld.html.erb 分别放到 files/default 和 template/default 路径下。 
通过 file 完成文件部署的时候使用的 cookbook_file 这个 Resource。后面的的名字就是部署在目标机器上的最终全路径位置。 
把 files 下的 helloworld.html 文件放到目标机器的/var/www/html 路径下 
cookbook_file "/var/www/html/helloworld.html" do
source "helloworld.html"
mode "0644"
end
编写一个.erb 的文件放在 templates/default 下面 
template "/var/www/html/helloworld.html " do
source " helloworld.html.erb"
end

metadata.rb 
depends "apache2"

recipe中
include_recipe "apache2::mod_ssl"

recipe中使用ruby
package_name = "apache2"
package "apache2" do
  case node[:platform]
  when "centos","redhat","fedora","suse"
    package_name "httpd"
  when "debian","ubuntu"
    package_name "apache2"
  when "arch"
    package_name "apache"
  end
  action :install
end
if platform?("debian", "ubuntu")
  # do something if node['platform'] is debian or ubuntu
else
  # do other stuff
end
unless node[:platform_version] == "5.0"
  # do stuff on everything but 5.0
end
["apache2", "apache2-mpm"].each do |p|
  package p
end
{"fog" => "0.6.0", "highline" => "1.6.0"}.each do |g,v|
  gem_package g do
    version v
  end
end

knife node run list add NODENAME "recipe[apache2],recipe[mysql],role[ssh]"

webservers = search(:node, "role:webserver")
template "/tmp/list_of_webservers" do
  source "list_of_webservers.erb"
  variables(:webservers => webservers)
end

file '/tmp/name_of_file' do
  action :create
end
raise "message" if node['platform'] == 'windows'
package 'name_of_package' do
  action :install
end
使用未处理异常,停止chef client运行

ruby_block "name" do
  block do
    # Ruby code with a condition, e.g. if ::File.exist?(::File.join(path, "/tmp"))
    fail "message"  # e.g. "Ordering issue with file path, expected foo"
  end
end
class CustomError < StandardError; end
def custom_error
  raise CustomError, "error message"
end
def custom_error
  fail CustomError, "error message"
end

begin
  dater = data_bag_item(:basket, "flowers")
  rescue Net::HTTPServerException
    # maybe some retry code here?
  raise "message_to_be_raised"
end

Automatic (Ohai)
node['platform'] 
node['platform_version'] 
node['ipaddress'] 
node['macaddress'] 
node['fqdn'] 
node['hostname'] 
node['domain'] 
node['recipes'] 
node['roles'] 
node['ohai_time'] 

Attribute Files
default["apache"]["dir"]          = "/etc/apache2"
default["apache"]["listen_ports"] = [ "80","443" ]
node.default["apache"]["dir"]          = "/etc/apache2"
node.default["apache"]["listen_ports"] = [ "80","443" ]
Using attribute?() in an attributes file:
if attribute?("ec2")
  # ... set stuff related to EC2
end
Using attribute?() in a recipe:
if node.attribute?("ec2")
  # ... do stuff on EC2 nodes
end


0 0
原创粉丝点击