Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to check whether the content of a variable is an odd number. But I need an integer for it.

$ip_array = split($ipaddress, '.')
$odd_ip = $ip_array[3] % 2

if $odd_ip == 1 {
  notice("is odd")

Is there an easy way to convert a string to integer?

share|improve this question

You can do this:

$n_timeout = 0 + $timeout

share|improve this answer
This breaks with stings with leading zeros, e.g. "04" – Gary van der Merwe Apr 6 at 7:17

Your code will work exactly as defined; Puppet implicitly converts strings and integers as appropriate for the comparison operator being used.

share|improve this answer
I got the following error: Could not match % at /etc/puppet/modules/my/manifests/target.pp:23 on node – MMore May 14 '12 at 17:16
If I replace '%' with '*' (testing) -> left operand of * is not a number at /etc/puppet/modules/my/manifests/target.pp:25 on node – MMore May 14 '12 at 17:20
Ah. You have found a genuine bug in Puppet. You should file a ticket at so that we can fix it. – Daniel Pittman May 14 '12 at 18:15
Puppet doesn't do that when the value comes from hiera. You get a "Error 400 on SERVER: comparison of Fixnum with String failed" error. – Rob Feb 14 '14 at 15:42

I've taken to using the scanf function in stdlib.

An example of this:

$ram = scanf("${::memorysize_mb}", "%i")

This requires puppet >= 3.7.5

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.