在我拥有的 Puppet 上的自定义模块中
g_iptables
├── files
│ └── fqdn-of-server
├── lib
│ └── puppet
│ └── parser
│ └── functions
│ └── file_exists.rb
└── manifests
└── init.pp
我想让模块做一些事情,无论文件“fqdn-of-server”是否存在于 Puppet Master 上。谷歌搜索确实给了我一个 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 {
...
}
在我的 list 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
}
关于ruby - 如何测试 Puppet Master 上的文件是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18784329/