[16] DevOps 自动化运维工具Chef----如何在本地运行cookbooks里面的配方?
2017-06-21 00:05
961 查看
在这节中,我们将会分享如何在Chef的客户端通过chef-zero里运行cookbooks里面的配方。下面看具体的步骤,
cookbooks
test.json
具体目录如下:
其中,knife.rb的内容如下:
current_dir = File.dirname(__FILE__)
log_level :info
log_location STDOUT
#chef_server_url 'http://127.0.0.1:8889'
node_name 'dsc-5'
#cookbook_path ["#{current_dir}/../cookbooks"]
client_key "#{current_dir}/test.pem"
其中metadata.rb如下:
name 'starter'
description 'A basic starter cookbook'
version '1.0.0'
maintainer 'Awesome Company, Inc.'
maintainer_email 'you@example.com'
其只要是cookbooks的说明
recipes里面包含一个default.rb
其内容如下:
# This is a Chef recipe file. It can be used to specify resources which will
# apply configuration to a server.
log "Welcome to Chef1111, #{node["starter_name"]}!" do
level :info
end
dsc_script 'BackupUser' do
code <<-EOH
$user = 'backup33'
$password = ConvertTo-SecureString -String "YourPass$(random)" -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user, $password
User $user
{
UserName = $user
Password = $cred
Description = 'Backup operator'
Ensure = "Present"
Disabled = $false
PasswordNeverExpires = $true
PasswordChangeRequired = $false
}
EOH
configuration_data <<-EOH
@{
AllNodes = @(
@{
NodeName = "localhost";
PSDscAllowPlainTextPassword = $true
})
}
EOH
end
# For more information, see the documentation: https://docs.chef.io/essentials_cookbook_recipes.html
{
"run_list": [ "recipe[starter]" ]
}
上面会调用C:\chef\chef-repo\cookbooks\starter\recipes下默认的default.rb; 如果还有其他的rb文件,可以在starter后面加上::然后写上rb的文件名。
比如,{
"run_list": [ "recipe[starter:another]" ]
}
C:\chef\chef-repo>chef-client --local-mode -j .\test.json
[2017-06-20T23:45:20+08:00] INFO: Auto-discovered chef repository at C:/chef/che
f-repo
[2017-06-20T23:45:21+08:00] INFO: Started chef-zero at chefzero://localhost:8889
with repository at C:/chef/chef-repo
One version per cookbook
Starting Chef Client, version 12.13.37
[2017-06-20T23:45:25+08:00] INFO: *** Chef 12.13.37 ***
[2017-06-20T23:45:25+08:00] INFO: Platform: i386-mingw32
[2017-06-20T23:45:25+08:00] INFO: Chef-client pid: 5984
[2017-06-20T23:45:36+08:00] INFO: HTTP Request Returned 404 Not Found: Object no
t found: chefzero://localhost:8889/nodes/dsc-5
[2017-06-20T23:45:36+08:00] INFO: Setting the run_list to ["recipe[starter]"] fr
om CLI options
[2017-06-20T23:45:36+08:00] INFO: Run List is [recipe[starter]]
[2017-06-20T23:45:36+08:00] INFO: Run List expands to [starter]
[2017-06-20T23:45:36+08:00] INFO: Starting Chef Run for dsc-5
[2017-06-20T23:45:36+08:00] INFO: Running start handlers
[2017-06-20T23:45:36+08:00] INFO: Start handlers complete.
[2017-06-20T23:45:36+08:00] INFO: HTTP Request Returned 404 Not Found: Object no
t found:
resolving cookbooks for run list: ["starter"]
[2017-06-20T23:45:36+08:00] INFO: Loading cookbooks [starter@1.0.0]
Synchronizing Cookbooks:
[2017-06-20T23:45:36+08:00] INFO: Storing updated cookbooks/starter/recipes/defa
ult.rb in the cache.
[2017-06-20T23:45:36+08:00] INFO: Storing updated cookbooks/starter/metadata.rb
in the cache.
- starter (1.0.0)
Installing Cookbook Gems:
Compiling Cookbooks...
Converging 2 resources
Recipe: starter::default
* log[Welcome to Chef1111, !] action write[2017-06-20T23:45:36+08:00] INFO: Pr
ocessing log[Welcome to Chef1111, !] action write (starter::default line 4)
[2017-06-20T23:45:36+08:00] INFO: Welcome to Chef1111, !
* dsc_script[BackupUser] action run[2017-06-20T23:45:36+08:00] INFO: Processin
g dsc_script[BackupUser] action run (starter::default line 8)
[2017-06-20T23:45:38+08:00] WARN: Could not parse LCM output: Could not parse:
WARNING: Whatif is deprecated in this cmdlet. Please remove Whatif and try
again.
[2017-06-20T23:45:50+08:00] INFO: DSC resource configuration completed successfu
lly
- converge DSC configuration 'BackupUser'
- converge DSC resource Unknown DSC Resources by
[2017-06-20T23:45:50+08:00] INFO: Chef Run complete in 14.448 seconds
Running handlers:
[2017-06-20T23:45:50+08:00] INFO: Running report handlers
Running handlers complete
[2017-06-20T23:45:50+08:00] INFO: Report handlers complete
Chef Client finished, 2/2 resources updated in 29 seconds
C:\chef\chef-repo>
如果您觉得文章对您有帮助,请打赏支持本人的写作热情。
@微信 @支付宝
@创建下面的文件结构
.chefcookbooks
test.json
@ .chef目录下指定访问Chef-zero服务器的地址和私钥
.chef目录的下面的后缀名为.pem的私钥可以为任何格式正确的私钥文件具体目录如下:
其中,knife.rb的内容如下:
current_dir = File.dirname(__FILE__)
log_level :info
log_location STDOUT
#chef_server_url 'http://127.0.0.1:8889'
node_name 'dsc-5'
#cookbook_path ["#{current_dir}/../cookbooks"]
client_key "#{current_dir}/test.pem"
@ cookbooks目录下面的内容
其中metadata.rb如下:
name 'starter'
description 'A basic starter cookbook'
version '1.0.0'
maintainer 'Awesome Company, Inc.'
maintainer_email 'you@example.com'
其只要是cookbooks的说明
recipes里面包含一个default.rb
其内容如下:
# This is a Chef recipe file. It can be used to specify resources which will
# apply configuration to a server.
log "Welcome to Chef1111, #{node["starter_name"]}!" do
level :info
end
dsc_script 'BackupUser' do
code <<-EOH
$user = 'backup33'
$password = ConvertTo-SecureString -String "YourPass$(random)" -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user, $password
User $user
{
UserName = $user
Password = $cred
Description = 'Backup operator'
Ensure = "Present"
Disabled = $false
PasswordNeverExpires = $true
PasswordChangeRequired = $false
}
EOH
configuration_data <<-EOH
@{
AllNodes = @(
@{
NodeName = "localhost";
PSDscAllowPlainTextPassword = $true
})
}
EOH
end
# For more information, see the documentation: https://docs.chef.io/essentials_cookbook_recipes.html
@test.json的内容
在test.json里面可以制定chef client运行的任务清单,比如{
"run_list": [ "recipe[starter]" ]
}
上面会调用C:\chef\chef-repo\cookbooks\starter\recipes下默认的default.rb; 如果还有其他的rb文件,可以在starter后面加上::然后写上rb的文件名。
比如,{
"run_list": [ "recipe[starter:another]" ]
}
@运行命令
切换到c:\chef\chef-repo 目录,然后运行C:\chef\chef-repo>chef-client --local-mode -j .\test.json@运行结果
运行结果如下:C:\chef\chef-repo>chef-client --local-mode -j .\test.json
[2017-06-20T23:45:20+08:00] INFO: Auto-discovered chef repository at C:/chef/che
f-repo
[2017-06-20T23:45:21+08:00] INFO: Started chef-zero at chefzero://localhost:8889
with repository at C:/chef/chef-repo
One version per cookbook
Starting Chef Client, version 12.13.37
[2017-06-20T23:45:25+08:00] INFO: *** Chef 12.13.37 ***
[2017-06-20T23:45:25+08:00] INFO: Platform: i386-mingw32
[2017-06-20T23:45:25+08:00] INFO: Chef-client pid: 5984
[2017-06-20T23:45:36+08:00] INFO: HTTP Request Returned 404 Not Found: Object no
t found: chefzero://localhost:8889/nodes/dsc-5
[2017-06-20T23:45:36+08:00] INFO: Setting the run_list to ["recipe[starter]"] fr
om CLI options
[2017-06-20T23:45:36+08:00] INFO: Run List is [recipe[starter]]
[2017-06-20T23:45:36+08:00] INFO: Run List expands to [starter]
[2017-06-20T23:45:36+08:00] INFO: Starting Chef Run for dsc-5
[2017-06-20T23:45:36+08:00] INFO: Running start handlers
[2017-06-20T23:45:36+08:00] INFO: Start handlers complete.
[2017-06-20T23:45:36+08:00] INFO: HTTP Request Returned 404 Not Found: Object no
t found:
resolving cookbooks for run list: ["starter"]
[2017-06-20T23:45:36+08:00] INFO: Loading cookbooks [starter@1.0.0]
Synchronizing Cookbooks:
[2017-06-20T23:45:36+08:00] INFO: Storing updated cookbooks/starter/recipes/defa
ult.rb in the cache.
[2017-06-20T23:45:36+08:00] INFO: Storing updated cookbooks/starter/metadata.rb
in the cache.
- starter (1.0.0)
Installing Cookbook Gems:
Compiling Cookbooks...
Converging 2 resources
Recipe: starter::default
* log[Welcome to Chef1111, !] action write[2017-06-20T23:45:36+08:00] INFO: Pr
ocessing log[Welcome to Chef1111, !] action write (starter::default line 4)
[2017-06-20T23:45:36+08:00] INFO: Welcome to Chef1111, !
* dsc_script[BackupUser] action run[2017-06-20T23:45:36+08:00] INFO: Processin
g dsc_script[BackupUser] action run (starter::default line 8)
[2017-06-20T23:45:38+08:00] WARN: Could not parse LCM output: Could not parse:
WARNING: Whatif is deprecated in this cmdlet. Please remove Whatif and try
again.
[2017-06-20T23:45:50+08:00] INFO: DSC resource configuration completed successfu
lly
- converge DSC configuration 'BackupUser'
- converge DSC resource Unknown DSC Resources by
[2017-06-20T23:45:50+08:00] INFO: Chef Run complete in 14.448 seconds
Running handlers:
[2017-06-20T23:45:50+08:00] INFO: Running report handlers
Running handlers complete
[2017-06-20T23:45:50+08:00] INFO: Report handlers complete
Chef Client finished, 2/2 resources updated in 29 seconds
C:\chef\chef-repo>
如果您觉得文章对您有帮助,请打赏支持本人的写作热情。
@微信 @支付宝
相关文章推荐
- [13] DevOps 自动化运维工具Chef----用chef-apply命令单机运行Chef配方(Receipt)
- [7]DevOps 自动化运维工具Chef---如何安装Chef的Report?
- [6]DevOps 自动化运维工具Chef---如何通过Chef WorkStation在Window 7客户机节点上远程自动安装最新的Chef Client?
- [8]DevOps 自动化运维工具Chef---如何查看Chef 自身的开发的API的文档?
- [9]DevOps 自动化运维工具Chef---如何解决在Window平台上执行一些knife子命令时所报的错误, knife search, knife status?
- [14] DevOps 自动化运维工具Chef----如何用Chef调用PowerShell DSC的Resource
- [12] DevOps 自动化运维工具Chef----如何在Window机器上用pgadmin打开数据库?
- [11] DevOps 自动化运维工具Chef----如何获得Chef数据用户的用户名和密码?
- [1] DevOps 自动化运维工具Chef----入门
- [5]DevOps 自动化运维工具Chef---Unbuntu系统下,Chef Client 安装位置介绍
- [2] DevOps 自动化运维工具Chef----在Ubuntu上安装最新的Chef server core 12.1
- [15] DevOps 自动化运维工具Chef----chef-apply, chef-client,chef-zero,chef-solo区别
- [3] DevOps 自动化运维工具Chef----在Ubuntu上安装最新的Chef WorkStation 12.13.37
- [10]DevOps 自动化运维工具Chef----- Chef 12 服务器端数据库解密
- [4] DevOps 自动化运维工具Chef----在Ubuntu上安装最新的Chef Client 12.13.37 并和Chef server关联起来
- 使用Chef管理windows集群 | 运维自动化工具
- 使用Chef管理windows集群 | 运维自动化工具
- 自动化运维工具安装部署 chef (九)- 重温安装
- DevOps落地三部曲:如何归责?用啥工具?往哪里去? – 运维派