Ask Your Question
3

hiera dealing with undefined variables in erb templates

asked 2014-08-12 05:16:48 -0500

jaksky gravatar image

Hi,

I start using hiera for environment or machine specific variables but I am facing an issue when variable is not found nowhere. Desired behaviour is that shouldn't be included into template. I did following: In my manifest code

$yarn_app_mapreduce_am_command_opts=hiera('yarn.app.mapreduce.am.command-opts',undef)

and in erb template I have:

<% if !@yarn_app_mapreduce_am_command_opts.nil? %>   
    <property>
    <name>yarn.app.mapreduce.am.command-opts</name>
    <value><%= @yarn_app_mapreduce_am_command_opts %></value>
  </property>
<%end %>

According to the documentation here safest is to test on nil. I tried several variants but non of them worked.

Could someone help?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2014-08-12 16:19:07 -0500

timelord gravatar image

Maybe a workaround:

$yarn_app_mapreduce_am_command_opts=hiera('yarn.app.mapreduce.am.command-opts', '')

$yarn_app_mapreduce_am_command_opts_real = $yarn_app_mapreduce_am_command_opts ? {
   ''      => nil,
   nil     => nil,
   default => $yarn_app_mapreduce_am_command_opts,
}

<% if !@yarn_app_mapreduce_am_command_opts_real.nil? %>   
    <property>
    <name>yarn.app.mapreduce.am.command-opts</name>
    <value><%= @yarn_app_mapreduce_am_command_opts %></value>
  </property>
<%end %>
edit flag offensive delete link more
0

answered 2014-08-12 22:54:23 -0500

ramindk gravatar image

I just set undef and then only check for the var. Seems to work on my system.

modules/redis/manifests/server.pp

define redis::server (
  $password     = undef,
  $port         = '6379',
  $bind         = '0.0.0.0',
  $master       = 'localhost',
  $master_port  = $port
) { }

modules/redis/templates/redis-slave.conf.erb

# blah blah other stuff
<% if @password -%>
masterauth <%= @password %>
<% end -%><% end -%>
edit flag offensive delete link more

Comments

I would say that you are testing a bit different use case. Seems that hiera function is causing the problem in this case :-)

jaksky gravatar imagejaksky ( 2014-08-13 02:25:35 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2014-08-12 05:16:48 -0500

Seen: 1,657 times

Last updated: Aug 12 '14