Using puppet 0.23.2, I have a template where I wish to do something
like this:
<% if defined? some_variable %>
....
<% end %>
Ideally I''d like to tell these three cases apart:
a) defined to a non-empty value
b) defined to an empty value
c) undefined
However case c) causes problems - in
templatewrapper.rb::method_missing it causes an error to be thrown.
I''ve changed my copy so that it merely warns instead and returns nil.
Interestingly the defined? check _never_ succeeeds - it always returns
false :( I''ve done some google to see if anyone else has been doing
optional bits like this but couldn''t see any which makes me suspicious.
Is this a reasonable thing to expect? If the "correct" approach is to
define all variables in the base class and then override it like this:
class test {
$var1 = "foo"
$var2 = ""
file { "/tmp/test": content => template("test") }
exec { "/bin/echo $mail_test1 , $mail_test2": }
node foo {
class test2 inherits test {
$var1 = ""
$var2 = "bar"
}
include test2
}
then I just get "foo" and "" (both in the file and the
exec), which
strikes me as a bit odd - in particular it seems to differ from the
variable scoping example in the Language Tutorial. This is in the
middle of my config, but I''ve split this out.
I even tried doing something like this in the class:
$some_variable_set = $some_variable ? {
nil => "NIL",
"" => "blank",
undefined => "undef",
default => "default",
}
But had about as much success as I thought I might!
Many thanks,
Adrian
Adrian Bridgett wrote:> Using puppet 0.23.2, I have a template where I wish to do something > like this: > > <% if defined? some_variable %> > .... > <% end %> > > Ideally I''d like to tell these three cases apart: > a) defined to a non-empty value > b) defined to an empty value > c) undefined > > However case c) causes problems - in > templatewrapper.rb::method_missing it causes an error to be thrown. > I''ve changed my copy so that it merely warns instead and returns nil. > > Interestingly the defined? check _never_ succeeeds - it always returns > false :( I''ve done some google to see if anyone else has been doing > optional bits like this but couldn''t see any which makes me suspicious. > > Is this a reasonable thing to expect? If the "correct" approach is to > define all variables in the base class and then override it like this: > class test { > $var1 = "foo" > $var2 = "" > file { "/tmp/test": content => template("test") } > exec { "/bin/echo $mail_test1 , $mail_test2": } > node foo { > class test2 inherits test { > $var1 = "" > $var2 = "bar" > } > include test2 > } > > then I just get "foo" and "" (both in the file and the exec), which > strikes me as a bit odd - in particular it seems to differ from the > variable scoping example in the Language Tutorial. This is in the > middle of my config, but I''ve split this out. > > I even tried doing something like this in the class: > > $some_variable_set = $some_variable ? { > nil => "NIL", > "" => "blank", > undefined => "undef", > default => "default", > } > > But had about as much success as I thought I might! > > Many thanks, >Variables are immutable, and I don''t think you can override them because the base class has already been evaluated. I''d suggest using a define instead of a class and pass these values as parameters. -- Russell A. Jackson <raj@csub.edu> Network Analyst California State University, Bakersfield The difference between the right word and the almost right word is the difference between lightning and the lightning bug. -- Mark Twain _______________________________________________ Puppet-users mailing list Puppet-users@madstop.com https://mail.madstop.com/mailman/listinfo/puppet-users
On Thu, Sep 13, 2007 at 11:08:11 -0700 (-0700), Russell Jackson wrote:> Variables are immutable, and I don''t think you can override them because the base class > has already been evaluated. I''d suggest using a define instead of a class and pass these > values as parameters.Thanks. The language tutorial is a bit confusing on this. It says: The primary difference is that you cannot change the value of a variable within a single scope, because that would rely on file order to determine the value of the variable. And explains that this is invalid: $user = root ... $user = fred But says that this _is_ valid: $test = foo class myclass { (NB: I put a copy of $test _here_) .. } class other { $test = other include myclass } I was hoping that puppet would automagically override that, oh well! Thanks, Adrian -- Email: adrian@smop.co.uk -*- GPG key available on public key servers Debian GNU/Linux - the maintainable distribution -*- www.debian.org
FYI what I''ve done is something like this:
class bind::top {
# this effectively sets $acl to "" if it is currently unset
$acl = $acl
file { "named.conf.options":
content => template {"named.conf.options"},
}
}
class bind::cache inherits bind::top {
...
}
class bind::slave inherits bind::top {
...
}
node foo {
$acl = "set to something"
include bind::slave
}
node bar {
# $acl not set
include bind::cache
}
With the template saying:
<% if $acl != "" %>
... (produces ACL list)
<% end %>
This doesn''t seem right, but I couldn''t find a nicer way :-(
The ''!""'' bit is needed since even if $acl was set
to "" then the code would
be executed. As mentioned previously, defined? doesn''t work (works
fine in plain ERB FWIW). The "$acl=$acl" stops puppet from dying with
"cannot find $acl" (which removes the need for the change to
templatewrapper.rb).
I could not use definitions as you suggested as it would stop file
overrides (e.g. of named.conf.local) from occuring.
Adrian
--
Email: adrian@smop.co.uk -*- GPG key available on public key servers
Debian GNU/Linux - the maintainable distribution -*- www.debian.org