在这节中,我们将会分享如何在Chef的客户端通过chef-zero里运行cookbooks里面的配方。下面看具体的步骤,

@创建下面的文件结构

.chef

cookbooks

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>


如果您觉得文章对您有帮助,请打赏支持本人的写作热情。

             @微信                                                                  @支付宝

             



Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐