Forge Home

ruby_task_helper

A helper for writing tasks in ruby

289,421 downloads

43,532 latest version

4.5 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

  • 0.6.1 (latest)
  • 0.6.0
  • 0.5.1
  • 0.4.0
  • 0.3.0
  • 0.2.0
  • 0.1.0
released Oct 29th 2018
This version is compatible with:
  • Puppet Enterprise 2018.1.x, 2017.3.x, 2017.2.x, 2017.1.x, 2016.5.x, 2016.4.x
  • Puppet >= 4.7.0 < 6.0.0
  • , , , , , ,

Start using this module

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

Add this module to your Puppetfile:

mod 'puppetlabs-ruby_task_helper', '0.1.0'
Learn more about managing modules with a Puppetfile

Add this module to your Bolt project:

bolt module add puppetlabs-ruby_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-ruby_task_helper --version 0.1.0

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/ruby_task_helper — version 0.1.0 Oct 29th 2018

Ruby Task Helper

A Ruby helper library for writing Puppet tasks. It provides a class that handles error generation, simplifies JSON input and output, and makes testing your task easier. It requires Bolt >= 1.1 or Puppet Enterprise >= 2019.0.

Table of Contents

  1. Description
  2. Requirements
  3. Setup
  4. Usage

Description

This library handles parsing JSON input, serializing the result as JSON output, and producing a formatted error message for errors.

Requirements

This library works with Ruby 2.3 and later.

Setup

To use this library, include this module in a Puppetfile:

mod 'puppetlabs-ruby_task_helper'

Add it to your task metadata

{
  "files": ["ruby_task_helper/lib/task_helper.rb"],
  "input_method": "stdin"
}

Usage

When writing your task include the library in your script, extend the TaskHelper module, and write the task() function. The task() function should accept its parameters as symbols, and should return a hash. The following is an example of a task that uses the library

mymodule/tasks/task.rb

#!/usr/bin/env ruby

require_relative "../../ruby_task_helper/lib/task_helper.rb"

class MyClass < TaskHelper
  def task(name: nil, **kwargs) 
    {greeting: "Hi, my name is #{name}"}
  end 
end

if __FILE__ == $0
  MyClass.run
end

You can then run the task like any other Bolt task:

bolt task run mymodule::task -n target.example.com name="Robert'); DROP TABLE Students;--"

You can additionally provide detailed errors by raising a TaskError, such as

class MyTask < TaskHelper
  def task(**kwargs)
    raise TaskHelper::Error.new("my task error message",
                               "mytask/error-kind",
                               "Additional details")