[英]How do I read a Hiera value from a Puppet function?
我必須編寫一個函數,按順序執行以下操作來讀取變量的值:
我已經設法使用此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
}
但是我想避免重復這個,如果我希望以這種方式解析的每個變量的條件,因此想到編寫格式為get_args('foo', 'default_value')
的新Puppet函數,它將返回我的值
default_value
。 我知道我可以使用lookupvar
從ruby函數中讀取一個因素。 如何從我的Puppet ruby函數中讀取hiera變量?
您可以使用function_
前綴調用已定義的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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.