Forge Home

python_task_helper

A Python helper library for use by Puppet Tasks

149,858 downloads

57,233 latest version

4.8 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.5.0 (latest)
  • 0.4.3
  • 0.4.0
  • 0.3.0
  • 0.2.0
  • 0.1.3
  • 0.1.2
released Jan 22nd 2021
This version is compatible with:
  • Puppet Enterprise 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, 2019.8.x, 2019.7.x, 2019.5.x, 2019.4.x, 2019.3.x, 2019.2.x, 2019.1.x, 2019.0.x, 2018.1.x, 2017.3.x, 2017.2.x, 2017.1.x, 2016.5.x, 2016.4.x
  • Puppet >= 4.7.0 < 8.0.0
  • , , , , , , , , ,

Start using this module

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

Add this module to your Puppetfile:

mod 'puppetlabs-python_task_helper', '0.5.0'
Learn more about managing modules with a Puppetfile

Add this module to your Bolt project:

bolt module add puppetlabs-python_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-python_task_helper --version 0.5.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/python_task_helper — version 0.5.0 Jan 22nd 2021

python_task_helper

A Python helper library for use by 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 and Puppet Enterprise >= 2019.0.

Table of Contents

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

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 Python 2.7 and later.

Setup

To use this library, include this module in a Puppetfile

mod 'puppetlabs-python_task_helper'

Add it to your task metadata (note that the helper expects to read arguments on stdin)

{
  "files": ["python_task_helper/files/task_helper.py"],
  "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 must take a hash of parameters as input (even if it's an empty hash), and must return a hash. The following is an example of a task that uses the library

#!/usr/bin/env python

import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'python_task_helper', 'files'))
from task_helper import TaskHelper

class MyTask(TaskHelper):
    def task(self, args):
        return {'result': 'Hi, my name is '+args['name']}

if __name__ == '__main__':
    MyTask().run()

You can then run the task like any other Bolt task

bolt task run mymodule::task -t target.example.com name='Robert'

You can find this example in examples, as well as an example test in tests. For a real task, examples would be renamed to tasks.

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

class MyTask(TaskHelper):
    def task(self, args):
        raise TaskError('my task errored', 'mytask/error_kind', {'location': 'task entry'})

Debugging

When writing your task, it can be helpful to write debugging statements to locate the source of any errors. The library includes a debug method that accepts arbitrary values and logs it as a debugging statement. If the task errors, the list of debugging statements will be included in the resulting error message.

The list of debugging statements can also be accessed from the task itself by accessing the debug_statements attribute. This can be used to include the debugging statements in a TaskError that you explicitly raise.

Adding a debugging statement:

self.debug('Result of method call: {}'.format(result))

Adding the list of debugging statements to a TaskError:

raise TaskError('my task error message',
                'mytask/error-kind',
                { 'debug': self.debug_statements })