sdot257
sdot257

Reputation: 10376

How to handle different templates by server groups in Chef?

New Chef user ...

I want to push out config files for a Sumologic install. I have a basic recipe that collects logs for /var/log/messages. This config file (it's really a template) goes out to ALL servers. I also have a config file that should only go to let's say a web server to collect /var/log/httpd/access.log.

Should I be creating another recipe file (is that what you call them)? Here's what I have now.

recipe

# 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

template

# 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
    }
}

Upvotes: 0

Views: 59

Answers (1)

Miguel Marques
Miguel Marques

Reputation: 2866

Disclaimer: I haven't tested this for syntax errors or anything else, just did it from memory. It is also incomplete. You could make more things configurable, here I am just allowing log_source and the log source config file path to be configurable. It could be something along this lines:

attributes

default.rb

default['sumologic']['sources'] = nil

resources sumologic_source.rb

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

providers: 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

templates default

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
            }
        }

call in the recipe:

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

Then you could set the attribute:

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

Upvotes: 1

Related Questions