【问题标题】:Puppet conditional assignment and += operatorPuppet 条件赋值和 += 运算符
【发布时间】:2015-06-03 00:36:38
【问题描述】:

在 puppet 中,我想根据nodes.pp 中的配置编写一个带有字符串的文件。

  • nodes.pp 定义了 $sslClientCertConfig 变量,该变量具有 loadBalancerIp 属性。
  • 我想创建一个配置字符串,我们称之为$config,不再重复字符串代码。配置字符串应该是
    • A:如果loadBalancerIp是一个有效的字符串,则为字符串1和2的组合
    • B:如果loadBalancerIp为空字符串,则只有字符串2
  • 然后,使用该配置字符串,我想设置另一个变量。

我知道 puppet 只允许在每个范围内声明一个变量名,但我不知道它是否支持 += 运算符,或者下面的变量声明是否适用于稍后的 if/else 子句。

这是我想尝试的:

if $sslClientCertConfig {
    $loadBalancerIp = $sslClientCertConfig[loadBalancerIp]

    $config

    if $loadBalancerIp {
          $config += "string1"
    }
    $config += "string2"
}

if $config {
   $custom = "true"
} else {
    $custom = "false"
}

puppet 支持这种模式吗?有什么办法可以改进吗?

【问题讨论】:

  • 您要解决的根本问题是什么?这是环境的不同吗?虽然there is a Puppet pattern for string concatenation,但可能有更好的方法,也许使用 Hiera。
  • 在您的示例中,您不需要任何+=,只需使用:if loadBalancerIp { $config = "string1" } else { $config = "string2" }
  • 避免+= 它可能不会像您认为的那样做。

标签: string variables scope puppet


【解决方案1】:

您不能在 puppet 中使用 reassign 变量。而是删除 $configempty 声明和 试试下面的代码

if $loadBalancerIp {
      $prefix = "string1"
}
$config = "${prefix}string2"

例子:

$prefix1 = "pref1"
$config1 = "${prefix1}blabla1"
$config2 = "${prefix2}blabla2"
notify { $config1: }
notify { $config2: }

注意:/Stage[main]/Main/Notify[pref1blabla1]/message:将“消息”定义为“pref1blabla1” 注意:/Stage[main]/Main/Notify[blabla2]/message:将'message'定义为'blabla2'

更新:注意automatic string to boolean conversion.

【讨论】:

    猜你喜欢
    • 2018-06-09
    • 2011-03-11
    • 2010-11-21
    • 2011-09-18
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    • 2011-11-16
    • 2018-08-14
    相关资源
    最近更新 更多