Puppet 复制文件(如果不为空)

Puppet copy file if not empty

我有一个要求,我需要在 puppet master 上检查一个文件,只有当它不为空时才将其复制到代理。

到目前为止我有以下内容:

  exec {
    'check_empty_file':
      provider => shell,
      command  => "test -s puppet:////path/to/puppetmaster/file",
      returns  => ["0", "1"],
  }

  if $check_empty_file == '0' {
    file {
      'file_name':
        path    => '/path/to/agent/file',
        alias   => '/path/to/agent/file',
        source  => "puppet:///path/to/puppetmaster/file",
    }
  }

但是没用。任何帮助表示赞赏。谢谢!

您不能使用 Exec 资源来执行检查,因为您需要在目录构建期间执行评估,并且在构建目录之后才会应用资源。此外,test 命令测试指定的 路径 是否存在。它不知道 URLs,即使知道,也不太可能识别或处理 puppet: URL 方案。此外,资源标题和变量名称之间没有任何关联。

要在目录构建时收集数据,您需要一个 puppet function. It is not that hard to add your own custom function to Puppet, but you don't need that for your case -- the built-in file() 函数来满足您的目的。它可能看起来像这样:

$file_content = file('<module-name>/<file-name>')

if $file_content != '' {
  file { '/path/to/target/file':
    ensure  => 'file',
    content => $file_content,
    # ...
  }
}