vagn scott
2011-Aug-02 07:26 UTC
[Puppet Users] prep for 2.8: default values and overrides
prep for 2.8: default values and overrides I was very happy, some months ago, when I worked out how to separate configuration from code in puppet. The scheme even allowed for specialization. The style was something like this: cat 04_default.pp #! /usr/bin/puppet apply node default { class myfoo1 inherits foo::default { $foo_param1 = "something else" # call 1 specialization $foo_param3 = "dull and dreary" # change the default foo { "call 1": } } include myfoo1 class myfoo2 inherits foo::default { $foo_param2 = "anything else" # call 2 specialization $foo_param3 = "dull and dreary" # change the default again! foo { "call 2": } } include myfoo2 } class foo::default { $foo_param1 = "something" $foo_param2 = "anything" $foo_param3 = "dull" $foo_param4 = "duller" } define foo() { notice("foo_param1 = ${foo_param1}") notice("foo_param2 = ${foo_param2}") notice("foo_param3 = ${foo_param3}") notice("foo_param4 = ${foo_param4}") } and the output with 2.6.2: ./04_default.pp notice: Scope(Foo[call 1]): foo_param1 = something else notice: Scope(Foo[call 1]): foo_param2 = anything notice: Scope(Foo[call 1]): foo_param3 = dull and dreary notice: Scope(Foo[call 1]): foo_param4 = duller notice: Scope(Foo[call 2]): foo_param1 = something notice: Scope(Foo[call 2]): foo_param2 = anything else notice: Scope(Foo[call 2]): foo_param3 = dull and dreary notice: Scope(Foo[call 2]): foo_param4 = duller But, with 2.7.2rc2 it doesn''t work. ./04_default.pp Could not parse for environment production: Classes, definitions, and nodes may only appear at toplevel or inside other classes at /u0/home/vagn/puppet-nika/patterns/04_default.pp:5 on node nika.agawamtech.com Even if I moved the classes out of the node the scheme would still fail in 2.8 and later because it relies on dynamic scoping. The following works in both 2.6.2 and 2.7.2. It is both a shorter and a cleaner expression of the idea. cat 05_default.pp #! /usr/bin/puppet apply node default { class { "foo::default": foo_param3 => "dull and dreary", } foo { "call 1": foo_param1 => "something else", } foo { "call 2": foo_param2 => "anything else", } } class foo::default ( $foo_param3 = "dull", $foo_param4 = "duller" ) { # conditionals and composites would go here ... } define foo( $foo_param1 = "something", $foo_param2 = "anything" ) { notice("foo_param1 = ${foo_param1}") notice("foo_param2 = ${foo_param2}") notice("foo_param3 = ${foo::default::foo_param3}") notice("foo_param4 = ${foo::default::foo_param4}") } Note that foo::default MUST be instantiated for there to be any values for foo() to reference. Even if no overrides are provided you have to explicitly put class { "foo::default": } # required somewhere in your manifests. Three observations - most of the work is done in the argument lists - arguments can have defaults - we only have to express the specializations As always, comments and corrections welcome. -- vagn -- 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.
Justin Lambert
2011-Aug-04 04:03 UTC
Re: [Puppet Users] prep for 2.8: default values and overrides
Thanks for these - this is very helpful. On Tue, Aug 2, 2011 at 1:26 AM, vagn scott <vagnscott@gmail.com> wrote:> ** > prep for 2.8: default values and overrides > > I was very happy, some months ago, > when I worked out how to separate > configuration from code in puppet. > > The scheme even allowed for specialization. > > The style was something like this: > > cat 04_default.pp > #! /usr/bin/puppet apply > > node default { > > class myfoo1 inherits foo::default { > $foo_param1 = "something else" # call 1 > specialization > $foo_param3 = "dull and dreary" # change the > default > foo { "call 1": } > } > include myfoo1 > > class myfoo2 inherits foo::default { > $foo_param2 = "anything else" # call 2 > specialization > $foo_param3 = "dull and dreary" # change the > default again! > foo { "call 2": } > } > include myfoo2 > > > } > > class foo::default { > $foo_param1 = "something" > $foo_param2 = "anything" > $foo_param3 = "dull" > $foo_param4 = "duller" > } > > define foo() { > notice("foo_param1 = ${foo_param1}") > notice("foo_param2 = ${foo_param2}") > notice("foo_param3 = ${foo_param3}") > notice("foo_param4 = ${foo_param4}") > } > > and the output with 2.6.2: > > ./04_default.pp > notice: Scope(Foo[call 1]): foo_param1 = something else > notice: Scope(Foo[call 1]): foo_param2 = anything > notice: Scope(Foo[call 1]): foo_param3 = dull and dreary > notice: Scope(Foo[call 1]): foo_param4 = duller > notice: Scope(Foo[call 2]): foo_param1 = something > notice: Scope(Foo[call 2]): foo_param2 = anything else > notice: Scope(Foo[call 2]): foo_param3 = dull and dreary > notice: Scope(Foo[call 2]): foo_param4 = duller > > > But, with 2.7.2rc2 it doesn''t work. > > ./04_default.pp > Could not parse for environment production: Classes, definitions, > and nodes may only appear at toplevel or inside other classes > at /u0/home/vagn/puppet-nika/patterns/04_default.pp:5 on node > nika.agawamtech.com > > Even if I moved the classes out of the node the > scheme would still fail in 2.8 and later because it > relies on dynamic scoping. > > The following works in both 2.6.2 and 2.7.2. > It is both a shorter and a cleaner > expression of the idea. > > cat 05_default.pp > #! /usr/bin/puppet apply > > node default { > > class { "foo::default": foo_param3 => "dull and dreary", } > foo { "call 1": foo_param1 => "something else", } > foo { "call 2": foo_param2 => "anything else", } > > } > > class foo::default ( > $foo_param3 = "dull", > $foo_param4 = "duller" > ) { > # conditionals and composites would go here ... > } > > define foo( > $foo_param1 = "something", > $foo_param2 = "anything" > ) { > notice("foo_param1 = ${foo_param1}") > notice("foo_param2 = ${foo_param2}") > notice("foo_param3 = ${foo::default::foo_param3}") > notice("foo_param4 = ${foo::default::foo_param4}") > } > > Note that foo::default MUST be instantiated for there to be > any values for foo() to reference. Even if no overrides > are provided you have to explicitly put > > class { "foo::default": } # required > > somewhere in your manifests. > > Three observations > - most of the work is done in the argument lists > - arguments can have defaults > - we only have to express the specializations > > As always, comments and corrections welcome. > > -- > vagn > > > -- > 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.