簡體   English   中英

木偶-創建NESTED自定義事實

[英]Puppet - create NESTED custom fact

我已經成功創建了一個.rb自定義事實,該自定義事實分析了內置事實以創建新值,但是我現在正嘗試將其用作Puppet的嵌套自定義事實。

我要創建的層次結構類似於內置事實,例如運行Facter(或Facter -p)將顯示:

custom_parent => {
  custom_fact_1 => whatever
  custom_fact_2 => whatever2
}

人偶清單中的用法是:

$custom_parent.custom_fact_1

到目前為止,我已經嘗試了領先的語法,例如:

Facter.add (:custom_parent)=>(custom_fact_1) do
Facter.add (:custom_parent)(:custom_fact_1) do
Facter.add (:custom_parent.custom_fact_1) do
Facter.add (:custom_parent:custom_fact_1) do
Facter.add (custom_parent:custom_fact_1) do

...以及其他許多變體,但是無法創建嵌套的自定義事實數組。 我已經搜索了一段時間,如果有人知道是否有可能,我將非常感激。

我確實發現可以使用/etc/puppetlabs/facter/facts.d/目錄中的.yaml文件中的數組來創建嵌套事實,如下所示,但是這會設置FIXED值,並且不會處理我自定義中需要的邏輯事實。

{
  "custom_parent":
  {
    "custom_fact_1": "whatever",
    "custom_fact_2": "whatever2",
  }
}

提前致謝。

我要創建的層次結構類似於內置事實,例如運行Facter(或Facter -p)將顯示:

 custom_parent => { custom_fact_1 => whatever custom_fact_2 => whatever2 } 

沒有“嵌套”事實。 但是,存在“結構化”事實,這些事實可能具有哈希值。 在一定程度上,Facter提供了您描述為“嵌套”的輸出,這肯定是您要查找的內容。

由於結構化事實的價值要素本身並不是事實,因此需要在事實本身的解決方案中指定它們:

Facter.add (:custom_parent) do
    {
        :custom_fact_1 => 'whatever',
        :custom_fact_2 => 'whatever2',
    }
end

whateverwhatever2不必是文字字符串; 它們可以是或多或少的任意Ruby表達式。 當然,您也可以將成員與創建散列分開設置(但以相同的事實分辨率):

Facter.add (:custom_parent) do
    value = new Hash()
    value[:custom_fact_1] = 'whatever'
    value[:custom_fact_2] = 'whatever2'
    value
end

謝謝@約翰·布林格。 您的示例非常接近,但是我發現我需要使用type => aggregate和chunk才能使其正常工作。 我還將其與定義的函數結合在一起,最終結果基於以下代碼。

如果您有任何其他建議可改善此代碼的一致性,請隨時指出。 干杯

# define the function to process the input fact
def dhcp_octets(level)
  dhcp_split = Facter.value(:networking)['dhcp'].split('.')
  if dhcp_split[-level..-1]
    result = dhcp_split[-level..-1].join('.')
    result
  end
end

# create the parent fact
Facter.add(:network_dhcp_octets, :type => :aggregate) do
  chunk(:dhcp_ip) do
    value = {}

# return a child => subchild array
    value['child1'] = {'child2' => dhcp_octets(2)}

# return child facts based on array depth (right to left)
    value['1_octets'] = dhcp_octets(1)
    value['2_octets'] = dhcp_octets(2)
    value['3_octets'] = dhcp_octets(3)
    value['4_octets'] = dhcp_octets(4)

# this one should return an empty fact
    value['5_octets'] = dhcp_octets(5)
    value
  end
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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