Hi Doug:

On Wed, Aug 10, 2016 at 12:28 PM, dkoleary <[email protected]>
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 [email protected].
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/puppet-users/18556755-8853-4367-8a1e-47d337e66a22%40googlegroups.com
> <https://groups.google.com/d/msgid/puppet-users/18556755-8853-4367-8a1e-47d337e66a22%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>



-- 
--
Peter Huene - Senior Software Engineer
[email protected] | @peterhuene
-- 
PuppetConf 2016
<https://www.google.com/url?q=https%3A%2F%2Fpuppet.com%2Fpuppetconf&sa=D&sntz=1&usg=AFQjCNFf4WhS0623bLixxPqtXYm9RV8fyg>
, 19 - 21 October , San Diego, California
*Summer Savings - Register by 15 September and save $240
<https://www.google.com/url?q=https%3A%2F%2Fpuppetconf2016.eventbrite.com%2F%3Fdiscount%3DSummerSavings&sa=D&sntz=1&usg=AFQjCNGjMlH0LEUoxVdNBzlb4PcuUbs42Q>*

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-users/CACZQQfNDuOmpzfZeHZ5SBxcjCvWyO9os-UaERk5jq-%3DUkJNY-A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to