Search code examples
rubyautomationpuppet

Puppet test defined variable with an empty string


I'm trying to test if a variable has a value with a simple if in puppet, but it never reachs the else condition that I'm trying to achieve.

ex :

if $::some_var['some:name'] != undef {
   $var = $::some_var['some:name']
}
else {
  $var = $::some_var['another:name']
} 

some_var cames from a json file, and if the ::some_var['some:name'] is emtpy it will put $var as empty, but what I'm trying to achieve is, if the var is defined but is empty to actually go through the else, can this be achieved using for instance

if defined($::some_var['some:name']) {
   $var = $::some_var['some:name']
}

else {
   $var = $::some_var['another:name']
}

Solution

  • I resolved it by using

    $var = ''  
    if $var =~ String[1] { 
    notify{"The value is: ${var}": } 
    } 
    else { 
    notify{"The value is: empty": } 
    }   
    
    file {'/tmp/helloworld.txt':   ensure  => present,   content => "Hello World!", }
    

    If I define $var whit some value, I'll met the if condition, if $var is empty I'll met the else.