On 2014-17-07 20:26, Cristian Falcas wrote:
Hi,

I have a "postfix" class with this init:

class postfix (
   $ensure             = 'latest',
   $email_user         = undef,
   $email_pass         = undef,
   $smtp_endpoint      = "smtp.${::domain}",
   $smtp_endpoint_port = '25',
   $from_domain        = $::domain,
   $from_user          = 'donotreply',
   $debug              = false) {
   anchor { 'postfix::begin': }
   anchor { 'postfix::end': }

   include postfix::install
   include postfix::config
   include postfix::service

   Anchor['postfix::begin'] ->
   Class['postfix::install'] ->
   Class['postfix::config'] ~>
   Class['postfix::service'] ->
   Anchor['postfix::end']
}


And I call it from an other module like this:

   class { 'postfix':
     email_user         => hiera('email_user', undef),
     email_pass         => hiera('email_pass', undef),
     smtp_endpoint      => hiera('email_smtp_endpoint', undef),
     smtp_endpoint_port => hiera('smtp_endpoint_port', undef),
     from_domain        => hiera('email_from_domain', undef),
     from_user          => hiera('email_from_user', undef),
   }

All parameters that not found in hiera are not initialized with the
default values. In the template I use for the config I get only empty
values.

Best regards,
Cristian Falcas

This is caused by the undef given to hiera (as the default value) is transformed to an empty string. This value is then passed on as the value of the parameter, and the parameter is bound to empty string, since it is only exactly undef that selects the default over the given value. The transformation to empty string is dictated by the 3x function API. (The 4x function API in Puppet 4.0 does not do this).

Thus, for now, you have to do the transformation back to undef yourself. There is no easy way to do this in the puppet language with the 3x (current) parser - especially not if an empty string is to be considered a valid value (i.e. it is difficult to differentiate between the two). (Or, do what jcbollinger suggests).

If empty strings are not valid and it is ok to always treat them as undef, then you can write a ruby function that translates the value to undef (or write a ruby function that calls hiera and modifies the return value - this to avoid having to wrap each call to hiera in your puppet logic).

e.g. something like this

   Puppet::Parser::Functions::newfunction(
  :fix_undef, :type => :rvalue, :arity => 1,
  :doc => "transforms '' to undef") do |args|

  case args[0]
  when ''
    nil
  else
    args[0]
  end
end

Then use it like this:

 class { 'postfix':
   email_user         => fix_undef(hiera('email_user', undef)),
   email_pass         => fix_undef(hiera('email_pass', undef)),
   smtp_endpoint      => fix_undef(hiera('email_smtp_endpoint', undef)),
   smtp_endpoint_port => fix_undef(hiera('smtp_endpoint_port', undef)),
   from_domain        => fix_undef(hiera('email_from_domain', undef)),
   from_user          => fix_undef(hiera('email_from_user', undef)),
 }

If you want to wrap the hiera function, do this:

   Puppet::Parser::Functions::newfunction(
  :my_hiera, :type => :rvalue, :arity => -2,
  :doc => "transforms '' to undef") do |args|

  case function_hiera(args)
  when ''
    nil
  else
    args[0]
  end
end

And use like this:

   email_user         => my_hiera('email_user', undef),

And you can continue to elaborate on this implementation so you do not have to pass undef (i.e. an empty string) and instead supply this value inside your my_hiera. You can then use another magic value as the "undef" default that you translate in your function. If you try to give hiera a default that is truly undefined (i.e. nil), then it will think it did not find anything and raise an error.

You are probably better off doing what jcbollinger suggested.

- henrik
--

Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/

--
You received this message because you are subscribed to the Google Groups "Puppet 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to puppet-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-users/lqhte8%24t3i%241%40ger.gmane.org.
For more options, visit https://groups.google.com/d/optout.

Reply via email to