如何使用 CHEF 在配方中複製適當的設定檔?

如何使用 CHEF 在配方中複製適當的設定檔?

我開始學習如何recipes發展chef。我需要Ganglia Monitor在一些伺服器上安裝(或nodes在神經節文獻中)。這就是為什麼我要檢查平台是否ubuntu以及centOS其他許多平台來安裝正確的軟體包。

問題是我有兩個不同的.config文件,實際上該文件中只有一兩個參數.config彼此不同。我需要幫助如何偵測datacenter該文件server所屬的位置,以便我可以複製正確的.config文件。到目前為止,我能夠在下面開發這個腳本,但我有一些疑問,這些疑問在程式碼的註解中。

#
# Cookbook Name:: ganglia
# Recipe:: default
#
# Copyright 2013, Valter Henrique.com
#
# All rights reserved - Do Not Redistribute
#
# Installing Ganglia Monitor

case node[:platform]
  when "ubuntu", "debian"
    package "ganglia-monitor"
  when "redhat", "centos", "fedora"
    package "ganglia-gmond"
  end
  user "ganglia"
end

# Setting different .config files
case ipaddress
# DataCenter #1
# how put more options in the when condition ? A when for /^200.222./ or /^200.223./ ?
    when /^200.222./ 
        # putting config file
        cookbook_file "/etc/ganglia/gmond.conf" do
            owner "root"
            group "root"
            mode "0644"
            source "dc1/gmond.conf"
            notifies(:restart, "service[gmond]")
        end
    #DataCenter #2
    when /^216.235./
        cookbook_file "/etc/ganglia/gmond.conf" do
            owner "root"
            group "root"
            mode "0644"
            source "dc2/gmond.conf"
            notifies(:restart, "service[gmond]")
        end
  end

關於如何以更好的方式開發此程式碼有什麼建議嗎?

答案1

source您可以在資源的屬性中使用變數cookbook_file以避免程式碼重複。

dc = case ipaddress
     when /^200\.222\./
       'dc1'
     when /^216\.235\./
       'dc2'
     end

cookbook_file "/etc/ganglia/gmond.conf" do
   owner "root"
   group "root"
   mode "0644"
   source "#{dc}/gmond.conf"
   notifies(:restart, "service[gmond]")
end

相關內容