2

在 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 功能源于网络上的某个人,他声称它检查主文件是否存在,但事实并非如此(至少不是我能看到的)。

4

3 回答 3

4

Alex G 为我提供了一些更正的解决方案,也许对其他人有用:

if file('/does/it/exist', '/dev/null') != '' {
    # /does/it/exist must exist, do things here
}
于 2014-08-20T15:08:28.383 回答
2

在 puppet master 中,您可以像这样测试它:

# 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 回答
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 回答