2017-04-20 60 views
0

我想要執行一些類只有當特定的rpm版本不存在。根據條件在傀儡執行類

例如:

class base{ 
    if specified_rpm_absent { 
    include base::class1 
    include base::class2 
    } 
    else { 
    notify {"Already there":} 
    } 
} 

回答

2

你可以做的是定義一個custom fact返回true或false取決於這個RPM的存在或不存在,然後在條件邏輯使用它,即

事實上代碼:

Facter.add(:specified_rpm_absent) do 
    setcode do 
    # Some Ruby code to return true or false depending on RPM package 
    # Facter::Core::Execution.exec() can be used to execute a shell 
    # command. 
    end 
end 

木偶4

class base { 
    if $facts['specified_rpm_absent'] { 
    include base::class1 
    include base::class2 
    } 
    else { 
    notify {"Already there":} 
    } 
} 

木偶3

class base { 
    if $::specified_rpm_absent { 
    include base::class1 
    include base::class2 
    } 
    else { 
    notify {"Already there":} 
    } 
} 

的OP辯稱以下,最好是在這裏使用的傀儡功能,並且功能也讓爭論。

問題是Functions execute on the Puppet master. They do not execute on the Puppet agent. Hence they only have access to the commands and data available on the Puppet master host

但是,如果使用Masterless Puppet,但不受Puppet支持,函數可用於此目的,此用例在Jussi Heinonen的「學習木偶」(2015)中描述。

我不會推薦這種方法的幾個原因:

  • 它不是由木偶支持,因此也不能保證木偶的未來版本將不會使這個不可能的。
  • 該代碼將不可移植。也就是說,代碼不能在Puppet Forge上共享,並且不能遷移到傳統的主/僞設置。
  • 這不是慣用的,會混淆認識木偶的人,即違反。

最後,應該指出的是,根據是否安裝RPM來做出決定的設計可能存在更根本性的錯誤。爲什麼傀儡不知道RPM是否已安裝?

+0

任何其他不需要自定義事實的解決方案? –

+0

如果您的要求是僅在特定rpm版本不存在的情況下執行類,那麼通常使用自定義事實來實現。我想不出還有其他辦法可以做到。 –

+0

謝謝。需要了解自定義事實! –