Puppet - 有条件地覆盖内置默认值

thi*_*ice 2 puppet

以 ssh_authorized_key 的包装为例:

define sshauthkeys::helper ($user,$ensure='present') {


        ssh_authorized_key { "puppet: ${name2} ${user}":
          ensure => $ensure,
          type => $ssh_keys["${name2}"]["type"],
          key => $ssh_keys["${name2}"]["key"],
          user => "${user}"
        }
}
Run Code Online (Sandbox Code Playgroud)

假设我想为该资源类型引入对“目标”参数的支持,我会执行以下操作:

define sshauthkeys::helper ($user,$ensure='present', $target='') {
Run Code Online (Sandbox Code Playgroud)

问题是,如果我想使用从 $user 的 homedir 派生的 puppet 的合理默认值 - 并在边缘情况下覆盖它,我将失去在所有其他情况下设置默认值所做的工作的好处。

有没有一种方法可以选择性地覆盖该值而不必编写两个函数?

更好地描述问题:

定义 ssauthkeys::helper ($user,$ensure='present', $target='') {

    ssh_authorized_key { "puppet: ${name2} ${user}":
      // etc etc...
      target = $target 
      // ^--- Here, I'm forcing it to '' - how do I use the puppet 
      // built-in derivation, and only override optionally?
    }
Run Code Online (Sandbox Code Playgroud)

}

lar*_*sks 5

在这种情况下,设置target => undef与根本不指定它是一回事,因此您可以像这样编写包装器:

define sshauthkeys::helper ($user,
  $ensure='present',
  $target=undef
) {
    ssh_authorized_key { "puppet: ${name2} ${user}":
      ensure => $ensure,
      target => $target,
      type   => $ssh_keys["${name2}"]["type"],
      key    => $ssh_keys["${name2}"]["key"],
      user   => "${user}"
    }
}
Run Code Online (Sandbox Code Playgroud)