Saturday, January 13, 2018

Puppet: concatenate existing variable to make new variable

Oftenly, we have to need to create a new variable by concatenating two existing variable. Below is some of way we can do.

CODE:

  1 $first = 'jelly'
  2 $last = 'fish'
  3 $full = "$first$last"
  4 notice("I am $full")


OUTPUT

Notice: Scope(Class[main]): I am jellyfish


Modify code in a different way to see how it change output.    

CODE:

$full = "${first}$last"

OUTPUT:  
Notice: Scope(Class[main]): I am jellyfish


CODE:

$full = "${first}${last}"

OUTPUT: 

Notice: Scope(Class[main]): I am jellyfish


CODE:
   
$full = "${first}_${last}"

OUTPUT: 

Notice: Scope(Class[main]): I am jelly_fish


CODE:

  1 $first = ['jelly', 'start']  # Now, it is list
  2 $last = 'fish'

OUTPUT: ( Possibly, this is not what you want to use)
Notice: Scope(Class[main]): I am [jelly, start]_fish


CODE: Looping using each does not work on Puppet 3

$first = ['jelly', 'star']
$last = 'fish'
$first.each | $name | {
  $full = "${name}_${last}"
  notice("I am $full")
}

OUTPUT:

Notice: Scope(Class[main]): I am jelly_fish
Notice: Scope(Class[main]): I am star_fish