歡迎您光臨本站 註冊首頁

puppet能否判斷服務端模版文件是否存在

←手機掃碼閱讀     火星人 @ 2014-03-03 , reply:0

puppet能否判斷服務端模版文件是否存在

客戶端使用主機名區分,比如主機名為aaa、bbb、ccc、ddd四台機器,我模版文件里有aaa.erb、bbb.erb、standard.erb

我想取他們相對應的主機名的模版文件,如aaa取aaa.erb的模版文件,bbb取bbb.erb模版文件,ccc、ddd沒有相對應的,就取standard.erb

我現在設置如下:
        $mark = $hostname
        file {
                "test":
                mode => 644, owner => root, group => root,
                ensure => present,
                name    => "/tmp/test.txt",
                content => template("test/${mark}.erb");
       }

我用這種方法是模版文件里有相對應的可以取到,那麼沒有對應的,我要怎麼判斷,然後讓他去取standard.erb,請問各位有沒有做過類似這樣的設置
《解決方案》

的在外面加個判斷吧?比如:先把模板下的文件做個list,然後判斷hostname是否存在。存在則賦予hostname為mark如果沒有則使用默認模板。這個思路
《解決方案》

本帖最後由 關陰月飛 於 2013-09-27 14:12 編輯

回復 1# anran_008

試試這樣行不行:
https://github.com/deanwilson/puppet-multitemplate $mark = $hostname
        file {
                "test":
                mode => 644, owner => root, group => root,
                ensure => present,
                name    => "/tmp/test.txt",
                content => template("test/${mark}.erb",
                                 'test/standard.erb'                                             
                                              ),
       }  
《解決方案》

回復 3# 關陰月飛

試了不行,存在的文件去找standard.erb,不存在的文件直接報錯提示模版文件不存在

問題已經解決了,我自己定義了一個函數去判斷文件是否存在,然後返回不同值,然後配置文件里再去做判斷就可以了

自定義exist函數,文件存在返回0,不存在返回1Puppet::Parser::Functions::newfunction(:exist, :type => :rvalue) do |vals|
    key = vals
    result = []
    key.each do |file|
        if File.exists?(file)
          result << 0
        else
          result << 1
        end
    end
    return result.join(" ")
end配置文件里的設置        $myvar = exist("/puppet/modules/test/templates/${hostname}.erb")
        if $myvar == 0 {
          $mark = $hostname
        }
        else {
          $mark = Standard
        }


        file {
                "test":
                mode => 644, owner => root, group => root,
                ensure => present,
                name    => "/tmp/test.txt",
                content => template("test/${mark}.erb");
       }

[火星人 ] puppet能否判斷服務端模版文件是否存在已經有905次圍觀

http://coctec.com/docs/service/show-post-164.html