Hi, I''d like to include a line in a template if a facter is defined. I''ve tried (along with a few variations): <% if defined?(foo) %> foo is defined <% else %> foo is undefined <% end %> I always get ''foo is undefined'' (whether the facter foo is defined or not). Help? ...thnx, ...dave -- Dave Alden The Ohio State University Department of Mathematics
Blake Barnett
2008-Feb-18 22:25 UTC
Re: how to check if a variable is defined (in a template)?
On Feb 18, 2008, at 2:16 PM, Dave Alden wrote:> > Hi, > I''d like to include a line in a template if a facter is defined. > I''ve > tried (along with a few variations): > > <% if defined?(foo) %> > foo is defined > <% else %> > foo is undefined > <% end %> > > I always get ''foo is undefined'' (whether the facter foo is defined > or not). > Help?I guess you didn''t see my message on IRC: defined? doesn''t return true if the object is defined, it returns the value of the object. So to follow closely what you have... you will probably want something more like: <% if !defined? foo %> foo is undefined <% else %> foo is defined <% end %> -Blake
Dave Alden
2008-Feb-19 14:42 UTC
Re: how to check if a variable is defined (in a template)?
Hi, On Mon, Feb 18, 2008 at 02:25:29PM -0800, Blake Barnett wrote:> I guess you didn''t see my message on IRC: defined? doesn''t return > true if the object is defined, it returns the value of the object. So > to follow closely what you have... you will probably want something > more like: > > <% if !defined? foo %> > foo is undefined > <% else %> > foo is defined > <% end %>I''m sorry - I thought I had replied on #puppet that this too did not work. I have the following in a template: <% 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 then run puppet and I get: 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...:( ...dave -- Dave Alden The Ohio State University Department of Mathematics
Luke Kanies
2008-Feb-19 14:54 UTC
Re: how to check if a variable is defined (in a template)?
On Feb 19, 2008, at 8:42 AM, Dave Alden wrote:> > I''m sorry - I thought I had replied on #puppet that this too did not > work. I have the following in a template: > > <% 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 > then > run puppet and I get: > > 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
<Derek.Whayman@barclayscapital.com>
2008-Feb-22 10:32 UTC
Re: how to check if a variable is defined (ina template)?
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-bounces@madstop.com [mailto:puppet-users-bounces@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)? On Feb 19, 2008, at 8:42 AM, Dave Alden wrote:> > I''m sorry - I thought I had replied on #puppet that this too did not > work. I have the following in a template: > > <% 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 > then run puppet and I get: > > 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-users@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. ------------------------------------------------------------------------
Luke Kanies
2008-Feb-22 17:57 UTC
Re: how to check if a variable is defined (ina template)?
On Feb 22, 2008, at 5:32 AM, <Derek.Whayman@barclayscapital.com> <Derek.Whayman@barclayscapital.com > wrote:> 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.Looks like this is the only thing you can do, at this point. Anyone feel like adding a ''defined?'' method to the TemplateWrapper class, so they can just be tested directly? It should be pretty obvious how to do from lib/puppet/parser/templatewrapper.rb. -- The great aim of education is not knowledge but action. -- Herbert Spencer --------------------------------------------------------------------- Luke Kanies | http://reductivelabs.com | http://madstop.com