如何更改厨师执行订单?

时间:2018-05-21 19:50:21

标签: chef chef-recipe

我正在运行一个厨师食谱,但它似乎无法正常运行。

#
# Cookbook:: monit
# Recipe:: default
#
# Copyright:: 2017, The Authors, All Rights Reserved.


yum_package 'install Requirements' do
    package_name ['libtool', 'automake', 'autoconf', 'bison', 'flex', 'zlib-devel', 'git']
    action :install
end

script 'install monit' do
    interpreter "bash"
    code <<-EOH
        git clone https://tildeslash@bitbucket.org/tildeslash/monit.git monit
        cd monit/
        ./bootstrap
        ./configure --without-pam --without-ssl --sysconfdir=/etc --prefix=/usr
        make && make install
    EOH
end

iptables_rule 'monit_web' do
    action :enable
end

template '/etc/monitrc' do
    owner 'root'
    mode '0700'
    source 'monitrc.erb'
end

execute 'monit reload' do
    require 'mixlib/shellout'
    cmnd = Mixlib::ShellOut.new('monit start all')
    cmnd.run_command
    if cmnd.error?
        cmndd = Mixlib::ShellOut.new('monit reload')
        cmndd.run_command
    else
        puts cmnd.stdout
    end
end

script 'start service' do
    interpreter "bash"
    code <<-EOF
        monit
        monit start all
    EOF
end

根据我的理解,脚本资源'install monit'应该在执行资源'monit reload'之前运行。情况似乎并非如此;我收到以下错误:

================================================================================
Recipe Compile Error in /var/chef/cache/cookbooks/monit/recipes/default.rb
================================================================================

Errno::ENOENT
-------------
No such file or directory - monit

Cookbook Trace:
---------------
  /var/chef/cache/cookbooks/monit/recipes/default.rb:38:in `block in from_file'
  /var/chef/cache/cookbooks/monit/recipes/default.rb:35:in `from_file'

Relevant File Content:
----------------------
/var/chef/cache/cookbooks/monit/recipes/default.rb:

 31:    mode '0700'
 32:    source 'monitrc.erb'
 33:  end
 34:
 35:  execute 'monit reload' do
 36:    require 'mixlib/shellout'
 37:    cmnd = Mixlib::ShellOut.new('monit start all')
 38>>   cmnd.run_command
 39:    if cmnd.error?
 40:            cmndd = Mixlib::ShellOut.new('monit reload')
 41:            cmndd.run_command
 42:    else
 43:            puts cmnd.stdout
 44:    end
 45:  end
 46:
 47:  script 'start service' do

System Info:
------------
chef_version=13.9.1
platform=centos
platform_version=7.5.1804
ruby=ruby 2.4.3p205 (2017-12-14 revision 61247) [x86_64-linux]
program_name=chef-client worker: ppid=14633;start=14:44:25;
executable=/opt/chef/bin/chef-client

我已确认未安装程序'monit'。为什么Chef会跳过脚本并跳转执行?这一点工作正常,我不知道它为什么突然停止工作。

2 个答案:

答案 0 :(得分:2)

您应该熟悉Chef Client Overview。特别是编译资源收集阶段汇聚节点阶段的顺序。

作为一般规则;汇聚期间运行资源,但编译期间不在ruby_block资源中的任何ruby代码运行。因此,在这种情况下,monit reload的ruby代码实际上是在编译阶段运行的。您需要将该资源修改为ruby_block。像这样:

ruby_block 'monit reload' do
  block do
    require 'mixlib/shellout'
    cmnd = Mixlib::ShellOut.new('monit start all')
    cmnd.run_command
    if cmnd.error?
        cmndd = Mixlib::ShellOut.new('monit reload')
        cmndd.run_command
    else
        puts cmnd.stdout
    end
  end
  action :run
end

答案 1 :(得分:0)

阅读coderanger.net/two-pass但基本上你只是在执行资源中隐藏了一些编译时的Ruby代码。这不起作用。您可以正常使用资源或使用ruby_block。