Puppet:文件资源仅当文件存在时

use*_*593 1 puppet

我想做的很简单:

1.

复制/source/file/target/file. 我使用以下方法实现了这一点:

file { 'my_file_copy':
  ensure   => file,
  source   => 'file:/source/file',
  path     => "/target/file",
}
Run Code Online (Sandbox Code Playgroud)

2.

但是,如果文件/source/file不存在,我不希望它执行此任务。

我真的很纠结这个逻辑。我尝试了下面的解决方案,但它在木偶运行期间抛出异常。

puppet:如果存在一个文件,则复制另一个文件

有没有更好的方法来完成这项任务?理想情况下,我只想使用“文件”而避免使用“exec”。但在这一点上,我会满足于一个解决方案!

Ale*_*vey 5

因为 Puppet 是一种仅声明结束状态的声明性语言,所以命令式逻辑(例如您所描述的) - 如果 A,执行 X - 通常很难表达。

就我个人而言,当且仅当文件 A 存在时,我会尽量避免复制文件 B 的这种要求。通常有更好的方法。

但是,如果需求需要保留,那么在这里使用 Exec 对我来说是一个不错的选择。

exec { 'my_file_copy':
  command => 'cp /source/file /target/file',
  onlyif  => 'test -e /source/file',
  creates => '/target/file',
  path    => '/bin',
}
Run Code Online (Sandbox Code Playgroud)