1

我无法根据 node.chef_environment 的结果在模板上设置变量。当前,当我运行厨房收敛时,厨师在重新启动 nrpe 服务时运行错误,因为 NRPE 抱怨 allowed_hosts 为空白。这是我第一次写食谱,所以请原谅我所做的任何丑陋的事情。

if node.chef_environment == "development" || node.chef_environment == "qa" || node.chef_environment == "vagrant"
        node.default['allowed_hosts'] = "ipaddr"
elsif node.chef_environment == "staging" || node.chef_environment  == "production"
        node.default['allowed_hosts'] = "ipaddr2"
end

case
when platform_family?("debian")
    package "nagios-nrpe-server"
    package "nagios-plugins-basic"
when platform_family?("rhel")
    package "nagios-nrpe"
    package "nagios-plugins-nrpe"
    package "net-snmp-utils"
else
    Chef::Application.fatal! "[nagios-nrpe client] unsupported platform family: #{node[:platform_family]}"
end

template "/etc/nagios/nrpe.cfg" do
    source "nrpe.cfg.erb"
    owner "root"
    group "root"
    mode "0644"
    variables( 
        :allowed_hosts => node.default['allowed_hosts'] 
    )
end

bash "wget" do
    code <<-EOH
    cd /usr/lib/nagios/
    wget --no-check-certificate remote_source
    EOH
end

directory "/usr/lib/nagios/plugins" do
    action :delete
    recursive true
end

execute "untar plugins" do
    cwd "/usr/lib/nagios/"
    command "tar zxvf cc_sys_nrpe.tar.gz"
end

directory "/usr/lib/nagios/plugins" do
    mode "777"
    recursive true
end

file "/usr/lib/nagios/cc_sys_nrpe.tar.gz" do
    action :delete
end

service "nagios-nrpe-server" do
    supports :status => true, :restart => true, :reload => true
    action :restart
end
4

2 回答 2

1

要通过测试厨房设置您的环境,请将其添加到您的.kitchen.yml

provisioner:
  name: chef_solo
  solo_rb:
    environment: vagrant

或者

provisioner:
  name: chef_zero
  client_rb:
    environment: vagrant

虽然您可以从配方代码中设置环境名称,但这不会完全应用环境属性或说明书版本约束(尽管后者与单独没有实际意义)。

于 2014-12-22T20:20:01.727 回答
0

所以问题最终是 test-kitchen 没有配置为 set node.chef_environment as "vagrant"。一旦我在食谱中专门设置了 node.chef_environment,我就得出了这个结论。

于 2014-12-22T18:03:36.490 回答