如何在 Puppet 中确定文件/目录是否存在条件?

如何在 Puppet 中确定文件/目录是否存在条件?

fail我正在尝试在 Puppet 中编写一个函数,如果传递的目录路径不存在则执行该函数。

if File["/some/path"]总是返回 true,并且if defined(File["/some/path"])只有当资源在 puppet 中定义时才返回 true,不管它是否实际存在。

有没有办法用一个简单的if语句来做到这一点?

谢谢

答案1

解决方法:在 exec“测试”上使用 onlyif,并在您想要执行的操作中要求它:

exec {"check_presence":
  command => '/bin/true',
  onlyif => '/usr/bin/test -e /path/must/be/available',
}

whatever {"foo...":
  .....
  require => Exec["check_presence"],
}

答案2

我也曾遇到过困难,不知道如果目录不存在,如何阻止模块运行。我发现这个方法对我有用。

exec { 'module_name':
   command => "command to run with variables", # Double quotes for Variable interpolation
   path   => "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:",
   onlyif => 'test -d /mydirectory',
   }

添加路径对我来说确实很有效。出于某种原因,即使我将路径添加到测试命令中,/bin/test它似乎也无法正常工作。

希望这可以为遇到同样问题的其他人提供帮助。

相关内容