繁体   English   中英

人偶:仅当文件存在时文件资源

[英]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不存在,则我不希望它执行此任务。

我真的在为这种逻辑而苦苦挣扎。 我尝试了以下解决方案,但在运行木偶时抛出异常。

up:如果存在一个文件,则将另一个文件复制到

是否有更好的方法来完成此任务? 理想情况下,我只想使用“文件”,而避免使用“ 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"],
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM