Walter Heck
2011-Dec-21 21:40 UTC
[Puppet Users] Best practice for module params in foo::params
Hello, a related question here on this list sparked me to write this question, which has been bugging me for a bit now: I use the following pattern inside my modules quite abit: class foo::params{ $bar = $::foo_bar ? { '''' => ''somedefaultvalue'', default => $::foo_bar } } Then I can refer to the variable from my module by $foo::params::bar, which is quite nice. The problem is, I want to be able to set the value of $::foo_bar in a thrid module in my services folder, but that is not possible because $::foo_bar needs to be top-scope. I don''t want to make module foo dirty by adding $s_yadda::foo_bar but I don''t see a better way, can anyone enlighten me as to how they solve this problem? The only way I can think of is class parameters, but that is a lot less clean. I wish I could write to $::foo_bar from outside the top scope, even though that is kind of dirty too :) . cheers, -- Walter Heck -- follow @walterheck on twitter to see what I''m up to! -- Check out my new startup: Server Monitoring as a Service @ http://tribily.com Follow @tribily on Twitter and/or ''Like'' our Facebook page at http://www.facebook.com/tribily -- 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.
jcbollinger
2011-Dec-22 13:56 UTC
[Puppet Users] Re: Best practice for module params in foo::params
On Dec 21, 3:40 pm, Walter Heck <walterh...@gmail.com> wrote:> Hello, > > a related question here on this list sparked me to write this question, > which has been bugging me for a bit now: > > I use the following pattern inside my modules quite abit: > > class foo::params{ > > $bar = $::foo_bar ? { > '''' => ''somedefaultvalue'', > default => $::foo_bar > } > > } > > Then I can refer to the variable from my module by $foo::params::bar, which > is quite nice. > > The problem is, I want to be able to set the value of $::foo_bar in a thrid > module in my services folder, but that is not possible because $::foo_bar > needs to be top-scope. I don''t want to make module foo dirty by adding > $s_yadda::foo_bar but I don''t see a better way, can anyone enlighten me as > to how they solve this problem? The only way I can think of is class > parameters, but that is a lot less clean. > > I wish I could write to $::foo_bar from outside the top scope, even though > that is kind of dirty too :) .You''re exactly right: the behavior you want is dirty, too. Much dirtier, in fact, than one module directly using class variables of one of another module''s classes. Parameterizing foo::params would not be much better, because it would still leave that class dependent on another module''s variables (in that it needs them to be passed to it as parameters to achieve your desired result). Parameterizing foo::params would also make it harder to use, inasmuch as you would need to be much more careful about how and where it is declared than you otherwise need to be. The bottom line is that it doesn''t matter how you communicate the values of one class''s variables to another class; any way you do it produces a dependency of the variable consumer on the variable provider. To avoid such dependencies, you could consider having foo::params use external data instead of Puppet variables of any scope. Puppet has long provided the extlookup() function for this purpose, and there is now hiera as well. You might even find that external data could *replace* foo::params in some cases rather than merely supporting it. John -- 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.
Walter Heck
2011-Dec-22 14:54 UTC
Re: [Puppet Users] Re: Best practice for module params in foo::params
Hmm, that''s the second time in two weeks someone mentions hiera as a solution for a problem that cannot easily be solved inside puppet, I guess it''s time to go check it out :) On Thu, Dec 22, 2011 at 15:56, jcbollinger <John.Bollinger@stjude.org>wrote:> > > On Dec 21, 3:40 pm, Walter Heck <walterh...@gmail.com> wrote: > > Hello, > > > > a related question here on this list sparked me to write this question, > > which has been bugging me for a bit now: > > > > I use the following pattern inside my modules quite abit: > > > > class foo::params{ > > > > $bar = $::foo_bar ? { > > '''' => ''somedefaultvalue'', > > default => $::foo_bar > > } > > > > } > > > > Then I can refer to the variable from my module by $foo::params::bar, > which > > is quite nice. > > > > The problem is, I want to be able to set the value of $::foo_bar in a > thrid > > module in my services folder, but that is not possible because $::foo_bar > > needs to be top-scope. I don''t want to make module foo dirty by adding > > $s_yadda::foo_bar but I don''t see a better way, can anyone enlighten me > as > > to how they solve this problem? The only way I can think of is class > > parameters, but that is a lot less clean. > > > > I wish I could write to $::foo_bar from outside the top scope, even > though > > that is kind of dirty too :) . > > > You''re exactly right: the behavior you want is dirty, too. Much > dirtier, in fact, than one module directly using class variables of > one of another module''s classes. > > Parameterizing foo::params would not be much better, because it would > still leave that class dependent on another module''s variables (in > that it needs them to be passed to it as parameters to achieve your > desired result). Parameterizing foo::params would also make it harder > to use, inasmuch as you would need to be much more careful about how > and where it is declared than you otherwise need to be. > > The bottom line is that it doesn''t matter how you communicate the > values of one class''s variables to another class; any way you do it > produces a dependency of the variable consumer on the variable > provider. > > To avoid such dependencies, you could consider having foo::params use > external data instead of Puppet variables of any scope. Puppet has > long provided the extlookup() function for this purpose, and there is > now hiera as well. You might even find that external data could > *replace* foo::params in some cases rather than merely supporting it. > > > John > > -- > 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. > >-- Walter Heck -- follow @walterheck on twitter to see what I''m up to! -- Check out my new startup: Server Monitoring as a Service @ http://tribily.com Follow @tribily on Twitter and/or ''Like'' our Facebook page at http://www.facebook.com/tribily -- 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.
Nigel Kersten
2011-Dec-22 21:25 UTC
Re: [Puppet Users] Re: Best practice for module params in foo::params
On Thu, Dec 22, 2011 at 6:54 AM, Walter Heck <walterheck@gmail.com> wrote:> Hmm, that''s the second time in two weeks someone mentions hiera as a > solution for a problem that cannot easily be solved inside puppet, I guess > it''s time to go check it out :)We''re committed to Hiera, and are looking to integrate it as a first class citizen by the next major release. Totally go check it out :) http://projects.puppetlabs.com/projects/hiera -- 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.
Alessandro Franceschi
2011-Dec-26 13:53 UTC
Re: [Puppet Users] Re: Best practice for module params in foo::params
Note that you can use hiera to gather and define variables at top level scope and keep the usage of ::vars on your params class. This has the advantage that if you will ever need to change the method you use to give values to the top scope variable, you just have to change the references at top scope, and not in your modules. My2c -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/z4giwhowW5MJ. 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.