comparing facts as integers in puppet server 4.5?

1,915 views
Skip to first unread message

dkoleary

unread,
Aug 10, 2016, 3:28:46 PM8/10/16
to Puppet Users
Hey

I'm setting up a module to handle our smtp config.  

My simple if statement is:

  if ($facts['os']['release']['major'] > 5 ) {


results in a bright red error stating:

Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Evaluation Error: Error while evaluating a Resource Statement, Evaluation Error: Comparison of: String > Integer, is not possible. Caused by 'A String is not comparable to a non String'. at /etc/puppetlabs/code/environments/dkoleary/modules/mpismtp/manifests/init.pp:50:40 on node nap1d030.multiplan.com

Facts are obviously interpreted as strings.  Several posts referenced that puppet will auto-translate strings to integers if appropriate.  So, figuring there was something special about the facts hash, I created a variable for it:

  $mpismpt_orm = $facts['os']['release']['major']
 
if ($mpismpt_orm > 5) {

with the same result.  It wasn't until I updated the var declaration as "$mpismpt_orm = 0 + $facts['os']['release']['major']" that this works - a hint in a post from 2010.  There has to be a better way to have facts be interpreted as numbers, doesn't there?  

It works so, if all else fails, I'm good.  It just seems that something should have changed in this regards in the last 7 years.

Thanks for any information.

Doug O'Leary




Peter Huene

unread,
Aug 10, 2016, 6:48:47 PM8/10/16
to puppet...@googlegroups.com
Hi Doug:

On Wed, Aug 10, 2016 at 12:28 PM, dkoleary <dkol...@olearycomputers.com> wrote:
Hey

I'm setting up a module to handle our smtp config.  

My simple if statement is:

  if ($facts['os']['release']['major'] > 5 ) {


results in a bright red error stating:

Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Evaluation Error: Error while evaluating a Resource Statement, Evaluation Error: Comparison of: String > Integer, is not possible. Caused by 'A String is not comparable to a non String'. at /etc/puppetlabs/code/environments/dkoleary/modules/mpismtp/manifests/init.pp:50:40 on node nap1d030.multiplan.com

Facts are obviously interpreted as strings.  Several posts referenced that puppet will auto-translate strings to integers if appropriate.  So, figuring there was something special about the facts hash, I created a variable for it:

Recent versions of Puppet do not "stringify" facts and Facter 3 outputs facts of many different types, including numerical.  However, Facter's schema defines this particular fact as being a string because it cannot limit any component of a version string to be numerical; for example, a valid version string could just be a release code name.
 

  $mpismpt_orm = $facts['os']['release']['major']
 
if ($mpismpt_orm > 5) {

with the same result.  It wasn't until I updated the var declaration as "$mpismpt_orm = 0 + $facts['os']['release']['major']" that this works - a hint in a post from 2010.  There has to be a better way to have facts be interpreted as numbers, doesn't there?  

The Puppet compiler only automatically coerces strings to numeric types for arithmetic operations, such as the plus operator, as you've noticed; the comparison operators do no such coercion.

In Puppet 4.5+, you can use the `new` function (https://docs.puppet.com/puppet/latest/reference/function.html#new) to perform type conversions:

if Integer($facts['os']['release']['major']) > 5 {
  # ...
}

This example will explicitly convert the string to an integer using the "type-conversion-like" syntax for invoking the new function.  It is semantically equivalent to explicitly invoking the `new` function, like so:

if Integer.new($facts['os']['release']['major']) > 5 {
  # ...
}

On older versions, you can use the conversion technique you've already discovered or the `scanf` function (https://docs.puppet.com/puppet/latest/reference/function.html#scanf).

Hope this helps.
 

It works so, if all else fails, I'm good.  It just seems that something should have changed in this regards in the last 7 years.

Thanks for any information.

Doug O'Leary




--
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 view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/18556755-8853-4367-8a1e-47d337e66a22%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
--
Peter Huene - Senior Software Engineer

peter...@puppet.com | @peterhuene
-- 
PuppetConf 201619 - 21 October San Diego, California

dkoleary

unread,
Aug 10, 2016, 8:07:24 PM8/10/16
to Puppet Users
Excellent, sir!  Thank you very much for the tip and clear information.

Thanks again.

Doug
Hi Doug:

To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages