Chef 的配置文件主要有几种类型,取决于你是写 Cookbook(菜谱)、节点属性,还是 Chef Infra 客户端(client.rb) 等。下面按常见场景说明,并给示例。
| 类型 | 作用 |
|---|---|
client.rb |
Chef Infra Client 的配置 |
metadata.rb |
Cookbook 的元信息 |
recipes/default.rb |
执行逻辑(最重要) |
attributes/default.rb |
设置节点属性 |
templates |
配置文件模板 |
files |
静态文件 |
通常位于 /etc/chef/client.rb
log_level :info
log_location STDOUT
chef_server_url "https://chef.example.com/organizations/myorg"
node_name "web01"
client_key "/etc/chef/web01.pem"
ssl_verify_mode :verify_peer
chef_server_url:Chef Server 地址node_name:节点名client_key:节点密钥log_level:日志级别(:info, :debug)my_cookbook/
├── metadata.rb
├── recipes/
│ └── default.rb
├── attributes/
│ └── default.rb
├── templates/
│ └── default/
│ └── nginx.conf.erb
└── files/
name 'my_cookbook'
version '1.0.0'
maintainer 'Chef User'
license 'Apache-2.0'
description 'Install and configure nginx'
depends 'nginx'
package 'nginx' do
action :install
end
service 'nginx' do
action [:enable, :start]
end
template '/etc/nginx/nginx.conf' do
source 'nginx.conf.erb'
owner 'root'
group 'root'
mode '0644'
notifies :reload, 'service[nginx]'
end
default['nginx']['port'] = 80
default['nginx']['user'] = 'www-data'
在 recipe 中使用:
node['nginx']['port']
templates/default/nginx.conf.erb
user <%= node['nginx']['user'] %>;
worker_processes auto;
events {
worker_connections 1024;
}
http {
server {
listen <%= node['nginx']['port'] %>;
server_name localhost;
}
}
# recipes/default.rb
package 'nginx'
service 'nginx' do
action [:enable, :start]
end
# 本地检查语法
chef-client --local-mode --why-run
如果你有 具体场景(如:写 systemd、写数据库配置、多环境差异配置、Windows Chef),我可以给你更贴合的示例。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。