Puppet:文件资源仅当文件存在时 [英] Puppet : file resource only if file exists

查看:95
本文介绍了Puppet:文件资源仅当文件存在时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的很简单:

1.

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

Copy /source/file to /target/file. I achieve this using the following:

file { 'my_file_copy':
  ensure   => file,
  source   => 'file:/source/file',
  path     => "/target/file",
}

2.

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

However, if file /source/file does not exist, I do NOT want it to perform this task.

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

I am really struggling with this logic. I attempted the solution below but it throws exceptions during puppet run.

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

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

Is there a better way of achieving this task ? Ideally, I would like to only use "file" and avoid using "exec". But at this point I would settle for a solution !

推荐答案

因为 Puppet 是一种声明式语言,其中只声明结束状态,所以命令式逻辑(如您所描述的 - 如果 A,执行 X - 通常是难以表达.

Because Puppet is a declarative language where only the end-state is declared, imperative logic such as what you've described - if A, do X - is often hard to express.

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

Personally, I would try to simply avoid this requirement of having file B copied if and only if file A exists. Often there's a better way.

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

If the requirement needs to stay, however, then use of Exec here sounds like a pretty good option to me.

exec { 'my_file_copy':
  command => 'cp /source/file /target/file',
  onlyif  => 'test -e /source/file',
  creates => '/target/file',
  path    => '/bin',
}

这篇关于Puppet:文件资源仅当文件存在时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆