How do you concatenate strings in a Puppet .pp file?

String ConcatenationPuppet

String Concatenation Problem Overview


Here is my naive approach:

# puppet/init.pp
$x = 'hello ' + 
     'goodbye'

This does not work. How does one concatenate strings in Puppet?

String Concatenation Solutions


Solution 1 - String Concatenation

Keyword variable interpolation:

$value = "${one}${two}"

Source: http://docs.puppetlabs.com/puppet/4.3/reference/lang_variables.html#interpolation

Note that although it might work without the curly braces, you should always use them.

Solution 2 - String Concatenation

I use the construct where I put the values into an array an then 'join' them. In this example my input is an array and after those have been joined with the ':2181,' the resulting value is again put into an array that is joined with an empty string as separator.

$zookeeperservers = [ 'node1.example.com', 'node2.example.com', 'node3.example.com' ]
$mesosZK = join([ "zk://" , join($zookeeperservers,':2181,') ,":2181/mesos" ],'')

resulting value of $mesosZK

zk://node1.example.com:2181,node2.example.com:2181,node3.example.com:2181/mesos

Solution 3 - String Concatenation

Another option not mentioned in other answers is using Puppet's sprintf() function, which functions identically to the Ruby function behind it. An example:

$x = sprintf('hello user %s', 'CoolUser')

Verified to work perfectly with puppet. As mentioned by chutz, this approach can also help you concatenate the output of functions.

Solution 4 - String Concatenation

The following worked for me.

puppet apply -e ' $y = "Hello" $z = "world" $x = "$y $z" notify { "$x": } '
notice: Hello world
notice: /Stage[main]//Notify[Hello world]/message: defined 'message' as 'Hello world'
notice: Finished catalog run in 0.04 seconds

The following works as well:

$abc = "def"

file { "/tmp/$abc":

Solution 5 - String Concatenation

You could use the join() function from puppetlabs-stdlib. I was thinking there should be a string concat function there, but I don't see it. It'd be easy to write one.

Solution 6 - String Concatenation

As stated in docs, you can just use ${varname} interpolation. And that works with function calls as well:

$mesosZK = "zk://${join($zookeeperservers,':2181,')}:2181/mesos"
$x = "${dirname($file)}/anotherfile"

Could not use {} with function arguments though: got Syntax error at '}'.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrlandsterView Question on Stackoverflow
Solution 1 - String ConcatenationczervikView Answer on Stackoverflow
Solution 2 - String ConcatenationNiels BasjesView Answer on Stackoverflow
Solution 3 - String ConcatenationMifeetView Answer on Stackoverflow
Solution 4 - String ConcatenationAlbert TurriView Answer on Stackoverflow
Solution 5 - String Concatenationmc0eView Answer on Stackoverflow
Solution 6 - String ConcatenationAndrey RegentovView Answer on Stackoverflow