Puppet template erb if variable is not defined keep default

puppetruby

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.

Best Answer

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)
Related Topic