overriding class parameters with 'undef'

593 views
Skip to first unread message

Jeffrey Lewis

unread,
Jan 10, 2014, 6:04:50 PM1/10/14
to puppet...@googlegroups.com
Hello,

I apologize in advance if this question is redundant.

I'm using puppet version 2.7.11 (because that's what Ubuntu 12.04 LTS has to offer) and I'd like to override a class default parameter with 'undef'.  For example, the puppetlabs 'apache' module (version 0.10.0) sets 'ensure => running' for service 'apache' by default.  I do not what puppet to attempt starting or stopping apache, so I'm trying the following (which does not work): 

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

The above gives me the default 'ensure => running' for the 'apache' service.  Any idea how I can express 'ensure => undef' when declaring the apache class in my manifest?

Could suppose I could always hack the 'apache' module, but would prefer not to.

Thanks,
Jeffrey

David Arroyo

unread,
Jan 11, 2014, 10:49:14 AM1/11/14
to puppet...@googlegroups.com, puppet...@googlegroups.com
Hi Jeffrey,

You cannot override the default parameter to be undef, but you can override the service resource's behaviour with a resource collector[0]. It's less than ideal because you have to know the name of the service, but something like this:

class{'apache':}
Service <|title == 'apache'|> {
  noop => true,
}

should work. You can alternatively search by the class tag, but be sure it has no unintended side effects.

Cheers,
David

--
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/af002012-5084-48e8-9f61-9fac2b64889e%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

jcbollinger

unread,
Jan 13, 2014, 9:27:17 AM1/13/14
to puppet...@googlegroups.com


On Friday, January 10, 2014 5:04:50 PM UTC-6, Jeffrey Lewis wrote:
Hello,

I apologize in advance if this question is redundant.

I'm using puppet version 2.7.11 (because that's what Ubuntu 12.04 LTS has to offer) and I'd like to override a class default parameter with 'undef'.  For example, the puppetlabs 'apache' module (version 0.10.0) sets 'ensure => running' for service 'apache' by default.  I do not what puppet to attempt starting or stopping apache, so I'm trying the following (which does not work): 

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

The above gives me the default 'ensure => running' for the 'apache' service.  Any idea how I can express 'ensure => undef' when declaring the apache class in my manifest?



All class parameters must have values assigned, whether by explicit declaration or by default value (or by automated data binding, but that requires Puppet 3).  Undef is not a value.  In fact "not a value" is practically its definition.  Declaring a class or resource parameter as undef is an explicit expression of not assigning a value.  That can actually make a difference in the presence of declared resource-type defaults (e.g. File { owner => 'root', mode => 0644 } ), but what you're fighting with is a different kind of default -- the one assigned when there is no explicit value declared.  That is exactly the situation you present by declaring the parameter undef.

 
Could suppose I could always hack the 'apache' module, but would prefer not to.



Check the module documentation, but it sounds like the stock version does not support what you want to do.  David's suggestion to override Service['apache'] might succeed for you or it might not, as there could be other effects of the 'service_ensure' parameter, and the title of the relevant Service resource doesn't have to be 'apache'.  Either way, it is poor form to touch module internals, such as the putative Service['apache'], except as module documentation supports.

David's might still be the approach that works best for you.  Personally, though, I would look into modifying the module appropriately, then opening an issue against it and contributing your solution.  Perhaps you could make an empty value of service_ensure suppress managing the run state of the service.


John

Jeffrey Lewis

unread,
Jan 13, 2014, 2:01:46 PM1/13/14
to puppet...@googlegroups.com
On Monday, January 13, 2014 6:27:17 AM UTC-8, jcbollinger wrote:
All class parameters must have values assigned, whether by explicit declaration or by default value (or by automated data binding, but that requires Puppet 3).  Undef is not a value.  In fact "not a value" is practically its definition.  Declaring a class or resource parameter as undef is an explicit expression of not assigning a value.


Right.  So, setting "service_ensure => undef" is equivalent to setting nothing, and the module uses the default value for service_ensure (which is 'running'). 

 
Check the module documentation, but it sounds like the stock version does not support what you want to do.  

 
Agreed.  Worse, the documentation states that setting 'service_ensure => undef' should work, but that isn't the case for me, at least in puppet 2.7.11.

service_ensure

Determines whether the service should be running. Can be set to 'undef' which is useful when you want to let the service be managed by some other application like pacemaker. Defaults to 'running'.

 
Personally, though, I would look into modifying the module appropriately, then opening an issue against it and contributing your solution.  Perhaps you could make an empty value of service_ensure suppress managing the run state of the service.

 
I ended up using a 'selector' as a ternary operator to do just that.  Hopefully my syntax isn't too ugly.  I'll see if the puppetlabs folks can accept this upstream.  For reference, I modified manifests/service.pp to look like this:

  service { 'httpd':
    ensure => $service_ensure ? {
                ''      => undef,
                default => $service_ensure,
              },
    name   => $service_name,
    enable => $service_enable,
  }

Thanks,
Jefffrey

Felix Frank

unread,
Jan 19, 2014, 8:39:15 PM1/19/14
to puppet...@googlegroups.com
What I like to do in situations like this:

service { 'httpd':
name => $service_name,
enable => $service_enable,
}

if $service_ensure {
Service['httpd'] { ensure => $service_ensure }
}

It looks like an override, but it does work in the same class iff the
respective properties (or parameters) are not yet declared.

Cheers,
Felix
Reply all
Reply to author
Forward
0 new messages