Defined type parameter defaults not set (undef)

45 views
Skip to first unread message

Martijn Grendelman

unread,
Dec 3, 2014, 9:35:02 AM12/3/14
to puppet...@googlegroups.com
Hi,

Please consider the following simplified code:

class apache::params {
$priority = 25
}

define apache::vhost (
$priority = $::apache::params::priority
) {

include apache::params

notify { "$name prio 1: $apache::params::priority": }
notify { "$name prio 2: $priority": }
}

apache::vhost { 'test': }


Is there ANY way that the notifys in the apache::vhost type would display a different value? Shouldn't $apache::params::priority and $priority not be the same?

I have a case where on some nodes, $priority (and other parameters that have defaults from $::apache::params that I omitted here) are empty (undef). If I access $::apache::params::priority directly, the correct value is presented.
This code is in use on many nodes, and most don't exhibit the problem.

Any idea what might be the problem here? I've been trying different things for over an hour, and I'm afraid I'm losing my sanity...

Best regards,
Martijn.

Wil Cooley

unread,
Dec 3, 2014, 9:10:25 PM12/3/14
to puppet-users group


On Dec 3, 2014 6:53 AM, "Martijn Grendelman" <martijng...@gmail.com> wrote:
>
> Hi,
>
> Please consider the following simplified code:
>
> class apache::params {
> $priority = 25
> }
>
> define apache::vhost (
> $priority = $::apache::params::priority
> ) {
>
> include apache::params
>
> notify { "$name prio 1: $apache::params::priority": }
> notify { "$name prio 2: $priority": }
> }
>
> apache::vhost { 'test': }
>
>
> Is there ANY way that the notifys in the apache::vhost type would display a different value? Shouldn't $apache::params::priority and $priority not be the same?

AIUI, with a class, you'd need to inherit from params for the default to work. With a define, I suspect it's parse-order dependent and you've just gotten lucky.

Rather than doing what you're doing, consider making the default undef and using 'pick' from puppetlabs/stdlib to choose:

  $real_priority = pick($priority, $apache::params::priority)

> I have a case where on some nodes, $priority (and other parameters that have defaults from $::apache::params that I omitted here) are empty (undef). If I access $::apache::params::priority directly, the correct value is presented.
> This code is in use on many nodes, and most don't exhibit the problem.
>
> Any idea what might be the problem here? I've been trying different things for over an hour, and I'm afraid I'm losing my sanity...
>
> Best regards,
> Martijn.
>

> --
> 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...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/4b344b14-de9b-446c-93e7-110d335f91cd%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Henrik Lindberg

unread,
Dec 3, 2014, 11:02:02 PM12/3/14
to puppet...@googlegroups.com
On 2014-03-12 15:35, Martijn Grendelman wrote:
> Hi,
>
> Please consider the following simplified code:
>
> class apache::params {
> $priority = 25
> }
>
> define apache::vhost (
> $priority = $::apache::params::priority
> ) {
>
> include apache::params
>
> notify { "$name prio 1: $apache::params::priority": }
> notify { "$name prio 2: $priority": }
> }
>
> apache::vhost { 'test': }
>
>
> Is there ANY way that the notifys in the apache::vhost type would
> display a different value? Shouldn't $apache::params::priority and
> $priority not be the same?
>
> I have a case where on /some/ nodes, $priority (and other parameters
> that have defaults from $::apache::params that I omitted here) are
> empty (undef). If I access $::apache::params::priority directly, the
> correct value is presented.
> This code is in use on many nodes, and most don't exhibit the problem.
>
> Any idea what might be the problem here? I've been trying different
> things for over an hour, and I'm afraid I'm losing my sanity...
>
You have an order of evaluation problem primarily. See here for an
explanation of in which order classes and defines are evaluated:
http://puppet-on-the-edge.blogspot.se/2014/04/getting-your-puppet-ducks-in-row.html

The problem is that you have not included the class (and thus not
defined the parameter $::apache::params::priority) at the time
you are instantiating the resource apache::vhost{test: }. If you instead
include the class prior to instantiating the resource it may work better.

(Or organize the thing differently once you read about the order classes
and defines are evaluated.)

- henrik
> Best regards,
> Martijn.
> --
> 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...@googlegroups.com
> <mailto:puppet-users...@googlegroups.com>.
> <https://groups.google.com/d/msgid/puppet-users/4b344b14-de9b-446c-93e7-110d335f91cd%40googlegroups..com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.


--

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


Martijn Grendelman

unread,
Dec 4, 2014, 3:50:56 AM12/4/14
to puppet...@googlegroups.com
Hi Henrik and Wil,


> Is there ANY way that the notifys in the apache::vhost type would
> display a different value? Shouldn't $apache::params::priority and
> $priority not be the same?
>
> I have a case where on /some/ nodes, $priority (and other parameters
> that have defaults from $::apache::params that I omitted here) are
> empty (undef). If I access $::apache::params::priority directly, the
> correct value is presented.
> This code is in use on many nodes, and most don't exhibit the problem.
>
> Any idea what might be the problem here? I've been trying different
> things for over an hour, and I'm afraid I'm losing my sanity...
>
You have an order of evaluation problem primarily. See here for an
explanation of in which order classes and defines are evaluated:
http://puppet-on-the-edge.blogspot.se/2014/04/getting-your-puppet-ducks-in-row.html

The problem is that you have not included the class (and thus not
defined the parameter $::apache::params::priority) at the time
you are instantiating the resource apache::vhost{test: }. If you instead
include the class prior to instantiating the resource it may work better.

(Or organize the thing differently once you read about the order classes
and defines are evaluated.)

Thank you both for helpful tips. I see I need to rethink some stuff :-)

Best regards,
Martijn.

Martijn Grendelman

unread,
Dec 4, 2014, 4:01:02 AM12/4/14
to puppet...@googlegroups.com

Hence the following code in puppetlabs-apache's vhost type:

  # The base class must be included first because it is used by parameter defaults
  if ! defined(Class['apache']) {
    fail('You must include the apache base class before using any apache defined resources')
  }
 
I get it now :-)

Thanks again,
Martijn.

jcbollinger

unread,
Dec 4, 2014, 9:10:17 AM12/4/14
to puppet...@googlegroups.com


On Thursday, December 4, 2014 3:01:02 AM UTC-6, Martijn Grendelman wrote:Hence the following code in puppetlabs-apache's vhost type:

  # The base class must be included first because it is used by parameter defaults
  if ! defined(Class['apache']) {
    fail('You must include the apache base class before using any apache defined resources')
  }
 


Yuck.  At least that's a relatively benign use of defined(), in that when its inherent evaluation-order dependency falls the wrong way the result is certain catalog compilation failure (as opposed to a possibly-wrong declaration).  That there is a need for this sort of thing at all is a function of the mere existence of the resource-like class declaration syntax.  How I long for the days when you could simply rely on classes' singleton nature, so that instead of the above it would be safe simply to write "include 'apache'".


John

Reply all
Reply to author
Forward
0 new messages