Tasks
Stanislav Tiurikov edited this page Feb 12, 2020
·
7 revisions
Provisioning tasks define a list of action to run on specific set of hosts. We can define as many tasks as we want:
inlineInventory {
...
}.provision {
task actions: {
// some actions here
}
task name: 'simple task', actions: {
// some actions here
}
task name: 'simple task', parallel: 5, actions: {
// some actions here
}
task name: 'simple task', parallel: 5, filter: {'db_tag:true' && 'priority_tag:1'}, actions: {
// some actions here
}
task(name: 'simple task', filter: {'db_tag:true' && 'priority_tag:1'}) {
parallel = 5
actions = {
// some actions here
}
}
}
Task element can have following attributes:
Attribute | Type | Optional | Default Value | Description |
---|---|---|---|---|
name | String | yes | 'unnamed task' | a human readable name of the task |
filter | Closure | yes | { true } | a filtering expression to specify a subset of node to run actions on |
parallel | Integer | yes | 1 | a number of nodes the task can be run on at the same time |
actions | Closure | no | empty | a set of actions to run |
(deprecated) onSuccess | Closure | yes | empty closure | a set of actions to run if the task has finished without errors |
(deprecated) onFailure | Closure | yes | throw exception | a set of actions to run if the task has finished with an error |
With the node filtering expression (NFE) we can define where to run the task. NFE is a closure which operates with string representation of the node tags. It is possible to use logical operators in such expressions. Here are some examples:
inlineInventory {
node id: 'node_A', ..., tags: [web: true, priority: 1]
node id: 'node_B', ..., tags: [web: true, priority: 2]
node id: 'node_C', ..., tags: [db: true, priority: 1]
node id: 'node_D', ..., tags: [db: true, priority: 2]
node id: 'node_E', ..., tags: [service: true, priority: 1]
node id: 'node_F', ..., tags: [service: true, priority: 2]
}.provision {
task name: 'run on node_A and node_B', filter: {'web:true'}, actions: {
// install packages here
}
task name: 'run on node_A, node_B, node_C, node_D', filter: {'web:true' || 'db:true'}, actions: {
// update service configs here
}
task name: 'run on node_C only', filter: {'db:true' && !'priority:2'}, actions: {
// launch a new container here
}
task name: 'run on all nodes but node_F', filter: { !('service:true' && 'priority:2') }, actions: {
// collect system logs here
}
}
You can access current inventory implicitly:
inlineInventory {
...
}.provision {
def inventorySize = inventory.size()
}
or explicitly:
inlineInventory {
...
}.provision { currentInventory ->
def inventorySize = currentInventory.size()
def nodes = currentInventory.filter { 'someTag:someValue' }
}
It is possible to access to current node which the task is running for:
inlineInventory {
...
}.provision {
task name: 'example 1', actions: {
// variable node is always available in a task context
info "current node: $node"
}
task name: 'example 2', actions: { currentNode ->
// we can explicitly give a name to the node variable
info "current node: $currentNode"
}
}