Mark Phillips
2011-Mar-09 14:01 UTC
[Puppet Users] Variables, extlookup and inheritance from includes
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? Thanks --Mark -- 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
2011-Mar-11 14:39 UTC
Re: [Puppet Users] Variables, extlookup and inheritance from includes
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+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Mark Phillips
2011-Mar-11 17:04 UTC
Re: [Puppet Users] Variables, extlookup and inheritance from includes
On 11 Mar 2011, at 14:39, Felix Frank wrote:> 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.Thanks Felix. I realised the error of my ways shortly after posting. What I''m trying to do, probably wrongly, is set up some ''globals'' so I don''t have to keep calling them. This might not be all that clever though. Cheers for your thoughts though, it''s appreciated. --Mark -- 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.