FAQ

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.

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 2 of 3 | next ›
Discussion Overview
grouppuppet-users @
categoriespuppet
postedApr 1, '13 at 11:19p
activeApr 3, '13 at 11:59a
posts3
users2
websitepuppetlabs.com

2 users in discussion

Alexander Kulbiy: 2 posts Jcbollinger: 1 post

People

Translate

site design / logo © 2022 Grokbase