A Bash helper library for use by Puppet Tasks.
- Description
- Requirements
- Setup - The basics of getting started with bash_task_helper
- Usage - Configuration options and additional functionality
- Available functions
- Development - Guide for contributing to the module
This library handles producing a formatted error message for errors from stderr and uses indirection to munge PT_ environment variables.
- bash >= 4.0 on the primary Puppet server
To use this, include this module in a Puppetfile:
mod 'puppetlabs-bash_task_helper'
Add it to your task metadata:
{
"files": ["bash_task_helper/files/task_helper.sh"],
"input_method": "environment"
}
In your task, source the helper script and you can use the fail and sucess functions. Sucess outputs your provided output and fail outputs any provided output and appends stderr if applicable.
mymodule/tasks/mytask.sh
#!/bin/bash
declare PT__installdir
source "$PT__installdir/bash_task_helper/files/task_helper.sh"
if puppet resource service puppet | grep -q stopped; then
#Start the Puppet service
puppet resource service puppet ensure=running || task-fail "Couldn't start puppet service"
else
task-fail "Puppet is already running or not installed"
fi
task-succeed
This function ends the task. The task will return as failed. The function accepts an argument to set the task's return message, and an optional exit code to use.
$1
: A text string message to return in the task'smessage
key.$2
: A non-zero integer to use as the task's exit code.
Examples
End the task with a failing status.
task-fail
End the task with a failing status and message.
task-fail "task failed because of reasons"
End the task with a failing status, message, and exit code.
task-fail "task failed because of reasons" "127"
This function ends the task. The task will return as successful. The function accepts an argument to set the task's return message.
$1
: A text string message to return in the task'smessage
key.
Examples
End the task with a successful status.
task-succeed
End the task with a successful status and message.
task-succeed "task completed successfully
This function adds keys and values to the task output. It takes a key argument and a value argument and adds them to the task output when the task exits.
$1
: The output key. Should only include letters, digits, hyphens (-
), and underscores (_
).$2
: The output value. Should be a string. Values are automatically JSON-escaped.
Examples
Add values to the task output.
task-output "message" "an armadillo crossed the street"
task-output "maximum" "100GB"
This function adds keys and values to the task output. It takes a key argument and a value argument and adds them to the task output when the task exits. This function requires the task author pass valid JSON as the value, otherwise malformed task output will be produced.
$1
: The output key. Should only include letters, digits, hyphens (-
), and underscores (_
).$2
: The output value. Should be pre-formatted, valid JSON.
Examples
Add values to the task output.
task-output "number" 42
task-output "string" '"str"'
task-output "object" '{"one": "two"}'
task-output "array" '["zero", "one", "two"]'
Normally, tasks do not return all output if the task returns successfully. This function ensures the task returns all output, regardless of exit code.
$1
:true
orfalse
. Defaults totrue
. Passfalse
to turn verbose output off.
Examples
Enable verbose output.
task-verbose-output
Disable verbose output.
task-verbose-output false
This function attempts to JSON-escape text input. Because the function is constrained to rely only on lowest-common-denominator posix utilities, it may not be able to fully escape all text on all platforms.
$1
: The value to JSON-escape.
Examples
JSON-escape a printed string.
printf "a string\nwith newlines\n" | task-json-escape
JSON-escape the contents of a file.
task-json-escape < file.txt
This function is called by a task helper EXIT trap. It will print JSON task
return data on task termination. The return data will include all output keys
set using task-output
, and all uncaptured stdout/stderr output produced by the
script. This function should not be directly invoked, except inside a
user-created EXIT trap.
$1
: Exit code to terminate the task with. Defaults to$?
.
Examples
Exit the task.
task-exit
Exit the task with an exit code.
task-exit 1
If you are a PE user and need support using this module or are encountering issues, our Support team would be happy to help you resolve your issue and help reproduce any bugs. Just raise a ticket on the support portal.
If you have a reproducible bug or are a community user you can raise it directly on the Github issues page of the module here. We also welcome PR contributions to improve the module. Please see further details about contributing here.