On Nov 15, 3:42 pm, Justin Lloyd <[email protected]> wrote:
> I tried the following (names changed to protect the innocent and guilty):
>
> class myclass ( $param ) {
>
> $myvar = [ "foo", "bar" ]
>
> if $param == "special" {
> $myvar += [ "blah" ]
> }
>
> }
>
> and got the message "...Cannot append, variable myvar is defined in this
> scope...". According to the docs, variables cannot be modified in the same
> scope because of the declarative nature of
> Puppet<http://docs.puppetlabs.com/guides/language_guide.html>
> .
>
> However, if I change the plus-signment statment to
>
> $myclass::myvar += [ "blah" ]
>
> it works fine. Can someone explain this aspect of scoping? (Or is this
> possibly a bug...?)
Do check whether you are seeing the behavior Christopher reports.
It is a Puppet axiom that you cannot modify the value of any variable
once it is set. Even += doesn't really do that: that's why the
appended values are not seen outside the scope where the += is
performed.
I recommend you just come up with another way to do what you want. In
fact, I recommend you avoid += even in situations where it works.
Here's one way:
class myclass ( $param ) {
if $param == "special" {
$myvar = [ "foo", "bar", "blah" ]
} else {
$myvar = [ "foo", "bar" ]
}
}
There are also a couple of ways you could concatenate arrays in
Puppet. If you want to do that a lot then I'd write a custom
function, but for the odd one-off you could hack together something
using inline_template() and split().
John
--
You received this message because you are subscribed to the Google Groups
"Puppet Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/puppet-users?hl=en.