Forge Home

bash_task_helper

A Bash helper library for use by Puppet Tasks.

14,150 downloads

765 latest version

5.0 quality score

We run a couple of automated
scans to help you access a
module's quality. Each module is
given a score based on how well
the author has formatted their
code and documentation and
modules are also checked for
malware using VirusTotal.

Please note, the information below
is for guidance only and neither of
these methods should be considered
an endorsement by Puppet.

Version information

  • 2.1.1 (latest)
  • 2.1.0
  • 2.0.0
  • 1.0.0
released Jul 17th 2024
This version is compatible with:
  • Puppet Enterprise 2023.8.x, 2023.7.x, 2023.6.x, 2023.5.x, 2023.4.x, 2023.3.x, 2023.2.x, 2023.1.x, 2023.0.x, 2021.7.x, 2021.6.x, 2021.5.x, 2021.4.x, 2021.3.x, 2021.2.x, 2021.1.x, 2021.0.x
  • Puppet >= 7.0.0 < 9.0.0
  • , , , , ,

Start using this module

  • r10k or Code Manager
  • Bolt
  • Manual installation
  • Direct download

Add this module to your Puppetfile:

mod 'puppetlabs-bash_task_helper', '2.1.1'
Learn more about managing modules with a Puppetfile

Add this module to your Bolt project:

bolt module add puppetlabs-bash_task_helper
Learn more about using this module with an existing project

Manually install this module globally with Puppet module tool:

puppet module install puppetlabs-bash_task_helper --version 2.1.1

Direct download is not typically how you would use a Puppet module to manage your infrastructure, but you may want to download the module in order to inspect the code.

Download

Documentation

puppetlabs/bash_task_helper — version 2.1.1 Jul 17th 2024

bash_task_helper

A Bash helper library for use by Puppet Tasks.

Table of Contents

  1. Description
  2. Requirements
  3. Setup - The basics of getting started with bash_task_helper
  4. Usage - Configuration options and additional functionality
  5. Available functions
  6. Development - Guide for contributing to the module

Description

This library handles producing a formatted error message for errors from stderr and uses indirection to munge PT_ environment variables.

Requirements

  • bash >= 4.0 on the primary Puppet server

Setup

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"
}

Usage

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

Available functions

task-fail

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's message 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"

task-succeed

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's message 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

task-output

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"

task-output-json

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"]'

task-verbose-output

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 or false. Defaults to true. Pass false to turn verbose output off.

Examples

Enable verbose output.

task-verbose-output

Disable verbose output.

task-verbose-output false

task-json-escape

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

task-exit

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

How to Report an issue or contribute to the module

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.