如何在Chef中按服务器组处理不同的模板?

时间:2017-06-21 14:40:59

标签: chef chef-recipe

新厨师用户......

我想推出配置文件以进行Sumologic安装。我有一个基本的配方,收集/var/log/messages.的日志这个配置文件(它真的是一个模板)发送给所有服务器。我还有一个配置文件,只应该让我们说一个Web服务器来收集/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
    }
}

1 个答案:

答案 0 :(得分:1)

免责声明:我还没有对语法错误或其他任何内容进行测试,只是从内存中进行了测试。它也不完整。您可以将更多内容配置为可配置,这里我只允许log_source和日志源配置文件路径可配置。 它可能是这样的:

属性

default.rb

  

默认[&#39; sumologic&#39;] [&#39;来源&#39;] = nil

资源     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:<%=@log_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"}]
  }
}