Chef infra 重写默认哈希而不合并

Chef infra 重写默认哈希而不合并,chef-infra,logstash,chef-solo,Chef Infra,Logstash,Chef Solo,我试图在使用chef logstash cookbook,但很难覆盖['logstash']['instance']['server']['config_templates']属性。当我通过我的包装器食谱将其设置为时,我会得到一个默认值的合并散列,以及我通过包装器食谱添加的内容 作为参考,我在食谱中使用的代码是: #force override our attributes (or attempt to anyways) #attributes = node['logstash']['insta

我试图在使用chef logstash cookbook,但很难覆盖
['logstash']['instance']['server']['config_templates']
属性。当我通过我的包装器食谱将其设置为时,我会得到一个默认值的合并散列,以及我通过包装器食谱添加的内容

作为参考,我在食谱中使用的代码是:

#force override our attributes (or attempt to anyways)
#attributes = node['logstash']['instance'][name]
node.force_override['logstash']['instance']['server']['config_templates'] = {}
node.force_override['logstash']['instance']['server']['config_templates'] = {
   'input_redis' => 'config/input_redis.conf.erb',
   'filter_sidewinder' => 'config/filter_sidewinder.conf.erb',
   'output_elasticsearch' => 'config/output_elasticsearch.conf.erb'
}

我怎样才能完全覆盖这个属性,只设置为我的包装器食谱中的内容?

在@LearnChef工作人员提供了大量帮助后,解决方案是在我的包装器配方中使用ruby代码来迭代键并删除它们。gotchya确保迭代和删除循环使用与原始默认属性相同的属性优先级,原始默认属性在源LWRP中设置的不是默认级别,而是正常级别

最终代码如下所示:

#clear out the default config templates
attributes = node['logstash']['instance'][name]
node.normal['logstash']['instance'][name]['config_templates'].keys.each do |k|
  node.normal['logstash']['instance'][name]['config_templates'].delete(k)
end
node.force_override['logstash']['instance']['server']['config_templates'] = {
  'input_redis' => 'config/input_redis.conf.erb',
  'filter_sidewinder' => 'config/filter_sidewinder.conf.erb',
  'output_elasticsearch' => 'config/output_elasticsearch.conf.erb'
}

在类似的用例中,我只想将我的哈希设置为空哈希。对我来说,一个快速而肮脏的解决方法是将属性设置为空数组,如下所示:

原始属性定义:

我在环境中的定义:

"elasticsearch": {
  "plugins": []
}
这是使用它的代码:

node[:elasticsearch][:plugins].each do | name, config |
  next if name == 'elasticsearch/elasticsearch-cloud-aws' && !node.recipe?('aws')
  next if name == 'elasticsearch/elasticsearch-cloud-gce' && !node.recipe?('gce')
  install_plugin name, config
end

只是把它放在那里,以防你也在寻找一个快速/临时的黑客。希望有帮助

谢谢你,赛斯。这是我在研究这个问题时遇到的博客之一那篇文章谈到了哈希在添加和删除方面是如何优于数组的,但我在文章中没有看到关于如何进行删除的示例,也没有看到任何迹象表明上面的代码是错误的。我怀疑我在某些事情上过于沉迷,但就我个人而言,我不知道是什么(你不能…,这就是Noah说的。你需要将它们设置为
nil
我还建议问John Vincent。这是他的食谱,这个问题似乎更适合由作者回答GitHub问题,而不是Stackoverflow。我会按照你的建议跟进John。为了澄清,哈希只能合并到chef.Key中s永远不能被删除,即使使用force_override?这看起来很尴尬。在这一点上,您也可以只提供自己的模板并忽略其中的变量。
node[:elasticsearch][:plugins].each do | name, config |
  next if name == 'elasticsearch/elasticsearch-cloud-aws' && !node.recipe?('aws')
  next if name == 'elasticsearch/elasticsearch-cloud-gce' && !node.recipe?('gce')
  install_plugin name, config
end