Chef Ruby loop over attributes in an .erb template file

不打扰是莪最后的温柔 提交于 2019-12-03 08:19:23
Uri Agassi

What you probably meant was:

<% node['elasticsearch']['default'].each do |key, value| -%>
    <% unless value.empty? -%>
        <%= key %>=<%= value %>
    <% end %>
<% end %>

When iterating over a Hash, you go over its key-value pairs. So for the first iteration, key will be 'ES_USER', and value will be '' (which is not nil...).

Next you check that the value is not blank?, and print out the key=value line.

The elasticsearch cookbook was recently rewritten to use LWRP/HWRP/Custom Resources. Your implementation will need to be tweeked to work with the new cookbook.

To answer your question; node attributes are just a hash node['elasticsearch']['default'], you can pass the entire thing into the resource like so

elasticsearch_configure 'whatever' do
  configuration ( node['elasticsearch']['default'] )
  action :manage
  notifies :restart, 'elasticsearch_service[elasticsearch]'
end

Might help clarify things to see that the following are all different ways to represent a hash.

Inside a recipe

default['elasticsearch']['default']['LOG_DIR']  = '/var/log/elasticsearch'
default['elasticsearch']['default']['DATA_DIR'] = '/var/lib/elasticsearch'
...

Alternative syntax inside recipe

default[:elasticsearch][:default][:LOG_DIR]  = '/var/log/elasticsearch'
default[:elasticsearch][:default][:DATA_DIR] = '/var/lib/elasticsearch'

And another alternative syntax inside recipe

default.elasticsearch.default.LOG_DIR  = '/var/log/elasticsearch'
default.elasticsearch.default.DATA_DIR = '/var/lib/elasticsearch'

Inside a role

{
  "chef_type": "role",
  "default_attributes": {
    "elasticsearch": {
      "default": {
        "LOG_DIR": "/var/log/elasticsearch",
        "DATA_DIR": "/var/lib/elasticsearch"
      }
    }
  }
}

Since everything is a hash, and the config() resource takes a hash as a parameter, just pass the hash in as is.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!