puppet parser order random

46 views
Skip to first unread message

巨海录

unread,
Dec 10, 2014, 10:24:44 PM12/10/14
to puppet...@googlegroups.com
# ntp/manifests/init.pp
class ntp (
  $var_01 = '/etc/ntp.conf',
  $var_02 = 'ntp/ntp.conf.erb',
) {
  include ::ntp::params
  include ::ntp:install
}

ntp/manifests/params.pp
class ntp::params {
  $var_03 = 'no.3'
  $var_o4 = 'no.4'
}

ntp/manifests/install.pp
class ntp::install {
  notify {"var_01 == ${ntp::var01}": }
  notify {"var_01 == ${ntp::var02}": }
  notify {"var_03 == ${ntp::params::var_03}": }
  notify {"var_04 == ${ntp::params::var_04}": }
}

# site.pp
include ntp

and i run this cmmand:

puppet apply site.pp

how puppet program parser these code in order?

because sometime i can't get the correct value of var_03 and var_04.  and they would all be nil.

Martin Alfke

unread,
Dec 11, 2014, 3:24:11 AM12/11/14
to puppet...@googlegroups.com
You need to set an dependency for ntp::params and ntp::install to be processed in strict order.
e.g.
Class[‘::ntp::params’] -> Class[‘::ntp::install’]

hth,

Martin
> --
> 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/1178e436-af54-4381-bfc9-ad96ea1cedb4%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

jcbollinger

unread,
Dec 11, 2014, 8:56:36 AM12/11/14
to puppet...@googlegroups.com


On Thursday, December 11, 2014 2:24:11 AM UTC-6, Martin Alfke wrote:
You need to set an dependency for ntp::params and ntp::install to be processed in strict order.
e.g.
Class[‘::ntp::params’] -> Class[‘::ntp::install’]



NO.

This is an unfortunately common and persistent misconception.  The chaining operators and the relationship metaparameters influence the order in which classes and resources are applied on the target machine.  They have NO EFFECT WHATSOEVER on the order in which declarations are evaluated during catalog building.


John

Felix Frank

unread,
Dec 11, 2014, 9:22:50 AM12/11/14
to puppet...@googlegroups.com
On 12/11/2014 04:24 AM, 巨海录 wrote:
>
> because sometime i can't get the correct value of var_03 and var_04.
> and they would all be nil.

Weird. Should not typically happen, but on the other hand, relying on
that is bound to give you head aches.

You should

include ntp::params

in any scope that needs to have access to this class's variables.

Felix

jcbollinger

unread,
Dec 11, 2014, 9:25:02 AM12/11/14
to puppet...@googlegroups.com


On Wednesday, December 10, 2014 9:24:44 PM UTC-6, 巨海录 wrote:
# ntp/manifests/init.pp
class ntp (
  $var_01 = '/etc/ntp.conf',
  $var_02 = 'ntp/ntp.conf.erb',
) {
  include ::ntp::params
  include ::ntp:install
}



Is "::ntp:install" just a typo?  It looks like it should be "::ntp::install" (two colons between "ntp" and "install").

 
ntp/manifests/params.pp
class ntp::params {
  $var_03 = 'no.3'
  $var_o4 = 'no.4'
}

ntp/manifests/install.pp
class ntp::install {
  notify {"var_01 == ${ntp::var01}": }
  notify {"var_01 == ${ntp::var02}": }
  notify {"var_03 == ${ntp::params::var_03}": }
  notify {"var_04 == ${ntp::params::var_04}": }
}

# site.pp
include ntp

and i run this cmmand:

puppet apply site.pp

how puppet program parser these code in order?

because sometime i can't get the correct value of var_03 and var_04.  and they would all be nil.


With what you have actually written (modulo the typo I called out) you should never see the values of $ntp::params::var_03 or $ntp::params::var_04 not yet defined in ntp::install.  However, small changes in your manifests or in the starting manifest you apply could make a difference.  For example, reversing the order of the 'include' calls in class ntp would result in the variables not yet being defined when class ntp::install is evaluated (when starting by evaluating class 'ntp').  Either way, starting the evaluation at class 'ntp::install' will result in none of those variables having been defined.

There are really two different cases here:

Case 1:  Since class 'ntp::install' depends on variables of class 'ntp::params', it is wise for 'ntp::install' to start by 'include'ing 'ntp::params'.  It is safe for both 'ntp' and 'ntp::install' to do that.  If that is done then class 'ntp::install' will always see initialized values for the variables of class 'ntp::params'; otherwise, it relies on 'ntp::params' to have been evaluated at some other class's direction before 'ntp::install' is evaluated.

Case 2: The variables of class 'ntp' are different because class 'ntp::install' is declared by class 'ntp'.  It is unwise, and may not do as you expect, for class 'ntp::install' to create an evaluation-dependency loop by declaring class 'ntp'.  Puppet would not loop infinitely, but you could get unexpected results.  If 'ntp::install' must depend on variables (including class parameters) of class 'ntp', and class 'ntp' must declare class 'ntp::install', then the only good solution is to document that class 'ntp::install' is an internal class that must not be applied directly or declared by classes outside its module.  Inside the module, it probably should be declared only by class 'ntp', but another class that can be certain that class 'ntp' has already been evaluated could conceivably declare 'ntp::install', too.

With that said, if you want all the gory details then I don't know any better explanation than Henrik's.


John

Martin Alfke

unread,
Dec 11, 2014, 9:26:23 AM12/11/14
to puppet...@googlegroups.com
Argh. Yes. You are absolutely right.
Sorry.

巨海录

unread,
Dec 12, 2014, 5:00:06 AM12/12/14
to puppet...@googlegroups.com
i have to read  Henrik's carefully, and this is what i want to know, thanks you very much!


--
You received this message because you are subscribed to a topic in the Google Groups "Puppet Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/puppet-users/oh27fFB8-Ck/unsubscribe.
To unsubscribe from this group and all its topics, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/BA76D06D-0AA8-49CD-82E2-1C70CE013657%40gmail.com.

巨海录

unread,
Dec 12, 2014, 5:04:43 AM12/12/14
to puppet...@googlegroups.com
Is "::ntp:install" just a typo?  It looks like it should be "::ntp::install" (two colons between "ntp" and "install").

yes, it is a typo.
Reply all
Reply to author
Forward
0 new messages