我有一个简单的函数,它需要一个JSON和'做些什么'。主要部分工作良好,但功能不仅返回我想要的,而且还有。每个循环的结果!木偶 - 自定义函数中'each'的意外结果
的代码:
module Puppet::Parser::Functions
newfunction(:mlh, :type => :rvalue) do |args|
lvm_default_hash = args[0]
lvm_additional_hash = args[1]
if lvm_additional_hash.keys.length == 1
if lvm_additional_hash.keys.include? 'logical_volumes'
# do stuff - we have only 'logical_volumes'
lvm_default_hash.keys.each do |key|
pv_array = Hash['physical_volumes' => lvm_default_hash[key]['physical_volumes']]
lv_hash = lvm_default_hash[key]['logical_volumes']
new_lv_hash = lvm_additional_hash['logical_volumes']
merged_lv_hash = Hash['logical_volumes' => lv_hash.merge(new_lv_hash)]
# this is what I want to return to init.pp
puts Hash[key => pv_array.merge(merged_lv_hash)]
end
end
end
end
end
变量在init.pp是:
$default_volume_groups = {
'sys' => {
'physical_volumes' => [
'/dev/sda2',
],
'logical_volumes' => {
'root' => {'size' => '4G'},
'swap' => {'size' => '256M'},
'var' => {'size' => '8G'},
'docker' => {'size' => '16G'},
},
},
}
和从hieradata第二个参数:
modified_volume_groups:
logical_volumes:
cloud_log:
size: '16G'
在init.pp我有这样的事情来测试它:
notice(mlh($default_volume_groups, $modified_volume_groups))
,给了我一个结果:
syslogical_volumesvarsize8Gdockersize16Gcloud_logsize16Gswapsize256Mrootsize4Gphysical_volumes/dev/sda2
Notice: Scope(Class[Ops_lvm]): sys
的通知之前,“长”部分是正确的结果从放但注意:范围():sys这是什么我不想! 我知道,这是该每个循环的结果比default_volumes_groups:
lvm_default_hash.keys.each do |key|
# some stuff
end
如何阻止这种不必要的结果?它吹我的傀儡的逻辑,因为我的init.pp看到这系统而不是我想要的。
有人知道如何处理这样的问题吗?
谢谢!
我倾向于认为您看到的额外文本并不是函数返回值的一部分。它看起来像调试输出描述对'notice()'的调用,而不是呈现给notice()的值的任何部分以供它打印。 –
谢谢,但是我的函数如何返回两个值 - 一个来自_puts_,另一个来自_each_循环的默认值? – kamh