Passing 'undef' as a class parameter value

876 views
Skip to first unread message

DEGREMONT Aurelien

unread,
Oct 27, 2015, 5:03:33 AM10/27/15
to puppet...@googlegroups.com
Hello

When using class parameters I often face the same issue regarding undef
usage.
Let's say I got this simple class:

class foo (
$service_ensure = 'running',
) {
service { 'foo':
ensure => $service_ensure;
}
}


include foo # will set the service running


class { 'foo':
service_ensure => 'stopped'; # will set the service stopped
}


And now, I would like to say 'do not care about ensure'. Do not touch it.
For that, I need that ensure is set to 'undef'

But I cannot use:

class { 'foo':
service_ensure => undef;
}

because in this case, Puppet will use the default value of the
parameter, in this case: running.

I have this problem for a lot of different modules and I looking for a
simple way to do this.
I would like to avoid adding an if/else in all my classes.


Thanks in advance

Aurélien

Christopher Wood

unread,
Oct 27, 2015, 6:12:57 AM10/27/15
to puppet...@googlegroups.com
Unless there have been any developments of which I am unaware, you would need to handle this in the code+hiera. Here's where I dribbled all over this thing a few months ago:

https://groups.google.com/forum/#!msg/puppet-users/0l3m4VCHjOg/KKlz5LNpCgYJ
> --
> 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/562F3DD7.8010008%40cea.fr.
> For more options, visit https://groups.google.com/d/optout.

DEGREMONT Aurelien

unread,
Oct 27, 2015, 7:07:39 AM10/27/15
to puppet...@googlegroups.com
Le 27/10/2015 11:12, Christopher Wood a écrit :
> Unless there have been any developments of which I am unaware, you would need to handle this in the code+hiera. Here's where I dribbled all over this thing a few months ago:
>
> https://groups.google.com/forum/#!msg/puppet-users/0l3m4VCHjOg/KKlz5LNpCgYJ

I think it is different. In my example, there is no hiera involved.
My problem is :
I want my class param value to be 'undef', and not it being 'use the
default value'

There is no way to set a class param value to undef if there is default
value for it different from undef.

Thanks anyway

Christopher Wood

unread,
Oct 27, 2015, 9:14:48 AM10/27/15
to puppet...@googlegroups.com
Using undef in a class declaration... appears to be the subject of a bunch of documents I am ill-qualified to interpret.

https://docs.puppetlabs.com/puppet/latest/reference/lang_data_undef.html

"Puppet’s special undef value is roughly equivalent to nil in Ruby; it represents the absence of a value", implying that using undef in a class parameter is like not having that class parameter in the first place. I thought of hiera because when hiera returns nil it is interpreted as "value not present" per that thread.

https://docs.puppetlabs.com/puppet/latest/reference/lang_classes.html#overriding-resource-attributes

https://projects.puppetlabs.com/issues/16221

So yes, absolutely doesn't work.
> To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/562F5AEE.8040007%40cea.fr.

jcbollinger

unread,
Oct 27, 2015, 9:53:39 AM10/27/15
to Puppet Users


On Tuesday, October 27, 2015 at 4:03:33 AM UTC-5, Aurélien Degrémont wrote:
Hello

When using class parameters I often face the same issue regarding undef
usage.
Let's say I got this simple class:

class foo (
   $service_ensure = 'running',
) {
   service { 'foo':
      ensure => $service_ensure;
   }
}


include foo # will set the service running


class { 'foo':
   service_ensure => 'stopped'; # will set the service stopped
}


And now, I would like to say 'do not care about ensure'. Do not touch it.
 
For that, I need that ensure is set to 'undef'


The easiest way to approach the problem is to default to not managing whether the service foo is running, or else to avoid declaring class foo at all on machines where you don't care about the state of the resources it manages.

 

But I cannot use:

class { 'foo':
   service_ensure => undef;
}

because in this case, Puppet will use the default value of the
parameter, in this case: running.

I have this problem for a lot of different modules and I looking for a
simple way to do this.
I would like to avoid adding an if/else in all my classes.



Bad news: undef is not a value, so you cannot pass it or assign it.  Accepted forms that look like assigning or passing undef have the effect of undefining an existing definition or affirming that none is given.  If you want such behavior to be available, but not to be the default, then you pretty much need conditional logic.  The traditional form of this usually goes the other way, but here's how you could apply it to your situation:

class foo ($service_ensure = 'running') {
  service
{ 'foo':
   
ensure => $service_ensure ? { 'UNDEF' => undef, default => $service_ensure }
 
}
}

On the other hand, there are ways to shift around the location of the conditionality.  The forms most likely to suit you involve overriding resource properties.  You can do that anywhere, for any resource, via a resource collector, but I'm a fan employing a bit of discipline and using classification to control such things.  That requires using class inheritance (and this is the original and most appropriate use case for class inheritance).  To approach the problem that way, you would add a class alongside to your original one:

class foo (
   $service_ensure
= 'running',
) {
   service
{ 'foo':
     
ensure => $service_ensure;
   
}
}

class foo::unmanaged inherits foo {
   
Service['foo'] {
     
ensure => undef
   
}
}

Declaring class foo::unmanaged either instead of or in addition to class foo will then have all the effects of declaring class foo, except that the ensure property of Service['foo'] will be as if never declared.  In this case, the conditionality is pulled up to wherever you decide whether to declare class foo::unmanaged, and that can be all the way to the ENC / node block selector, so that it does not correspond to any explicit conditional in your DSL code.

There are other ways to approach the problem as well, mostly revolving around using Hiera for binding data to your class parameters (which is absolutely the only way you should ever assign non-default parameters to your modules' public classes).  It's not straightforward in Hiera, however, because as far as I know, Hiera does not provide a means to override a property mapping with absence of any mapping.


John

Reply all
Reply to author
Forward
0 new messages