On 03/09/2011 03:01 PM, Mark Phillips wrote:
> Hi all,
> 
> I've been testing something today that I can't get to play ball the
> way I'd like. Here's the setup:
> 
> site.pp
> import "nodes"
> $extlookup_datadir = "/etc/puppet/envs/poc/extdata"
> $extlookup_precedence = ["%{fqdn}", "common"]
> 
> nodes.pp
> class core {
>     include hostinfo
>     include motd
> }
> 
> hostinfo module, init.pp
> class hostinfo {
>     $os     = extlookup("os")
>     $region = extlookup("region")
> }
> 
> module motd, init.pp
> class motd  {
>     include "motd::$operatingsystem"
> }
> 
> class motd::common {
>     file { "/etc/motd":
>         owner => root, group => sys, mode => 0644,
>         content => template("motd/motd.erb"),
>     }
> }
> 
> module motd, redhat.pp
> class motd::redhat inherits motd::common {}
> 
> External data CSV:
> testhost:extdata$ egrep '^(os|region)' testhost.domain.com.csv
> os,core-redhat-5.5.1.19-11-10.x86_64
> region,eu
> 
> Originally the hostinfo module was laid out in the same way we do all
> the modules, i.e. init.pp has parent class which 'includes' class::
> $operatingsystem and a class::common {} , then $operatingsystem.pp has
> 'class blah::redhat inherits blah::common'. But in the early part of
> the problem I realised the OS layout for 'hostinfo' was bogus, and
> dropped it for simplification.
> 
> $os and $region were constantly failing to be found, but if I took the
> extlookup statements and copy n' pasted, verbatim, into the motd
> module itself, it worked. So then I tried a run stage to see if I
> could ensure hostinfo was imported before anything else. This was
> placed in site.pp:
> 
> stage { "initialise": before => Stage[main] }
> class { "hostinfo": stage => "initialise"; }
> 
> This is the first time I've messed with run stages, so I'm not 100% I
> got that right. It ran, and with puppet master --no-daemonize --debug
> I could see it being imported. So that was a good start in my mind.
> $os and $region still failed to be found though.
> 
> So, I cut hostinfo out of nodes.pp, and put 'inherits hostinfo' into
> 'class motd'; this worked fine, the variables were found as I'd like.
> 
> How come it doesn't work with run stages, or includes? Am I missing
> something fundamental here?

*cringe*

Do not rely on dynamic scoping. Do not declare variables in one class
and use them in another. It works sometimes, it doesn't at others. While
it's quite well-defined and understandable in most cases, it's bound to
drive you crazy sooner or later.

In this case, you should note that motd and hostinfo share a parent
scope, but are otherwise unrelated. The variables are declared in
hostinfo's own scope, but don't leak into the parent scope (class core)
and sure as hell not into the motd scope.

So, don't do this ;-)

A sane approach here might be the following:

class motd::motd($os,$region) {
  file "/etc/motd":
         owner => root, group => sys, mode => 0644,
         content => template("motd/motd.$operatingsystem.erb"),
  }
}

Then instantiate the motd::motd class from wherever you want with the
appropriate info. I hope you'll manage to save yourself from the messy
include something::$somefact, but if you're not as cowardly as me, you
may as well keep using them.

HTH,
Felix

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/puppet-users?hl=en.

Reply via email to