Hello,
I'm trying to make a set of functions to simulate an array with which I would be able to append value in the same scope.
The first function is called "array_append" which take 2 arguments: the variable name and the value to append. The function then iterates through variables "#{name}_#{i}" (with incrementing i from 0) until the variable does not exists and then set the value of this variable. This way I'm using one variable for each element of my array and I can simulate a real array from the puppet DSL.
Here's the function I've made:
module Puppet::Parser::Functions
newfunction(:array_append) do |args|
i += 1 while lookupvar("#{args[0]}_#{i}")
puts "I've set #{args[0]}_#{i} to #{args[1]}"
setvar("#{args[0]}_#{i}", args[1])
If I'm calling this function from a manifest it works as expected:
test.pp
array_append("toto", "prout0")
array_append("toto", "prout1")
array_append("toto", "prout2")
#puppet agent test.pp
I've set toto_0 to prout0
I've set toto_1 to prout1
I've set toto_2 to prout2
Finished catalog run in 0.02 seconds
But if I want to set a global variable it does not work anymore and I've errors. It seams that
array_append("::toto", "prout0")
array_append("::toto", "prout1")
array_append("::toto", "prout2")
I've set ::toto_0 to prout0
I've set ::toto_0 to prout1
Error: Cannot reassign variable ::toto_0 at /root/puppet/manifests/test.pp:9 on node www1.egasys.com Error: Cannot reassign variable ::toto_0 at /root/puppet/manifests/test.pp:9 on node www1.egasys.com
Then if I'm calling array_append from a module defined resource:
modules/network/manifests/route/add_net.pp
define network::route::add_net($gw) {
array_append("::routes_net", {name => $name, gw => $gw})
#puppet agent test3.pp
I've set ::routes_net_0 to name42.42.42.0/24gw192.168.0.1
I've set ::routes_net_0 to name54.54.54.0/24gw192.168.0.1
this time: no errors but setvar is not working: it's like setvar does nothing
I can't understand why case #2 and #3 are not working and if it's a normal behaviour or a bug.
if you have any ideas ?
thx for your help
++ Jerome