puppet官方提供的create_resources函数的参数生成版,主要使用场景用以转换对应传递的hash字段给erb模板使用.
# 相关代码的github地址,传送门如下:
代码: hash_merge.rb
erb部分的使用,可参考笔者前阵子的rsync管理模块的 类引用 和 erb迭代
函数文件: hash_merge.rb 函数注释部分写在代码中了,因此直接上代码如下:
module Puppet::Parser::Functions
newfunction(:hash_merge, :type => :rvalue, :doc => <<-EOS
*Examples:*
$ddd = {'t1' => 'aa', 't2' => 'ab'}
$ttt = {
'key_1' => { 't3' => 'a1'},
'key_2' => { 't1' => 'zz', 't3' => 'a8' }
}
hash_merge($ttt, $ddd)
Would result in: "
{
'key_1' => { 't3' => 'a1', 't1' => 'aa', 't2' => 'ab' },
'key_2' => { 't1' => 'zz', 't3' => 'a8', 't2' => 'ab' }
}
"
EOS
) do |arguments|
# Technically we support two arguments but only first is mandatory ...
raise(Puppet::ParseError, "hash_merge(): Wrong number of arguments " +
"given (#{arguments.size} for 1)") if arguments.size < 1
res = arguments[0]
unless res.is_a?(Hash)
raise Puppet::ParseError, "hash_merge(): The argument must be a hash"
end
if arguments.size == 2
t_hash = arguments[1] || {}
res.each_key do |k|
t_hash.each_key do |t|
res[k][t] = t_hash[t] unless res[k].key?(t)
end
end
end
return res
end
end
# vim: set ts=2 sw=2 et :
转载于:https://blog.51cto.com/mos1989/1699509