Alexander Kulbiy
2013-Apr-01 18:28 UTC
[Puppet Users] Template function does not show warnings if variable not defined
Hello all, I have a problem while applying templates. I will show here a sample example while I can reproduce this problem but it also exist in more complicated configuration. I have a next manifest: class testClass { $testvar = ''test'' } class {"testClass": } file { "/home/alex/workspace/Config_cleanup/config_cleanup/test/test.out": content => template("/home/alex/workspace/Config_cleanup/config_cleanup/test/test.erb"), } And I have next template file: <%= scope["testClass::testvar2"] %> So my template must read variable testvar2 from testClass and insert it into file test.out. While I don''t have $testvar2 in my class as result I get just a clean file. The problem for me is that Puppet does not show any kind of warnings/errors/notices about this. I was digging through the documentation and was not able to find any way to enable this. Is it possible? For me this is critical because on our project it is possible that templates would be changed by one person but then applied by another and without such warnings we can have a misconfiguration of our services. Any help is greatly appreciated. Thanks in advance, Alex -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
jcbollinger
2013-Apr-02 13:50 UTC
[Puppet Users] Re: Template function does not show warnings if variable not defined
On Monday, April 1, 2013 1:28:21 PM UTC-5, Alexander Kulbiy wrote:> > Hello all, > > I have a problem while applying templates. I will show here a sample > example while I can reproduce this problem but it also exist in more > complicated configuration. > I have a next manifest: > > class testClass { > $testvar = ''test'' > } > > class {"testClass": } > > file { "/home/alex/workspace/Config_cleanup/config_cleanup/test/test.out": > content => > template("/home/alex/workspace/Config_cleanup/config_cleanup/test/test.erb"), > } > > And I have next template file: > > <%= scope["testClass::testvar2"] %> > > So my template must read variable testvar2 from testClass and insert it > into file test.out. While I don''t have $testvar2 in my class as result I > get just a clean file. > The problem for me is that Puppet does not show any kind of > warnings/errors/notices about this. I was digging through the documentation > and was not able to find any way to enable this. Is it possible? > >What you see is Puppet''s (and ERB''s and Ruby''s) standard behavior. See below.> For me this is critical because on our project it is possible that > templates would be changed by one person but then applied by another and > without such warnings we can have a misconfiguration of our services. > >The first and most important way to approach anything in this arena is to test before you put code into production. If you don''t have and enforce standard practices for doing that then you WILL produce service misconfigurations from time to time, whether by this particular vector or some other. There is nothing unique to Puppet about that, but Puppet (or any other CM system) affords you the *possibility* of testing by giving you a reliably reproducible way of applying configuration to your systems. Nevertheless, you can cause references to undefined variables to cause parse errors (or to signal in some other way) by creating a custom wrapper function that performs such a test (http://docs.puppetlabs.com/guides/custom_functions.html), and wrapping your variable references in it. Perhaps something along these lines: module Puppet::Parser::Functions newfunction(:not_nil, :type => :rvalue) do |args| Puppet::Parser::Functions.function(''fail'') # This is for Puppet 3; a different condition # is needed for Puppet 2, see issue 8707: function_fail([''Undefined value'']) if args[0].nil? args[0] endend Put that in any module and you can then use that in manifests (with ease) and in templates (with slightly less ease): in a manifest: ===class foo { include ''bar'' # Parse error if $bar::var is undefined: $var = not_nil($bar::var) } === in a template: ===<% # my_template.erb -%> <%= scope.function_not_nil(scope[''bar::var'']) %> === John -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
Alexander Kulbiy
2013-Apr-03 11:59 UTC
[Puppet Users] Re: Template function does not show warnings if variable not defined
Hello John, Thanks very much for your help. I stated to think about such solution, but I thought it might be some built-in function... While I''m going to use such function I have another question about funtions: is there any way to understand name of template from where my function is called? With this I want to provide error messages like: "Error: undefined variable #{var} in template #{template}" . Is this possible with Puppet? Thanks in advance, Alex -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users?hl=en. For more options, visit https://groups.google.com/groups/opt_out.