Darren Worrall
2010-Sep-28 16:01 UTC
[Puppet Users] Variables with class inheritance and templates
With a manifest like so: class base { $myvar = ''1234'' file { ''/tmp/foo'': content => template(''test.erb'') } } class newbase inherits base { $myvar = ''5678'' } node default { include newbase } And test.erb with <%= myvar %> /tmp/foo contains 1234, when I would expect it to contain 5678. Is this a bug, or a limitation of class inheritance? (ie, base classes are evaluated completely, and subclasses are only allowed to modify things after the fact?) Cheers :) -- 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.
Tony G.
2010-Sep-28 16:33 UTC
Re: [Puppet Users] Variables with class inheritance and templates
Darren, On Tue, Sep 28, 2010 at 11:01 AM, Darren Worrall <iwebdarren@gmail.com>wrote:> With a manifest like so: > > class base { > $myvar = ''1234'' > file { > ''/tmp/foo'': > content => template(''test.erb'') > } > } > > class newbase inherits base { > $myvar = ''5678'' > } > > node default { > include newbase > } > > And test.erb with <%= myvar %> > > /tmp/foo contains 1234, when I would expect it to contain 5678. > > Is this a bug, or a limitation of class inheritance? (ie, base classes > are evaluated completely, and subclasses are only allowed to modify > things after the fact?) > > Cheers :) >Is the way scoping works, you can take more complete explanation here http://projects.puppetlabs.com/projects/puppet/wiki/Frequently_Asked_Questions#Class+Inheritance+and+Variable+Scope Basically the value of the inner scope is the one being used and not overridden as one might expect.> -- > 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<puppet-users%2Bunsubscribe@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/puppet-users?hl=en. > >-- Tony http://blog.tonyskapunk.net -- 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.
Bruce Richardson
2010-Sep-28 17:36 UTC
Re: [Puppet Users] Variables with class inheritance and templates
On Tue, Sep 28, 2010 at 09:01:25AM -0700, Darren Worrall wrote:> > Is this a bug, or a limitation of class inheritance? (ie, base classes > are evaluated completely, and subclasses are only allowed to modify > things after the fact?) >They''re not classes, they shouldn''t have been called classes, it only upsets people who understand OO languages and make intelligent assumptions based on this. The only things you can really override in a subclass are properties of resources defined in the superclass. This is almost the only thing that class inheritance is useful for (not quite, but the points are quite subtle and you need to have banged your head on some of Puppet''s walls before you can appreciate them). Don''t set variables to explicit variables inside a class unless you really know what you are doing. For the most part, set variables at manifest or node level and include your classes at the appropriate level. If you get your head around the way this works, some of it is very powerful and elegant. But whatever the party line, some of it is also inelegant and hard to work with. I tend to use node inheritance trees for defining variables at the appropriate level and class heirarchies to define function. Then I include the appropriate high-level classes in the right nodes. With care, you can minimise the verbosity in node definitions but it is still noisier than it should be, because of the inheritance model. Look around at the way most people write their puppet configs and you''ll find node definitions littered at the top level with all the classes a node needs, because it isn''t safe to include them earlier if you''re going to want to override values for a subset of nodes. This makes node configuration repetitive where it should be DRY and a potential source of error. Still, puppet''s the best option around. -- Bruce The ice-caps are melting, tra-la-la-la. All the world is drowning, tra-la-la-la-la. -- Tiny Tim. -- 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.