[英]Puppet : file resource only if file exists
What I want to do is quite simple : 我想做的很简单:
1. 1。
Copy /source/file
to /target/file
. 将
/source/file
复制到/target/file
。 I achieve this using the following: 我使用以下方法实现此目的:
file { 'my_file_copy':
ensure => file,
source => 'file:/source/file',
path => "/target/file",
}
2. 2。
However, if file /source/file
does not exist, I do NOT want it to perform this task. 但是,如果文件
/source/file
不存在,则我不希望它执行此任务。
I am really struggling with this logic. 我真的在为这种逻辑而苦苦挣扎。 I attempted the solution below but it throws exceptions during puppet run.
我尝试了以下解决方案,但在运行木偶时抛出异常。
puppet: if one file exists then copy another file over up:如果存在一个文件,则将另一个文件复制到
Is there a better way of achieving this task ? 是否有更好的方法来完成此任务? Ideally, I would like to only use "file" and avoid using "exec".
理想情况下,我只想使用“文件”,而避免使用“ exec”。 But at this point I would settle for a solution !
但是在这一点上,我会解决的!
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. 因为Puppet是只声明结束状态的声明性语言,所以命令式的逻辑(如您所描述的)(如果是A,则执行X)通常很难表达。
Personally, I would try to simply avoid this requirement of having file B copied if and only if file A exists. 就个人而言,我会尝试避免仅当文件A存在时才复制文件B的要求。 Often there's a better way.
通常有更好的方法。
If the requirement needs to stay, however, then use of Exec here sounds like a pretty good option to me. 但是,如果需要保留该要求,那么对我来说,在这里使用Exec听起来是一个不错的选择。
exec { 'my_file_copy':
command => 'cp /source/file /target/file',
onlyif => 'test -e /source/file',
creates => '/target/file',
path => '/bin',
}
You could use this logic: 您可以使用以下逻辑:
$file = "/source/file"
exec { "chk_${file}_exist":
command => "true",
path => ["/usr/bin","/usr/sbin", "/bin"],
onlyif => "test -f ${file}"
}
file {"/target/file":
ensure => file,
source => 'file:/source/file',
owner => 'root',
group => 'root',
mode => '0750',
require => Exec["chk_${file}_exist"],
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.