2

私が持っているPuppetのカスタマイズされたモジュールで

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

ファイル「fqdn-of-server」が Puppet マスターに存在するかどうかに関係なく、モジュールに何かをさせたいと考えています。グーグルで 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:/// uri 構造を使用したいのですが、私の関数はこの uri を理解していません。

誰かが私を助けることができますか?ruby 関数は、マスター上のファイルの存在をチェックすると主張するウェブ上の誰かに由来しますが、そうではありません (少なくとも私が見ることができるものではありません)。

4

3 に答える 3

2

標準関数ライブラリを使用する方法を次に示します。コンテンツが「見つかりません」という既知の場所にファイルを作成し、このチェックを使用します。

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

これはfile()、Puppet の関数が、実際に存在する最初のファイルの内容を読み取るためです。このように使用するのは面倒ですが、機能し、デフォルトの関数セットを変更する必要はありません。

于 2013-12-13T23:12:07.170 に答える
2

パペット マスターでは、次のようにテストできます。

# 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
于 2013-09-13T15:53:21.067 に答える