Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何测试Puppet Master上是否存在文件_Ruby_Linux_Puppet - Fatal编程技术网

Ruby 如何测试Puppet Master上是否存在文件

Ruby 如何测试Puppet Master上是否存在文件,ruby,linux,puppet,Ruby,Linux,Puppet,在一个定制的木偶模块上我有 g_iptables ├── files │ └── fqdn-of-server ├── lib │ └── puppet │ └── parser │ └── functions │ └── file_exists.rb └── manifests └── init.pp 我想让模块做一些事情,不管Puppet主机上是否存在文件“服务器的fqdn”。谷歌确实给了我一个文件_exists.

在一个定制的木偶模块上我有

g_iptables
├── files
│   └── fqdn-of-server
├── lib
│   └── puppet
│       └── parser
│           └── functions
│               └── file_exists.rb
└── manifests
    └── init.pp
我想让模块做一些事情,不管Puppet主机上是否存在文件“服务器的fqdn”。谷歌确实给了我一个文件_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 {
 ...
}
在我的manifest init.pp中(当然$fqdn是一个因素)。问题是它只在客户端上工作(因此$does_fqdn_file_exists为1)。如果/tmp/$fqdn在客户端$fqdn上存在,它在puppet主机上不工作

另外,我想在这个构造中使用puppet:///uri结构,但是到目前为止,我的函数不理解这个uri


有人能帮我吗?ruby函数源于web上的某个人,他声称它会检查主机上是否存在文件,但事实并非如此(至少不是我所看到的情况)。

在puppet主机中,您可以这样测试它:

# 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中的函数将读取实际存在的第一个文件的内容。以这种方式使用它有点困难,但它可以工作,并且不需要修改默认的函数集。

Alex G提供的解决方案对我有一些更正,可能对其他人有用:

if file('/does/it/exist', '/dev/null') != '' {
    # /does/it/exist must exist, do things here
}
您可以使用exist()函数,希望对您有所帮助