Ask Your Question
0

Ignore line in template if variable is undefined or empty

asked 2016-11-24 15:37:31 -0500

Poppey gravatar image

updated 2016-11-24 15:40:02 -0500

How do I make sure that line is not inserted if variable (in hiera) is empty or undefined?

Example:

common.yaml has a default variable defined, but nodes/hostname is undefined, so it should be ignored.

File: hieradata/nodes/myserver.yaml

mypackage::version:
  -

File: modules/mypackage/template/config.erb

<% version.each do |package_version| %>
version_allowed = <%= package_version %>  
<% end %>
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2016-11-24 18:09:53 -0500

updated 2016-11-24 18:14:33 -0500

At which level (do you wanna tackle this situation)?

In the embedded ruby template you can wrap it into a nil? condition

<%-
version = scope['mypackage::version']
unless version.nil? then
  version.each() { |package_version|
    -%>version_allowed = <%= package_version.to_s() + "\n" %><%
  }
end
-%>

In puppet language it's defined()

if ! defined('version') {
  file { # file resource using your template
  }
}
edit flag offensive delete link more

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: 2016-11-24 15:37:31 -0500

Seen: 77 times

Last updated: Nov 24 '16