Puppet:覆盖类似资源的 class 声明

Puppet: Override resource-like class declaration

希望比我有更多木偶经验的人可以给我一些关于我正在尝试的配置的建议。

我正在使用 saz/ssh 作为此模块。

我的目标

  1. 使用这个模块创建一个默认的 ssh 配置,它被推送到所有 redhat 7/8 服务器。
  2. 允许通过 hiera 在单个服务器基础上覆盖单个 ssh 选项

为了实现#1,我创建了自己的 'module',它声明了 ssh::server class,并定义了我的选项。我相信这称为类似资源的 class 声明。 (为了便于阅读缩短了代码)

#
class profiles::sshd::config {
  class { 'ssh::server':
    validate_sshd_file   => true,
    storeconfigs_enabled => false,
    options              => {
      'Port'                      => [22],
      'AddressFamily'             => 'any',
      'ListenAddress'             => '0.0.0.0',
      'Protocol'                  =>  '2',
      'HostKey'                   => ['/etc/ssh/ssh_host_rsa_key', '/etc/ssh/ssh_host_ecdsa_key','/etc/ssh/ssh_host_ed25519_key'],
      'RekeyLimit'                =>  'default none',
      'SyslogFacility'            => 'AUTHPRIV',
      'LogLevel'                  => 'INFO',
    }
  }
}

然后我将这个 'module' 包含在我的基础中。这很好用,直到我想在单个服务器的基础上覆盖这些选项中的任何一个。我无法让 hiera 工作。

声明

ssh::server::options:
    Port: [2222]

例如,在服务器的 fqdn yaml 文件中,什么都不做。如果我直接包含 ssh::server 模块,而不是通过我自己的 class,hiera 覆盖工作。

我已经尝试了各种用于 hiera 覆盖的语法,但从未在人偶 运行 上获得任何输出,除非我直接使用 class。例如我试过:

ssh::server:options:
profiles::sshd::config:
profiles::sshd::config::ssh::server:
profiles::sshd::config::options:
profiles::sshd::config::ssh::server::options:

有没有更好的方法可以做我想做的事,或者任何人都可以帮助我如何以这种方式声明 hiera 覆盖?

任何帮助都会非常有用

to achieve #1 I've created my own 'module'

... 在其中,一个名为 profile::sshd::config ...

的 class

which declares the ssh::server class, and defines my options. I believe this is called a resource-like class declaration.

是的,您已经编写了类似资源的 class 声明。这通常是个坏主意。有几个原因,但其中一个是...

[when] I want to override any of these options on an individual server basis[,] I can't get hiera to work.

Class 通过类似资源的 class 声明指定的参数值优先于通过任何其他可用方式(包括 Hiera 数据)指定的值。您不能覆盖它们。

您有两个主要选择:

  1. 为包装 class 的每个您希望能够自定义的包装参数提供自己的参数。使用它们来初始化包装的 class:

    class profiles::sshd::config(
        Boolean $validate_sshd_file = true,
        Boolean $storeconfigs_enabled = false,
        Hash $options = { ... },
    ) {
      class { 'ssh::server':
        validate_sshd_file   => $validate_sshd_file,
        storeconfigs_enabled => $storeconfigs_enabled,
        options              => $options,
      }
    }
    

    假设您随后通过类似 include 的声明class profiles::sshd::config 声明,您可以通过 Hiera 自定义其参数。但请注意

  2. 如果您更改为 class ssh::server 的类似包含的声明,那么您既可以通过 Hiera 为其定义默认参数,也可以通过 Hiera 自定义其参数。

也有一条中间道路:给定一个类似资源的声明 ssh::server,您 可以 使用 Hiera 自定义除指定参数之外的任何和所有参数在其声明中。因此,如果您希望避免某些参数可用于自定义,那么您可以考虑(仅)通过类似资源的 class 声明显式声明它们,其余的依赖于 Hiera。但是请注意,这仍然会让您面临一些其他问题,如资源类 class 声明(这将是一个单独的问题)。