Inherit two classes?

465 views
Skip to first unread message

JamieC

unread,
Mar 18, 2013, 3:48:11 PM3/18/13
to puppet...@googlegroups.com
Hello,

I have a class called website, which requires both httpd and mysqld classes. Without defining it within the site.pp (node section). Is it possible to define that "website" requires these additions? Such as;

class website inherits httpd, mysqld {

...



Matthew Burgess

unread,
Mar 18, 2013, 4:34:18 PM3/18/13
to puppet...@googlegroups.com
On Mon, Mar 18, 2013 at 7:48 PM, JamieC <jamiecr...@googlemail.com> wrote:

> Is it possible to
> define that "website" requires these additions? Such as;
>
> class website inherits httpd, mysqld {
>
> ...
>
> }

Yes. Try:

class website {
...
require => Class['httpd', 'mysqld']
}

I personally prefer more fine-grained dependencies though:

class website {
...
require => Service['httpd', 'mysqld']
}

The idea here is that you don't require the class itself, but require
some resource declared in that class. I've assumed here that each of
your classes declares a service for their respective daemon.

See also http://docs.puppetlabs.com/puppet/3/reference/lang_relationships.html#syntax

Regards,

Matt.

joe

unread,
Mar 19, 2013, 12:08:59 AM3/19/13
to puppet...@googlegroups.com
Better practice is to expose the functionality of those classes through defines or parameterized classes.

For instance, use a define like apache::vhost in the puppetlabs/apache module:
https://github.com/puppetlabs/puppetlabs-apache/blob/master/manifests/vhost.pp

That way, all you have to do is use the define (no need to include or require the apache class at all), and all the relationships are handled internally in the apache module.

Similarly, a define that allows you to create a database instead of a class that just installs mysql.

This has the benefit of being reusable easily for different purposes and it also keeps you from having to have internal knowledge of the apache and mysql modules to make use of them. Otherwise, you still have to know *how* things are done in the apache and mysql modules, when really you just want a website or a database.

jcbollinger

unread,
Mar 19, 2013, 11:07:17 AM3/19/13
to puppet...@googlegroups.com

You have a completely wrong idea of what class inheritance is for in Puppet.  Its appropriate uses are in fact very narrow.  The natural and idiomatic expression of what you describe is this:

class website {
  include 'httpd'
  include 'mysqld'
  ...
}

That approach would be better form even if class website needed only one other class to be applied.

Note also that that does not in itself say anything about the order in which resources are applied to clients; rather, it says that all nodes to which the 'website' class is assigned also have the 'httpd' and 'mysqld' classes assigned to them.  If you need ordering relationships among these then there are additional steps you can take.

You certainly do not need a define to express the concept you asked about, although you can do it that way, too.  Whether a defined type instance or a class is more appropriate for your particular case would involve an analysis of your use of the class, but the fact that you can do what you want with a class at all is a strong argument for a class being the right thing to use.


John

jcbollinger

unread,
Mar 19, 2013, 11:14:48 AM3/19/13
to puppet...@googlegroups.com


On Monday, March 18, 2013 3:34:18 PM UTC-5, Matthew Burgess wrote:

Yes.  Try:

class website {
...
require => Class['httpd', 'mysqld']
}


Nope.  You are confusing separate concepts and separate language structures.  The OP is defining his class, not declaring it.  The 'require' metaparameter would be applicable to a parameterized-style class declaration: class { 'website': require => Class['httpd', 'mysqld'] }.  But that wouldn't say what the OP wants to say.  Specifically, it does not (itself) say that nodes to which class 'website' is assigned also get classes 'httpd' and 'mysqld'; instead, it says that latter two classes -- which are assumed to have been assigned somehow -- should be applied to the node before class 'website' is applied.


John

Reply all
Reply to author
Forward
0 new messages