我必须编写一个函数,按顺序执行以下操作来读取变量的值:
我已经设法使用此if条件在我的Puppet脚本中执行此操作.
# foo is read from (in order of preference): facter fact, hiera hash, hard coded value
if $::foo == undef {
$foo = hiera('foo', 'default_value')
} else {
$foo = $::foo
}
Run Code Online (Sandbox Code Playgroud)
但是我想避免重复这个,如果我希望以这种方式解析的每个变量的条件,因此想到写一个格式的新Puppet函数get_args('foo', 'default_value'),它将返回我的值
default_value.我知道我可以用来lookupvar从ruby函数中读取一个因素.如何从我的Puppet ruby函数中读取hiera变量?
您可以使用function_前缀调用定义的函数。
您lookupvar已经找到了该功能。
把它们放在一起:
module Puppet::Parser::Functions
newfunction(:get_args, :type => :rvalue) do |args|
# retrieve variable with the name of the first argument
variable_value = lookupvar(args[0])
return variable_value if !variable_value.nil?
# otherwise, defer to the hiera function
function_hiera(args)
end
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3270 次 |
| 最近记录: |