Puppet template erb if variable is not defined keep default

6,882

Solution 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)

Solution 2

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 %>
Share:
6,882

Related videos on Youtube

cr0c
Author by

cr0c

Updated on September 18, 2022

Comments

  • cr0c
    cr0c over 1 year

    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.

  • cr0c
    cr0c almost 10 years
    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.
  • Michael Hampton
    Michael Hampton almost 10 years
    That's probably not a good idea, because if elasticsearch changes their idea of the default, then you will get unexpected behavior.
  • cr0c
    cr0c almost 10 years
    Oh that would be the issue as well yes. I guess I will keep it the way you sed :) Thank you :)
  • Felix Frank
    Felix Frank almost 10 years
    Fair point, but does not address the issue from the question.