2017-06-21 67 views
0

新廚師用戶...如何處理Chef中服務器組的不同模板?

我想推出一個Sumologic安裝配置文件。我有一個收集日誌的基本配方/var/log/messages.這個配置文件(它真的是一個模板)發佈到所有服務器。我也有一個配置文件,只應該去讓我們說一個網絡服務器收集/var/log/httpd/access.log.

我應該創建另一個配方文件(就是你所說的他們)?這是我現在擁有的。

配方

# cookbooks/ic_sumologic/recipes/config.rb 
directory '/opt/SumoCollector/sources' do 
    owner 'root' 
    group 'sumologic_collector' 
    mode '0775' 
    action :create 
end 

# This should go to all servers 
template '/opt/SumoCollector/sources/messages.json' do 
    source 'messages.json.erb' 
    owner 'root' 
    group 'sumologic_collector' 
    mode '0664' 
    action :create 
end 

# This should only go to Apache servers 
template '/opt/SumoCollector/sources/access_logs.json' do 
    source 'access_logs.json.erb' 
    owner 'root' 
    group 'sumologic_collector' 
    mode '0664' 
    action :create 
end 

模板

# messages.json.erb 
{ 
    api.version:v1, 
    source:{ 
     name:messages, 
     "category":"<%= node.chef_environment %>_messages", 
     automaticDateParsing:true, 
     multilineProcessingEnabled:true, 
     useAutolineMatching:true, 
     forceTimeZone:false, 
     filters:[], 
     encoding:UTF-8, 
     pathExpression:/var/log/messages, 
     blacklist:[], 
     sourceType:LocalFile 
    } 
} 

# access_logs.json.erb 
{ 
    api.version:v1, 
    source:{ 
     name:messages, 
     "category":"<%= node.chef_environment %>_access", 
     automaticDateParsing:true, 
     multilineProcessingEnabled:true, 
     useAutolineMatching:true, 
     forceTimeZone:false, 
     filters:[], 
     encoding:UTF-8, 
     pathExpression:/var/log/httpd/access, 
     blacklist:[], 
     sourceType:LocalFile 
    } 
} 
+0

也許您可以使其更具可配置性,這樣您無需在需要新源時聲明新的模板資源。 erbs的內容是什麼,你可以用一些可以配置的變量來標準化嗎?這樣你可以在屬性中定義新的源代碼,迭代它們並擁有一個模板聲明 –

+0

更新我的文章。 – luckytaxi

回答

1

聲明:我沒有測試過這句法錯誤或其他任何東西,只是沒有從內存中。它也不完整。你可以做更多的事情可配置,在這裏我只是允許log_source和日誌源配置文件路徑是可配置的。 這可能是沿着這條路線的東西:

屬性

default.rb

默認[ 'sumologic'] [ '源'] =零個

資源 sumologic_source。 rb

actions :install 
    default_action :install 
    attribute :source_path, :kind_of => String, :name_attribute => true 
    attribute :log_source, :kind_of => String 

個供應商: sumologic_source.rb

action :install do 
      template new_resource.path do 
      source 'sumologic.erb' 
       owner 'root' 
       group 'sumologic_collector' 
       mode '0664' 
       action :create 
       variables(
        :source => new_resource.log_source 
      ) 
      end 
     end 

模板 默認

sumologic.json.erb 
     { 
      api.version:v1, 
      source:{ 
       name:messages, 
       "category":"<%= node.chef_environment %>_access", 
       automaticDateParsing:true, 
       multilineProcessingEnabled:true, 
       useAutolineMatching:true, 
       forceTimeZone:false, 
       filters:[], 
       encoding:UTF-8, 
       pathExpression:<%[email protected]_source%>, 
       blacklist:[], 
       sourceType:LocalFile 
      } 
     } 

呼叫配方:

unless node['sumologic']['sources'].nil? 
    node['sumologic']['sources'].each do |source| 
    sumologic source['path'] do 
     action :install 
     log_source source['log_source'] 
    end 
    end 
end 

然後,你可以設置屬性:

{ 
    "sumologic":{ 
     "sources": [{"path": "/opt/SumoCollector/sources/access_logs.json", "log_source": "/var/log/httpd/access"}] 
    } 
} 
+0

非常好。所以我確實有一個問題。對於屬性,我可以在哪裏爲每個主機組設置它?也就是說,我的Web服務器有一個不同的列表,比如說我的數據庫服務器。我想我在角色配置中提出了自己的問題。 – luckytaxi

+1

你可以通過角色設置屬性:https://docs.chef.io/attributes.html#roles –

+0

在'resources sumologic_source.rb'中應該再次使用':source_path',就像在配方中一樣? – luckytaxi

相關問題