簡體   English   中英

如何在 Puppet Master 上測試文件是否存在

[英]How to test for existence of a file on the Puppet Master

在 Puppet 上的自定義模塊中,我有

g_iptables
├── files
│   └── fqdn-of-server
├── lib
│   └── puppet
│       └── parser
│           └── functions
│               └── file_exists.rb
└── manifests
    └── init.pp

我想讓模塊做一些事情,不管 Puppet Master 上是否存在文件“fqdn-of-server”。 谷歌搜索確實給了我一個 file_exists.rb 函數:

#!/usr/bin/ruby

require 'puppet'

module Puppet::Parser::Functions
  newfunction(:file_exists, :type => :rvalue) do |args|
    if File.exists?(args[0])
      return 1
    else
      return 0
    end
  end
end

這在放入以下內容時確實有效:

$does_fqdn_file_exists = file_exists("/tmp/$fqdn")

if $does_fqdn_file_exists == 1 {
 ...
}

在我的清單 init.pp 中(當然 $fqdn 是一個因素)。 問題是它僅適用於客戶端(因此,如果 /tmp/$fqdn 存在於客戶端 $fqdn 上,則 $does_fqdn_file_exists 為 1,它不適用於 puppet master。

另外,我想在這個構造中使用 puppet:/// uri 結構,但到目前為止,我的函數不理解這個 uri。

有人可以幫助我嗎? ruby 函數源於網絡上的某個人,他聲稱它會檢查 master 上的文件是否存在,但事實並非如此(至少不是我所看到的)。

Alex G 為我提供了一些更正的解決方案,也許對其他人有用:

if file('/does/it/exist', '/dev/null') != '' {
    # /does/it/exist must exist, do things here
}

在木偶大師中,您可以這樣測試:

# create a temporary file that may look like this :

]$ cat /tmp/checkfile.pp
$does_fqdn_file_exists = file_exists("/tmp/$fqdn")
notify{ "Check File = $does_fqdn_file_exists" : }

# Then run it :

]$ puppet apply /tmp/checkfile.pp

這是一種處理標准函數庫的方法。 在已知位置創建一個內容“未找到”的文件,並使用此檢查:

if file('/does/it/exist', '/file/containing/not_found') != 'not found' {
    # /does/it/exist must exist, do things here
}

這是因為 Puppet 中的file()函數將讀取實際存在的第一個文件的內容。 以這種方式使用它有點麻煩,但它可以工作並且不需要修改默認的函數集。

你可以使用exists()函數https://gist.github.com/j4m3s/3307835我希望它有幫助

暫無
暫無

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

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