使用木偶检查自定义文件

Checking for custom file using puppet

我的 puppet 清单的一部分检查是否存在自定义 sshd_config。如果找到一个,我会使用它。如果不是,那么我使用我的默认值。我只是想知道是否有更多 "puppet" 方法来做到这一点

if file("/etc/puppetlabs/puppet/files/${::fqdn}/etc/ssh/sshd_config", '/dev/null') != '' {
  $sshd_config_source = "puppet:///private/etc/ssh/sshd_config"
} else {
  $sshd_config_source = "puppet:///public/etc/ssh/sshd_config"
}

file { '/etc/ssh/sshd_config':
  ensure => 'present',
  mode   => '600',
  source => $sshd_config_source,
  notify => Service['sshd'],
}

此代码有效,但有点奇怪,因为 file 我必须在 puppet master 上给它完整路径,但是在分配 $sshd_config_source 时我必须使用 puppet 文件服务器路径(puppet:///private/etc...).

有更好的方法吗?

file 类型的一个鲜为人知的特性是您可以提供多个 source 值。

来自文档:

Multiple source values can be specified as an array, and Puppet will use the first source that exists. This can be used to serve different files to different system types:

file { "/etc/nfs.conf":
  source => [
    "puppet:///modules/nfs/conf.$host",
    "puppet:///modules/nfs/conf.$operatingsystem",
    "puppet:///modules/nfs/conf"
  ]
}

所以您应该按顺序指定特定文件和通用文件 URL,Puppet 会为您做正确的事情。