代码之家  ›  专栏  ›  技术社区  ›  user1074593

Puppet:仅当文件存在时才使用文件资源

  •  1
  • user1074593  · 技术社区  · 7 年前

    我想做的很简单:

    1.

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

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

    2.

    /源/文件 不存在,我不希望它执行此任务。

    puppet: if one file exists then copy another file over

    有没有更好的方法来完成这项任务? 理想情况下,我希望只使用“file”,而不要使用“exec”。但在这一点上,我会满足于一个解决方案!

    2 回复  |  直到 7 年前
        1
  •  5
  •   Alex Harvey    7 年前

    因为Puppet是一种只声明最终状态的声明性语言,所以命令逻辑,例如您所描述的-如果是a,do X-通常很难表达。

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

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

    exec { 'my_file_copy':
      command => 'cp /source/file /target/file',
      onlyif  => 'test -e /source/file',
      creates => '/target/file',
      path    => '/bin',
    }
    
        2
  •  0
  •   Innocent Anigbo    7 年前

    您可以使用以下逻辑:

    $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"],
      }