0

In puppet, I want to write a file with a string based on configuration in nodes.pp.

  • nodes.pp defines the $sslClientCertConfig variable, which has a loadBalancerIp property.
  • I want to create a configuration string, we'll call it $config, without repeating the string code. The configuration string should be either
    • A: if loadBalancerIp is a valid string, a combination of strings 1 and 2
    • B: if loadBalancerIp is an empty string, only string 2
  • Then, using that configuration string, I want to set another variable.

I know that puppet only allows a single declaration of a variable name per scope, but I do not know if it supports the += operator, or if the variable declarations below will work for the if/else clause later.

Here's what I want to try:

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

    $config

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

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

Is this pattern supported by puppet? Is there a way I can improve this?

jxa
  • 47
  • 8
  • What is the underlying problem that you are trying to solve? Is this a difference in environments? While [there is a Puppet pattern for string concatenation](http://stackoverflow.com/q/14885263/78845), there is likely to be a better way, perhaps using Hiera. – Johnsyweb Jun 03 '15 at 00:45
  • In your example you do not need any `+=`, just use: `if loadBalancerIp { $config = "string1" } else { $config = "string2" }` – kkamilpl Jun 03 '15 at 08:07
  • Avoid `+=` it likely does not do what you think it does. – Felix Frank Jun 04 '15 at 10:05

1 Answers1

2

You cannot reassign variables in puppet. Instead remove empty declaration of $config and try the following code

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

Example:

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

Notice: /Stage[main]/Main/Notify[pref1blabla1]/message: defined 'message' as 'pref1blabla1' Notice: /Stage[main]/Main/Notify[blabla2]/message: defined 'message' as 'blabla2'

UPDATE: Be aware of automatic string to boolean conversion.

kkamilpl
  • 2,492
  • 9
  • 21