Puppet:仅当文件存在时才提供文件资源
Puppet : file resource only if file exists
我想做的很简单:
1.
将/source/file
复制到/target/file
。我使用以下方法实现此目的:
file { 'my_file_copy':
ensure => file,
source => 'file:/source/file',
path => "/target/file",
}
2.
但是,如果文件 /source/file
不存在,我不希望它执行此任务。
我真的很纠结这个逻辑。我尝试了下面的解决方案,但它在 puppet 运行.
期间抛出异常
puppet: if one file exists then copy another file over
有没有更好的方法来完成这个任务?
理想情况下,我只想使用 "file" 并避免使用 "exec"。但在这一点上,我会接受一个解决方案!
因为 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',
}
你可以使用这个逻辑:
$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"],
}
我想做的很简单:
1.
将/source/file
复制到/target/file
。我使用以下方法实现此目的:
file { 'my_file_copy':
ensure => file,
source => 'file:/source/file',
path => "/target/file",
}
2.
但是,如果文件 /source/file
不存在,我不希望它执行此任务。
我真的很纠结这个逻辑。我尝试了下面的解决方案,但它在 puppet 运行.
期间抛出异常puppet: if one file exists then copy another file over
有没有更好的方法来完成这个任务? 理想情况下,我只想使用 "file" 并避免使用 "exec"。但在这一点上,我会接受一个解决方案!
因为 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',
}
你可以使用这个逻辑:
$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"],
}