根据厨师的环境设置变量

时间:2014-12-22 16:38:12

标签: ruby variables chef nagios nrpe

我在根据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

2 个答案:

答案 0 :(得分:1)

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

provisioner:
  name: chef_solo
  solo_rb:
    environment: vagrant

provisioner:
  name: chef_zero
  client_rb:
    environment: vagrant

虽然您可以从配方代码设置环境名称,但这不会完全应用环境属性或食谱版本限制(尽管后者没有独奏)。

答案 1 :(得分:0)

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