Search code examples
puppet

Puppet printing undef string instead of nil


I'm currently in the process of updating some legacy Puppet files to a newer version of puppet and am running into the following problem:

The hieradata for one of our server has variables that can be left undefined and still work when we generate an env.yml for our RoR application from an erb file.

Previously, this worked correctly with our env.yml generating those values like:

read_only_mode:

With our update to Puppet v5, the values now generate as:

read_only_mode: undef

In the erb template:

read_only_mode: <%= @data['read_only_mode'] %>

I'm currently trying to write a test in the Puppet file that generates the env.yml with the thought that the following logic should work:

for ($key, value in $hieradata) {
  if ($hierdata[$key] == undef) {
    $hieradata[$key] = '' // Empty string
  }
}

As implemented:

$envdata.each |String $key, String $value| {
  if $envdata[$key] == undef {
    $envdata[$key] = ''
  }
}

However, this isn't working and the undef string is still being printed.

Does anyone have ideas as to a solution to this issue?


Solution

  • We ended up changing the Hash values using the following code:

    $data_tuples = $data.map |$key, $value| {
      if type($value) == Undef {
        [$key, '']
      } else {
        [$key, $value]
      }
    }
    
    $data_filtered = Hash($data_tuples)