What Is a Plan?
A plan is a program that runs several tasks, commands and scripts across targets with logic: 'check the load balancer, drain the server, patch it, reboot it, test it, put it back'. Plans make multi-step operations repeatable and safe. Two languages are available: YAML plans (simple, readable) and Puppet language plans (more power).
A YAML Plan
yaml
# modules/ops/plans/health_check.yaml
---
description: Check that web servers answer correctly
parameters:
targets:
type: TargetSpec
description: Servers to check
port:
type: Integer[1, 65535]
default: 80
steps:
- name: service_status
task: service
targets: $targets
parameters:
action: status
name: nginx
- name: http_check
command: "curl -s -o /dev/null -w '%{http_code}' http://localhost:${port}/health"
targets: $targets
description: Ask the local health endpoint
return: $http_check
bash
bolt plan show
bolt plan show ops::health_check
bolt plan run ops::health_check targets=web port=8080
In YAML plans, step results are saved in a variable named after the step ($http_check). A step failure stops the plan unless you set catch_errors.
A Puppet Language Plan
puppet
# modules/ops/plans/deploy.pp
plan ops::deploy (
TargetSpec $targets,
String $version,
Integer $batch_size = 1,
) {
$nodes = get_targets($targets)
out::message("Deploying ${version} to ${nodes.length} servers")
# Take servers in small groups
$nodes.slice($batch_size).each |$batch| {
out::message("Batch: ${batch.map |$t| { $t.name }.join(', ')}")
# 1. Remove from the load balancer
run_task('ops::lb_drain', $batch, 'wait' => 20)
# 2. Deploy
$result = run_task('ops::install_app', $batch, 'version' => $version, '_catch_errors' => true)
# 3. Check
if $result.ok {
$check = run_command('curl -fs http://localhost:8080/health', $batch, '_catch_errors' => true)
if $check.ok {
run_task('ops::lb_enable', $batch)
} else {
out::message('Health check failed. Rolling back.')
run_task('ops::install_app', $batch, 'version' => 'previous')
fail_plan('Deployment failed health check', 'ops/health-failed', { 'targets' => $check.error_set.names })
}
} else {
fail_plan('Install failed', 'ops/install-failed', { 'targets' => $result.error_set.names })
}
}
return "Deployed ${version} to all targets"
}
| Function | Purpose |
run_task(task, targets, params) | Run a task |
run_command(cmd, targets) | Run a shell command |
run_script(path, targets) | Run a script |
upload_file, download_file | Move files |
run_plan('other::plan', ...) | Call another plan |
apply(targets) { ... } | Apply Puppet code |
get_targets, get_target | Resolve names to target objects |
facts($target), run_task('facts') | Read node facts |
prompt, out::message, log::info | Interaction and output |
wait_until_available | Wait until a node accepts connections (after reboot) |
fail_plan, catch_errors | Stop or handle errors |
parallelize(targets) |$t| { ... } | Run blocks in parallel |
Handling Results
puppet
$rs = run_command('systemctl is-active nginx', $targets, '_catch_errors' => true)
$rs.each |$r| {
out::message("${r.target.name}: ${r.value['stdout'].strip}")
}
$failed = $rs.error_set # ResultSet of failures
if !$failed.empty {
out::message("Failed on: ${failed.names}")
}
$ok_targets = $rs.ok_set.targets # use only the good ones in the next step
# Read the exit code and output
$r = run_command('grep -c error /var/log/app.log', $targets, '_catch_errors' => true).first
notice($r['exit_code'])
Rolling Patch Plan
puppet
plan ops::rolling_patch (
TargetSpec $targets,
Integer $batch_size = 2,
Boolean $reboot = true,
) {
$all = get_targets($targets)
$all.slice($batch_size).each |$batch| {
out::message("Patching: ${batch.map |$t| { $t.name }.join(', ')}")
# 1. Stop the Puppet agent from changing things mid-patch
run_command('puppet agent --disable "bolt patching"', $batch)
# 2. Remove from load balancer
run_task('ops::lb_drain', $batch, 'wait' => 30)
# 3. Update packages
run_task('package', $batch, 'action' => 'upgrade', 'name' => '*')
# 4. Reboot if needed and wait
if $reboot {
run_task('reboot', $batch, 'timeout' => 300)
wait_until_available($batch, 'wait_time' => 300)
}
# 5. Check health
$health = run_command('curl -fs http://localhost/health', $batch, '_catch_errors' => true)
unless $health.ok {
run_command('puppet agent --enable', $batch)
fail_plan("Health check failed on ${health.error_set.names}", 'ops/patch-health')
}
# 6. Back to service
run_task('ops::lb_enable', $batch)
run_command('puppet agent --enable', $batch)
run_command('puppet agent -t', $batch, '_catch_errors' => true)
}
}
bash
bolt plan run ops::rolling_patch targets=web batch_size=2
bolt plan run ops::rolling_patch targets=web reboot=false --noop # preview where supported
The batch size is a safety setting: A batch size of 1 or 10% of the group keeps most servers serving traffic. If any batch fails, the plan stops and the rest of the servers remain untouched.
Using Prompts and Confirmations
puppet
$answer = prompt("Deploy ${version} to production? (yes/no)", 'default' => 'no')
if $answer != 'yes' {
fail_plan('Cancelled by operator')
}
Plan with Puppet Agent Runs
puppet
# Trigger an agent run on selected nodes now
run_command('/opt/puppetlabs/bin/puppet agent -t', $targets, '_catch_errors' => true)
Puppet Enterprise also offers task and plan orchestration from the console and API (puppet job run). With open source you can combine Bolt with your CI system or schedule plans from tools such as Jenkins or GitLab CI.
Testing Plans
- Run on a lab group first, with a batch size of 1
- Use
bolt plan run ... --noop for tasks that support it
- Use the
BoltSpec::Plans library to unit test plans
- Keep tasks small and idempotent so a failed plan can be re-run
- Log what happens with
out::message and store --format json output
Production Safety Checklist
| Check | Why |
| Small batches with a health check after each | Limit the blast radius |
_catch_errors plus explicit fail_plan | Stop in a controlled way, with context |
| Disable Puppet runs during manual operations | Avoid agent and Bolt fighting each other |
| Pre-checks (disk space, cluster health) | Do not start a deploy that must fail |
| Rollback step defined | Know how to return to a good state |
| Approval prompt or CI approval for production | Human control for risky actions |
| Audit: who ran what, when | Use CI logs or the Bolt analytics-free log file |
Practice Tasks
- Write a YAML plan that runs two commands on a group and returns the output
- Write a Puppet plan with
slice that restarts a service in batches of two
- Add error handling so the plan stops when a health check fails
- Create a
rolling_patch plan for your lab and run it with batch size 1
- Call one plan from another with
run_plan
Conclusion
Plans turn tasks into reliable procedures. Use batches, health checks, error handling and clear failure messages, disable agent runs during manual work, and always test on a lab group first.
Quick Summary: YAML or Puppet plans; run_task/run_command; slice for batches; _catch_errors + fail_plan; health check before continuing.