# hiera dealing with undefined variables in erb templates

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 close merge delete ## 2 Answers Sort by » oldest newest most voted 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 %> more 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
<% end -%><% end -%>
more

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

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