Interpolation
Interpolation allows strings to contain expressions, which can be replaced with their values. You can interpolate any expression that resolves to a value, except for statement-style function calls. You can interpolate expressions in double-quoted strings, and in heredocs with interpolation enabled.
To interpolate an expression, start with a dollar sign and wrap the
expression in curly braces, as in: "String content ${<EXPRESSION>} more
content"
.
The dollar sign doesn’t have to have a space in front of it. It can be placed directly after any other character, or at the beginning of the string.
An interpolated expression can
include quote marks that would end the string if they occurred outside the
interpolation token. For example: "<VirtualHost
*:${hiera("http_port")}>"
.
Preventing interpolation
If you want a string to include a literal sequence that looks like an interpolation
token, but you don't want Puppet to try to evaluate
it, use a quoting syntax that disables interpolation (single quotes or a
non-interpolating heredoc), or escape the dollar sign with \$
.
Short forms for variable interpolation
The most common thing to interpolate into a string is the value of a variable. To make this easier, Puppet has some shorter forms of the interpolation syntax:
$myvariable
$myclass::myvariable
.${myvariable}
${myclass::myvariable}
."Using interface ${::interfaces.split(',')[3]} for broadcast"However, this doesn’t work if the variable’s name overlaps with a language keyword. For example, if you had a variable called
$inherits
, you would have to use
normal-style
interpolation:"Inheriting ${$inherits.upcase}."
Conversion of interpolated values
Puppet converts the value of any interpolated expression to a string using these rules:
Data type | Conversion |
---|---|
String | The contents of the string, with any quoting syntax removed. |
Undef | An empty string. |
Boolean | The string 'true' or 'false' . |
Number | The number in decimal notation (base 10). For floats, the
value can vary on different platforms. Use the sprintf function for more
precise formatting. |
Array | A pair of square brackets ([ and ] )
containing the array’s elements, separated by a comma and a
space (, ), with no trailing
comma. Each element is converted to a string using these
rules. |
Hash | A pair of curly braces ({ and } )
containing a <KEY> =>
<VALUE> string for each key-value pair,
separated by a comma and a space (,
), with no trailing comma. Each key and value is
converted to a string using these rules. |
Regular expression | A stringified regular expression. |
Resource reference or data type | The value as a string. |