簡體   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