Antidot SAS
2012-Jan-18 10:32 UTC
[Puppet Users] Import Class vs Require vs Inherits Options
Hi everyone, I am just rediting an old post because don''t see any answer regarding this matter: ---------- Forwarded message ---------- From: Arnau Bria <arnaub...@pic.es> Date: Jun 30 2009, 5:01 pm Subject: import Class vs require vs inherits To: Puppet Users Hi all, I have a couple of "basic" questions on classes. In a class, what''s the diff between: *don''t take in count syntax, please. ---------------------------------- class class_B { package { fortune } file { dummy } } ---------------------------------- example 1) class class_A { include class_B package { foo } } example 2) class class_A { file { bogus before = > Package [foo], } package { foo require =>Class["class_B"], } } example 3) class class_A inherits class_B { package { foo } } In first example class_B will be evaluated before ALL class_A. so package fortune and dummy file will be installed/created before package foo. In second one, class_B will be evaluted ONLY when package foo. So, first bogus package, then, before foo, fortune and summy file. And on third example? same as first one? I don''t see difference between example 1 and 3. ------------- Does anyone could explain to me thoses differences? Regards, JM -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Felix Frank
2012-Jan-18 12:58 UTC
Re: [Puppet Users] Import Class vs Require vs Inherits Options
Hi, require => is largely orthogonal to the other mentioned concepts. Do require whatever must be handled first by puppet, e.g. service { "ntp": require => Package["ntp"] } so that puppet doesn''t try and start a service before its package is even installed. It will *not* automagically install package ntp, you need to give puppet an actual resource, i.e. package { "ntp": ... }. The same goes for classes: Requiring a Class[...] is god practice, but you must include it explicitly. An unincluded class cannot be required (or before''d, notified etc.) As for include vs. inherit - a good rule of thumb is *always* to include. Use inherit only for situations like this: class ntp::service { service { "ntp": enable => true } } class ntp::service::watchdog inherits ntp::service { Service { "ntp": ensure => "running" } } So selected nodes can include the inheriting class to override specific parameters of class ntp::service''s resources. Inheritance is a good way to do this and it should not be used for anything else. HTH, Felix On 01/18/2012 11:32 AM, Antidot SAS wrote:> Hi everyone, > > > I am just rediting an old post because don''t see any answer regarding > this matter: > > > > ---------- Forwarded message ---------- > From: Arnau Bria <arnaub...@pic.es <mailto:arnaub...@pic.es>> > Date: Jun 30 2009, 5:01 pm > Subject: import Class vs require vs inherits > To: Puppet Users > > > Hi all, > > I have a couple of "basic" questions on classes. > > In a class, what''s the diff between: > > *don''t take in count syntax, please. > > ---------------------------------- > > class class_B { > > package { fortune } > > file { dummy } > > } > > ---------------------------------- > > example 1) > > class class_A { > > include class_B > package { foo } > > } > > > example 2) > > class class_A { > > file { bogus > > before = > Package [foo], > > } > package { foo > > require =>Class["class_B"], > > } > > } > > > example 3) > > class class_A inherits class_B { > > package { foo } > > } > > In first example class_B will be evaluated before ALL class_A. > so package fortune and dummy file will be installed/created before > package foo. > > In second one, class_B will be evaluted ONLY when package > foo. So, first bogus package, then, before foo, fortune and summy file. > > And on third example? same as first one? > > I don''t see difference between example 1 and 3. > ------------- > > > Does anyone could explain to me thoses differences? > > Regards, > JM > > -- > You received this message because you are subscribed to the Google > Groups "Puppet Users" group. > To post to this group, send email to puppet-users@googlegroups.com. > To unsubscribe from this group, send email to > puppet-users+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/puppet-users?hl=en.-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
jcbollinger
2012-Jan-18 17:03 UTC
[Puppet Users] Re: Import Class vs Require vs Inherits Options
On Jan 18, 4:32 am, Antidot SAS <antidot...@gmail.com> wrote:> Hi everyone, > > I am just rediting an old post because don''t see any answer regarding this > matter:I observe that your thread title refers to "import", but there is no importing in the examples you asked about. I point this out only to be sure you recognize that there is a big difference between "import" and "include" -- much bigger, in fact, than the one between "include" and "inherits". As for the questions posed:> In a class, what''s the diff between: > > *don''t take in count syntax, please. > > ---------------------------------- > > class class_B { > > package { fortune } > > file { dummy } > > } > > ---------------------------------- > > example 1) > > class class_A { > > include class_B > package { foo } > > }In this case, any node that declares class_A, directly or indirectly, will also declare class_B (on account of the "include" statement). Furthermore, Puppet will ensure that class_B''s definition is evaluated before anything following the "include" line inside class_A''s definition. All of these are server-side considerations.> example 2) > > class class_A { > > file { bogus > > before = > Package [foo], > > } > package { foo > > require =>Class["class_B"], > > } > > }In this case, the Puppet agent will manage all resources declared by class_B before managing Package foo, and it will manage File bogus before it manages Package foo *provided that* the node''s manifests are compiled successfully. There is no guarantee that the manifests will compile, however, because class_A does not ensure that class_B has been evaluated before refering to it (contrary to Mr. Bria''s assertion). One way to solve that problem would be to "include" class_B, as in example (1).> example 3) > > class class_A inherits class_B { > > package { foo } > > }Functionally, this is the same as example (1), but as Felix describes, it is an inappropriate use of class inheritance. Just to muddy the waters, I''ll add that in addition to the "require" metaparameter, there is a "require" function related to both the metaparameter and to the "include" function. To illustrate its use, these two examples are thoroughly equivalent: example 4) class_A { include "class_B" package { "foo": } } # On nodes, apply class_B before class_A Class[''class_B''] -> Class[''class_A''] --- example 5) class_A { # include class_B AND apply it to nodes # before this class require "class_B" package { "foo": } } HTH, John -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Antidot SAS
2012-Jan-19 08:33 UTC
Re: [Puppet Users] Re: Import Class vs Require vs Inherits Options
Thx you very much this helps a lot. Just have a last point unclear from your example 4. As you said for example 1: "Puppet will ensure that class_B''s definition is evaluated before anything following the "include" line inside class_A''s definition" so what''s the use of declaring: Class[''class_B''] -> Class[''class_A''], isn''t it redundant? On Wed, Jan 18, 2012 at 6:03 PM, jcbollinger <John.Bollinger@stjude.org>wrote:> > > On Jan 18, 4:32 am, Antidot SAS <antidot...@gmail.com> wrote: > > Hi everyone, > > > > I am just rediting an old post because don''t see any answer regarding > this > > matter: > > > I observe that your thread title refers to "import", but there is no > importing in the examples you asked about. I point this out only to > be sure you recognize that there is a big difference between "import" > and "include" -- much bigger, in fact, than the one between "include" > and "inherits". > > As for the questions posed: > > > > In a class, what''s the diff between: > > > > *don''t take in count syntax, please. > > > > ---------------------------------- > > > > class class_B { > > > > package { fortune } > > > > file { dummy } > > > > } > > > > ---------------------------------- > > > > example 1) > > > > class class_A { > > > > include class_B > > package { foo } > > > > } > > > In this case, any node that declares class_A, directly or indirectly, > will also declare class_B (on account of the "include" statement). > Furthermore, Puppet will ensure that class_B''s definition is evaluated > before anything following the "include" line inside class_A''s > definition. All of these are server-side considerations. > > > > example 2) > > > > class class_A { > > > > file { bogus > > > > before = > Package [foo], > > > > } > > package { foo > > > > require =>Class["class_B"], > > > > } > > > > } > > > In this case, the Puppet agent will manage all resources declared by > class_B before managing Package foo, and it will manage File bogus > before it manages Package foo *provided that* the node''s manifests are > compiled successfully. There is no guarantee that the manifests will > compile, however, because class_A does not ensure that class_B has > been evaluated before refering to it (contrary to Mr. Bria''s > assertion). One way to solve that problem would be to "include" > class_B, as in example (1). > > > > example 3) > > > > class class_A inherits class_B { > > > > package { foo } > > > > } > > > Functionally, this is the same as example (1), but as Felix describes, > it is an inappropriate use of class inheritance. > > Just to muddy the waters, I''ll add that in addition to the "require" > metaparameter, there is a "require" function related to both the > metaparameter and to the "include" function. To illustrate its use, > these two examples are thoroughly equivalent: > > example 4) > > class_A { > include "class_B" > package { "foo": } > } > > # On nodes, apply class_B before class_A > Class[''class_B''] -> Class[''class_A''] > > --- > > example 5) > > class_A { > # include class_B AND apply it to nodes > # before this class > require "class_B" > package { "foo": } > } > > > HTH, > > John > > -- > You received this message because you are subscribed to the Google Groups > "Puppet Users" group. > To post to this group, send email to puppet-users@googlegroups.com. > To unsubscribe from this group, send email to > puppet-users+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/puppet-users?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
jcbollinger
2012-Jan-19 14:00 UTC
[Puppet Users] Re: Import Class vs Require vs Inherits Options
On Jan 19, 2:33 am, Antidot SAS <antidot...@gmail.com> wrote:> Thx you very much this helps a lot. > > Just have a last point unclear from your example 4. As you said for example > 1: "Puppet will ensure that class_B''s definition is evaluated before > anything following the "include" line inside class_A''s definition" so > what''s the use of declaring: Class[''class_B''] -> Class[''class_A''], isn''t it > redundant?Not at all. "Evaluating the definition" is part of parsing the manifests to compile them into a catalog for the node. This happens on the master. Resource and class relationships, such as those defined via resource chaining and the ''before'' metaparameter, affect only the order in which the Puppet agent *applies* resources to the node. It is very important to understand the distinction between evaluation / parse order on one hand, and application order on the other. The former affects only the master''s compilation of manifests into a catalog, and the latter affects only the agent''s sequence of actions. That''s what Felix was talking about when he wrote "require => is largely orthogonal to the other mentioned concepts". John -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.