Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
add comment

2 Answers 2

You can do this:

$n_timeout = 0 + $timeout

share|improve this answer
add comment

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 mynode.domain.de –  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 mynode.domain.de –  MMore May 14 '12 at 17:20
1  
Ah. You have found a genuine bug in Puppet. You should file a ticket at projects.puppetlabs.com/projects/puppet/issues/new 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 at 15:42
add comment

Your Answer

 
discard

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.