Using variables and simple expressions
The simplest way to use a variable is to reference it directly by name.
For example, this plan takes a parameter called nodes
and passes it as the target list to a step:
parameters: nodes: type: TargetSpec steps: - command: hostname -f target: $nodes
Variables can also be interpolated into string values. The string must be double-quoted to allow interpolation. For example:
parameters: username: type: String steps: - task: echo message: "hello ${username}" target: $nodes
Many operations can be performed on variables to compute new values for step parameters or other fields.
Indexing arrays or hashes
You can retrieve a value from an Array or a Hash using the []
operator.
This operator can also be used when interpolating a value inside a string.
parameters: users: # Array[String] is a Puppet data type representing an array of strings type: Array[String] steps: - task: user::add target: 'host.example.com' parameters: name: $users[0] - task: echo target: 'host.example.com' parameters: message: "hello ${users[0]}"
Calling functions
You can call a built-in Bolt function or Puppet function to compute a value.
parameters: users: type: Array[String] steps: - task: user::add parameters: name: $users.first - task: echo message: "hello ${users.join(',')}"
Using code blocks
Some Puppet functions take a block of code as an argument. For instance, you can filter an array of items based on the result of a block of code.
The result of the filter
function is an array here, not a string, because
the expression isn't inside quotes
parameters: numbers: type: Array[Integer] steps: - task: sum description: "add up the numbers > 5" parameters: indexes: $numbers.filter |$num| { $num > 5 }