6

So this might be a little confusing but bear with me. In short I want to loop over all attributes with a certain key value and then insert them into a template if the values are not empty. Here is my code:

ATTRIBUTES:

# === Default file Configurations
#
default['elasticsearch']['default']['ES_USER']              = ''
default['elasticsearch']['default']['ES_GROUP']             = ''
default['elasticsearch']['default']['ES_HEAP_SIZE']         = ''
default['elasticsearch']['default']['MAX_OPEN_FILES']       = ''
default['elasticsearch']['default']['MAX_LOCKED_MEMORY']    = 'unlimited'
default['elasticsearch']['default']['MAX_MAP_COUNT']        = ''
default['elasticsearch']['default']['LOG_DIR']              = '/var/log/elasticsearch'
default['elasticsearch']['default']['DATA_DIR']             = '/var/lib/elasticsearch'
default['elasticsearch']['default']['WORK_DIR']             = '/tmp/elasticsearch'
default['elasticsearch']['default']['CONF_DIR']             = '/etc/elasticsearch'
default['elasticsearch']['default']['CONF_FILE']            = '/etc/elasticsearch/elasticsearch.yml'
default['elasticsearch']['default']['RESTART_ON_UPGRADE']   = ''

TEMPLATE:

<% node['elasticsearch']['default'].each do |host| -%>
    <% if node.elasticsearch.default.host not nil -%>
        <%= host %>=<%= node.elasticsearch.default.host %>
<% end %>

OUTPUT (hopefully):

MAX_LOCKED_MEMORY=unlimited
LOG_DIR=/var/log/elasticsearch
DATA_DIR=/var/lib/elasticsearch
WORK_DIR=/tmp/elasticsearch
CONF_DIR=/etc/elasticsearch
CONF_FILE=/etc/elasticsearch/elasticsearch.yml

My ruby isn't the best because I'm just starting out with all of this stuff but I couldn't find any examples for this type of situation. Any help would be great, thanks.

jarsever
  • 690
  • 1
  • 7
  • 15

2 Answers2

10

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.

jarsever
  • 690
  • 1
  • 7
  • 15
Uri Agassi
  • 36,848
  • 14
  • 76
  • 93
  • Thank you... your answer was spot on except for a missing `<% end %>` after `<%= key %>=<%= value %>`. – jarsever Mar 07 '14 at 00:50
  • Oh and it is actually `unless value.empty?` because for some reason it wasn't accepting `blank` in the function. – jarsever Mar 07 '14 at 17:38
  • When I use this loop the chef run is outputting the whole json rather than the string of the value and key itself. Can you suggest how I can force it to putput the string? – Benjamin Goodacre Dec 31 '14 at 13:57
  • @BenjaminGoodacre - what is the exact code you are using? what is the input? – Uri Agassi Dec 31 '14 at 17:15
0

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.

spuder
  • 17,437
  • 19
  • 87
  • 153