2014-12-22 56 views
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 
+0

你有什麼看起來沒錯。也許驗證你在任何列出的環境中? –

+0

我已經縮小範圍來測試廚房沒有在流浪箱上設置環境。 – arukaen

回答

0

所以最終的問題是,測試廚房沒有配置爲node.chef_environment as "vagrant"。一旦我特別設置了食譜中的node.chef_environment,我就得出了這個結論。

1

要通過測試廚房設置你的環境中,這種添加到您的.kitchen.yml

provisioner: 
    name: chef_solo 
    solo_rb: 
    environment: vagrant 

provisioner: 
    name: chef_zero 
    client_rb: 
    environment: vagrant 

雖然你可以設置從配方代碼環境的名稱,這將不是完全應用環境屬性或食譜版本約束(儘管後者與獨奏沒有關係)。