【问题标题】:How to handle different templates by server groups in Chef?如何在 Chef 中按服务器组处理不同的模板?
【发布时间】:2017-06-21 23:51:30
【问题描述】:

新厨师用户...

我想推出 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
    }
}

【问题讨论】:

  • 也许您可以使其更具可配置性,这样您就不必在需要新源时声明新的模板资源。 erbs的内容是什么,你能用一些可以配置的变量使它成为标准吗?这样你就可以在属性中定义新的源,迭代它们并拥有一个模板声明
  • 更新了我的帖子。

标签: chef-infra chef-recipe


【解决方案1】:

免责声明:我没有对此进行过语法错误或其他任何测试,只是凭记忆进行的。它也是不完整的。你可以让更多的东西可配置,这里我只是允许 log_source 和日志源配置文件路径是可配置的。 可能是这样的:

属性

default.rb

默认['sumologic']['sources'] = 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"}]
  }
}

【讨论】:

  • 非常好。所以我确实有一个问题。对于属性,我可以在哪里为每个主机组设置它?意思是,我的网络服务器与假设我的数据库服务器有不同的列表。我想我在角色配置中提出了自己的问题。
  • Inresources sumologic_source.rb ... 是否应该再次使用:source_path,就像在食谱中一样?
  • 既然是name属性,调用资源时已经在recipe中使用了它: sumologic source['path'] do where source是数组属性['中的对象之一sumologic']['来源']
猜你喜欢
  • 2016-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-06
  • 2017-05-05
  • 2012-05-09
  • 1970-01-01
相关资源
最近更新 更多