0

I am trying to learn puppet and I can not understand how I can setup template erb to choose default value if variable is not defined in common.yaml or in node.yaml. This is what I have tried: 1)

# we don't need to run the embedded HTTP server here
<% if @elasticsearch_http_enabled %>
elasticsearch_http_enabled = <%= @elasticsearch_http_enabled %>
<% else %>
#elasticsearch_http_enabled = false
<% end %>

2)

# we don't need to run the embedded HTTP server here
<%- if @elasticsearch_http_enabled then -%>
elasticsearch_http_enabled = <%= @elasticsearch_http_enabled %>
<% else %>
#elasticsearch_http_enabled = false
<% end %>

3)

# we don't need to run the embedded HTTP server here
<% if @elasticsearch_http_enabled then %>
elasticsearch_http_enabled = <%= @elasticsearch_http_enabled %>
<% else %>
#elasticsearch_http_enabled = false
<% end %>

When I tried these I got error:

Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Could not find data item gl2_srv_elasticsearch_http_enabled in any Hiera data file and no default supplied at /etc/puppet/envs/testing/modules/graylog2/manifests/server.pp:28 on node

How can I make it so if I do not define the variable it puts the default variable to the config.

1

The template is not generating this error. Instead, your manifest is retrieving the data with a call like this:

hiera('gl2_srv_elasticsearch_http_enabled')

This will fail if in your Hiera data (let's assume YAML), this key does not appear, e.g.

gl2_srv_elasticsearch_http_enabled: true

To avoid this problem, define a default and pass it as the second argument to the hiera function.

hiera('gl2_srv_elasticsearch_http_enabled', false)
1

None of the above.

Instead, supply defaults for all of your parameters in, e.g. a manifests/params.pp.

An example init.pp:

class elasticsearch (
    $http_enabled        = $::elasticsearch:params:http_enabled,
) inherits ::elasticsearch:params {
    # your class here
}

An example params.pp:

class elasticsearch:params {
    $http_enabled = false 
}

This lets you isolate code which must choose different defaults for, e.g. different operating systems or distributions.

If you're 100% sure the default should always be false, you can just declare it as such in init.pp and forget about adding it to params.pp.

class elasticsearch (
    $http_enabled        = false,
) inherits ::elasticsearch:params {
    # your class here
}

Your template is then simplified to:

elasticsearch_http_enabled = <%= @http_enabled %>
4
  • Hello! How can I do it without params and without defining it anywhere so if its not defined then it just takes the variable that is commented out. I hope you understood what I ment. – mYzk Jul 28 '14 at 15:58
  • That's probably not a good idea, because if elasticsearch changes their idea of the default, then you will get unexpected behavior. – Michael Hampton Jul 28 '14 at 16:04
  • Oh that would be the issue as well yes. I guess I will keep it the way you sed :) Thank you :) – mYzk Jul 28 '14 at 16:05
  • Fair point, but does not address the issue from the question. – Felix Frank Jul 29 '14 at 0:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.