加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 如何从Puppet函数中读取Hiera值?

发布时间:2020-12-16 21:03:23 所属栏目:百科 来源:网络整理
导读:我必须编写一个函数,按顺序执行以下操作来读取变量的值: 检查是否定义了一个facter变量.如果不, 从Hiera读取变量的值.如果不, 使用默认值. 我已经设法使用此if条件在我的Puppet脚本中执行此操作. # foo is read from (in order of preference): facter fact
我必须编写一个函数,按顺序执行以下操作来读取变量的值:

>检查是否定义了一个facter变量.如果不,
>从Hiera读取变量的值.如果不,
>使用默认值.

我已经设法使用此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_前缀调用已定义的函数.

您已经找到了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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读