I use Ruby's exception handling to deal with this:
<%
begin
c_buildtype = buildtype
rescue
c_buildtype = "Unknown"
end
-%>
Buildtype = <%= c_buildtype %>
If sensible (Luke?) I can put this in the FAQ.
Regards,
Derek
-----Original Message-----
From: puppet-users-***@madstop.com
[mailto:puppet-users-***@madstop.com] On Behalf Of Luke Kanies
Sent: 19 February 2008 14:55
To: Puppet User Discussion
Subject: Re: [Puppet-users] how to check if a variable is defined (ina
template)?
Post by Dave AldenI'm sorry - I thought I had replied on #puppet that this too did not
<% if !defined? foo %>
foo is undefined, although its value is set to "<%= foo %>"
<% else %>
foo is defined
<% end %>
I have the environment variable "FACTER_FOO" defined to be "bob". I
foo is undefined, although its value is set to "bob"
So even though it thinks it is undefined, it puts the value into the output.
Of course if it truly isn't defined, I get the error "Could not find
value for 'foo' at ..." (which I would expect due to the <%= foo %>
line). I'm confused...:(
Puppet's templating support uses a ruby hack -- variables are actually
methods, not variables.
That is, Puppet uses Ruby's method_missing to notice when you call a
variable, and then it looks up variables with the same name as the
method you're using.
Thus, defined? will never actually work.
The only real way to do this is to get the variable's value, and see if
it's set to an empty string.
Enterprising individuals could add methods to the TemplateWrapper to do
this kind of test, if it were really needed.
--
Seize opportunity by the beard, for it is bald behind.
-- Bulgarian Proverb
---------------------------------------------------------------------
Luke Kanies | http://reductivelabs.com | http://madstop.com
_______________________________________________
Puppet-users mailing list
Puppet-***@madstop.com
https://mail.madstop.com/mailman/listinfo/puppet-users
------------------------------------------------------------------------
For important statutory and regulatory disclosures and more information about Barclays Capital, please visit our web site at http://www.barcap.com.
Internet communications are not secure and therefore the Barclays Group does not accept legal responsibility for the contents of this message. Although the Barclays Group operates anti-virus programmes, it does not accept responsibility for any damage whatsoever that is caused by viruses being passed. Any views or opinions presented are solely those of the author and do not necessarily represent those of the Barclays Group. Replies to this email may be monitored by the Barclays Group for operational or business reasons.
Barclays Capital is the investment banking division of Barclays Bank PLC, a company registered in England (number 1026167) with its registered office at 1 Churchill Place, London, E14 5HP. This email may relate to or be sent from other members of the Barclays Group.
------------------------------------------------------------------------