I am trying to understand what re-assigning
a variable means in puppet
. I have the following code:
$my_var = "${facts['os']['famil']}"
notice("value of my_var ${my_var}")
if $my_var != undef {
notice("my_var evaluates to true")
} else {
notice("my_var evaluates to false")
}
notice("Type of my_var ${type(my_var)}")
$my_var = 'Linux'
notice("Finishing now")
Notice there is an intentional type on the first line famil
instead of family
. So puppet
fact should not return a value for this fact. However, to my surprise I am seeing this output when I run the code:
Notice: Scope(Class[main]): value of my_var
Notice: Scope(Class[main]): my_var evaluates to true
Notice: Scope(Class[main]): Type of my_var String
Error: Evaluation Error: Cannot reassign variable '$my_var' (file: test.pp, line: 9, column: 9) on node govlupe1.std4.edge.vrsn.com
I have following questions:
my_var
is String when it clearly has no value?$my_var != undef
true?my_var
is null
(undef
) then why is puppet
complaining about re-assigning
the variable?About 3., whatever value is assigned to $my_var
, even undef
, it cannot be reassigned another value.
Here is what Puppet documentation says:
Unlike most other languages, Puppet only allows a given variable to be assigned once within a given scope. You cannot change the value of a variable, although you can assign a different value to the same variable name in a new scope
Example:
> puppet apply -e '$myvar = undef ; $myvar = undef'
Error: Evaluation Error: Cannot reassign variable '$myvar' (line: 1, column: 25) on node
About 1. and 2., the type is string because it is the result of an interpolation. Try with this:
$my_var = $facts['os']['famil']
if $my_var != undef {
notice("my_var evaluates to true")
} else {
notice("my_var evaluates to false")
}
notice("Type of my_var ${type(my_var)}")