簡體   English   中英

如何從Puppet函數中讀取Hiera值?

[英]How do I read a Hiera value from a Puppet function?

我必須編寫一個函數,按順序執行以下操作來讀取變量的值:

  • 檢查是否定義了一個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函數,它將返回我的值

  1. 一個事實,如果它存在,
  2. 變量變量,或
  3. 只需返回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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM