override parameter within base class?

1,433 views
Skip to first unread message

Ryan Bowlby

unread,
May 30, 2012, 3:37:18 PM5/30/12
to Puppet Users
Hi All,

Is there a way to override the value of a parameter to a declared
class within my base class. My nodes use a base class that
occasionally need to be changed. Example:

class "base" {
class { "apache":
mpm => "worker",
}
..other awesomeness
}

Then in the nodes:

node "a" {
include base
}

# made up syntax
node "specialhost" {
class "special" inherits base {
Class { "apache": mpm => "prefork" }
}
}

How do I override the mpm param within the apache declaration within
the base class for "specialhost"? Is this possible and if not what are
the common workarounds?

Thanks,
Ryan

Ryan Coleman

unread,
May 30, 2012, 4:05:32 PM5/30/12
to puppet...@googlegroups.com
On Wed, May 30, 2012 at 12:37 PM, Ryan Bowlby <rbow...@gmail.com> wrote:
> Hi All,

Hi!

>
> Is there a way to override the value of a parameter to a declared
> class within my base class. My nodes use a base class that
> occasionally need to be changed. Example:

Yes, there are several ways. One common way is to inherit the class
that has the resources you want to modify into a new class and then
override the resources.

I'd suggest you learn more about that and see if it meets your needs:
http://docs.puppetlabs.com/guides/language_guide.html#resource-collections

Nick Fagerlund

unread,
May 30, 2012, 4:16:51 PM5/30/12
to puppet...@googlegroups.com
Probably use a class parameter. You're gonna want something like this, I think:

class base ($kind = "normal") {
  $mpmtype = $kind ? {
    'special' => 'prefork',
    default => 'worker',
  }
  class {'apache': mpm => $mpmtype }
}

On your normal nodes, you'd just "include base" or "class {'base':}", and on your special nodes, you'd "class {'base': kind => 'special'}".

See?

Best practice is to try and present a clean interface with your wrapper classes -- that means instead of exposing every knob you might twiddle as a class parameter, settle on a limited number of roles (like that "kind" parameter I was showing), and then use logic inside the class to change any of the relevant bits in the way that node type needs.

Gary Larizza

unread,
May 30, 2012, 4:17:19 PM5/30/12
to puppet...@googlegroups.com
On Wed, May 30, 2012 at 12:37 PM, Ryan Bowlby <rbow...@gmail.com> wrote:

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To post to this group, send email to puppet...@googlegroups.com.
To unsubscribe from this group, send email to puppet-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.


The way Ryan has described will allow you to override parameters FROM a base class inside of a called class.  You were asking about overriding parameters from inside the BASE class to a called class?  If you're getting to this level of abstraction, I'd look a data lookup mechanism like Hiera that will allow you to specify different data/parameters based on hierarchy levels of your own design.  The benefit of THAT is that you don't need to use the parameterized class syntax, you can simply do 'include base' or even 'include apache' and the parameters to the apache class would always be looked-up to determine the appropriate value per-host.  Does that make sense (or help)?

--

Gary Larizza
Professional Services Engineer
Puppet Labs

jcbollinger

unread,
May 30, 2012, 5:00:38 PM5/30/12
to Puppet Users
What you wrote is similar to what I would expect to work, which would
be this:

class special inherits base {
Class['apache'] { mpm => 'prefork' }
}

node 'specialhost' {
include 'special'
}

That's the standard syntax for subclasses overriding their parent
class's resource's parameters. In practice, I'd put the subclass
definition in its own file in a suitable module, of course.


John

Ryan Bowlby

unread,
Jun 1, 2012, 10:44:59 PM6/1/12
to Puppet Users
Thanks John, but it appears the ability to override a parent class
parameter is limited to the resources DEFINED within that class. In my
base class I am merely declaring/instantiating the apache class and
not defining it. The overriding of parameters does not appear to work
in that case.

I ended up just doing a few simple wrapper parameters as advised by
Nick. I like the Hiera idea but it will have to wait since we have so
much low hanging fruit to pick for the time being. Thanks everyone!

-Ryan

jcbollinger

unread,
Jun 4, 2012, 9:17:12 AM6/4/12
to Puppet Users


On Jun 1, 9:44 pm, Ryan Bowlby <rbowlb...@gmail.com> wrote:
> Thanks John, but it appears the ability to override a parent class
> parameter is limited to the resources DEFINED within that class.


Subclasses can override only parameters of resources *declared* by
their parent classes. And that's exactly what I suggested you try,
relying on Puppet's rather blurry distinction between classes and
resources. Specifically, class "base" declares the resource
Class['apache'], and I suggested that subclass "special" override one
of the parameters that base declares for that resource.


> In my
> base class I am merely declaring/instantiating the apache class and
> not defining it.


And that's the case for most resources. With any resource other than
a class, a subclass of the declaring class can override that
resource's parameters. Indeed, few resources (including classes) are
*defined* inside other classes at all, but that doesn't prevent their
parameters from being overridden.


> The overriding of parameters does not appear to work
> in that case.


But of course, that's the bottom line. I find it slightly surprising,
considering how much Puppetlabs has tried to make classes a special
variety of resources, but not shocking. I'm sorry it turned out to be
a red herring.


> I ended up just doing a few simple wrapper parameters as advised by
> Nick. I like the Hiera idea but it will have to wait since we have so
> much low hanging fruit to pick for the time being. Thanks everyone!


I'm glad you found something that works for you.


Cheers,

John
Reply all
Reply to author
Forward
0 new messages