Shawn Knight
2012-Nov-27 18:20 UTC
[Puppet Users] Help: sysctl implementation with defined types
Hi folks, Still a beginner with Puppet. I''m trying to set up a simple class for building a sysctl.conf, and I''ve hit a stumbling block. I could go with the sysctl solutions other people have written with custom ruby functions or augeas, but solving the problem I''m having might be instructive for other Puppet problems down the road, if you follow, so I''m asking what I''m doing wrong. (I''m just creating a dummy file and not even putting it in sysctl.conf while I''m learning the infrastructure, so don''t be alarmed by the goofy parameter names and values. :) First I have a modules/sysctl/manifests/init.pp which includes (some stuff omitted): class sysctl { $params = { ''mem_max'' => ''5G'' } $params[''mem_min''] = ''1G'' ## template file resource here points at an ERB template ## which loops over the keys/values of the $params hash. ## sparing you the details of the file resource for now since it works fine. :) file { ... } } So far so good! I get a file which looks like mem_max = 5G mem_min = 1G Okay. Now I try to get fancy and create a defined type that would let me add more sysctl entries. In modules/sysctl/manifests/add_param.pp: define sysctl::add_param ( $value ) { notify { "Setting $title to $value": } ## for debugging purposes $sysctl::params[$title] = $value } And then I add to the sysctl class: ## add the extra param ## add_param { ''mem_avg'': value => ''3G'' } notify { "The params are ${params}": } What happens is, I see: "Setting mem_avg to 3G The params are mem_max5Gmem_min1G" and the produced file does not contain the new parameter. So it looks like the defined type resource does indeed get instantiated, and its internal notify {} shows the right thing, but it doesn''t actually add to the $sysctl::params hash. What makes this extra-frustrating is, it doesn''t give any error message about not being able to, either -- it just silently fails. Can a defined type not modify the variables in its parent namespace? If not, why am I not getting an error message? If it can, what am I doing wrong? The use case for the whole defined type mess is, supposing later I want to do something like this, to set sysctl parameters specific to a given software class. class oracle { sysctl::add_param{ ''oracle_wants_lots_of_ram'': value => ''1'' } } I''m perfectly fine with being told "your overall use case needs hiera" or something (which will probably have me coming back with more questions), but I''ll particularly appreciate knowing why the thing I''m actually trying isn''t working, as I''m still learning the basics. :) Thanks! BTW, this is with Puppet open source 3.0.0. Cheers, --Shawn -- 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/-/CsGGfCkcUVIJ. 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.
Fiddyspence
2012-Nov-27 19:06 UTC
[Puppet Users] Re: Help: sysctl implementation with defined types
Try http://forge.puppetlabs.com/fiddyspence/sysctl -- 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/-/rZPUaBBey3gJ. 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.
Shawn Knight
2012-Nov-27 19:28 UTC
[Puppet Users] Re: Help: sysctl implementation with defined types
Appreciated, certainly. But again, for the sake of learning Puppet better, I''d like to know why my code doesn''t work. :) On Tuesday, November 27, 2012 2:06:17 PM UTC-5, Fiddyspence wrote:> > Try http://forge.puppetlabs.com/fiddyspence/sysctl-- 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/-/O5OLIjytr3IJ. 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
2012-Nov-27 20:46 UTC
[Puppet Users] Re: Help: sysctl implementation with defined types
On Tuesday, November 27, 2012 12:20:02 PM UTC-6, Shawn Knight wrote:> > Hi folks, > > Still a beginner with Puppet. I''m trying to set up a simple class for > building a sysctl.conf, and I''ve hit a stumbling block. I could go with the > sysctl solutions other people have written with custom ruby functions or > augeas, but solving the problem I''m having might be instructive for other > Puppet problems down the road, if you follow, so I''m asking what I''m doing > wrong. > > (I''m just creating a dummy file and not even putting it in sysctl.conf > while I''m learning the infrastructure, so don''t be alarmed by the goofy > parameter names and values. :) > > First I have a modules/sysctl/manifests/init.pp which includes (some stuff > omitted): > > class sysctl { > > $params = { ''mem_max'' => ''5G'' } > $params[''mem_min''] = ''1G'' > > ## template file resource here points at an ERB template > ## which loops over the keys/values of the $params hash. > ## sparing you the details of the file resource for now since it works > fine. :) > file { ... } > > } > > So far so good! I get a file which looks like > > mem_max = 5G > mem_min = 1G > > Okay. Now I try to get fancy and create a defined type that would let me > add more sysctl entries. > > In modules/sysctl/manifests/add_param.pp: > > define sysctl::add_param ( $value ) { > > notify { "Setting $title to $value": } ## for debugging purposes > > $sysctl::params[$title] = $value > > } > > And then I add to the sysctl class: > > ## add the extra param > ## > add_param { ''mem_avg'': value => ''3G'' } > notify { "The params are ${params}": } > > What happens is, I see: > "Setting mem_avg to 3G > The params are mem_max5Gmem_min1G" > > and the produced file does not contain the new parameter. > > So it looks like the defined type resource does indeed get instantiated, > and its internal notify {} shows the right thing, but it doesn''t actually > add to the $sysctl::params hash. What makes this extra-frustrating is, it > doesn''t give any error message about not being able to, either -- it just > silently fails. > > Can a defined type not modify the variables in its parent namespace? If > not, why am I not getting an error message? If it can, what am I doing > wrong? > > The use case for the whole defined type mess is, supposing later I want to > do something like this, to set sysctl parameters specific to a given > software class. > > class oracle { > sysctl::add_param{ ''oracle_wants_lots_of_ram'': value => ''1'' } > } > > I''m perfectly fine with being told "your overall use case needs hiera" or > something (which will probably have me coming back with more questions), > but I''ll particularly appreciate knowing why the thing I''m actually trying > isn''t working, as I''m still learning the basics. :) Thanks! > >An important Puppet fundamental is that you cannot modify a variable''s value once it is set (thus the term "variable" is a bit misleading). In fact, I''m surprised that you''re able to assign to $params[''mem_min'']; I wouldn''t have expected that to be possible, and I''m inclined to call it a bug. Another important fundamental is that variables can be set only by the class (or defined type or node) that owns them, or at top scope for global variables. This is crucial, for without it there would be many cases where the value of a variable would be ambiguous. Remember always that Puppet DSL is not a programming language (in which primarily you describe data transformations and flows) but rather a declarative language (in which you express statements of fact). It does not lend itself well to constructing data iteratively or cooperatively, because that tends to require making statements that are not wholly true and complete. There are ways around that, but you need to adopt the right mindset. Also, I''m rather surprised that Puppet fails silently for you. Generally speaking, Puppet will report a parse error if your manifest tries to do something it shouldn''t, such as assign to a foreign variable or to a variable that already has a value. Are you sure you don''t see such a report, either in the client''s log or in the server''s? Don''t be fooled if the Puppet agent applies a cached catalog in the event that the master fails to compile a fresh one. John -- 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/-/ex4LwwFqt-AJ. 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.