Forge Home

deferlib

Deferred function library

249 downloads

121 latest version

4.7 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.0.4 (latest)
  • 2.0.3
  • 2.0.2
  • 2.0.1
released Feb 13th 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 < 9.0.0
  • , , , , , , , , ,

Start using this module

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

Add this module to your Puppetfile:

mod 'joknarf-deferlib', '2.0.4'
Learn more about managing modules with a Puppetfile

Add this module to your Bolt project:

bolt module add joknarf-deferlib
Learn more about using this module with an existing project

Manually install this module globally with Puppet module tool:

puppet module install joknarf-deferlib --version 2.0.4

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

joknarf/deferlib — version 2.0.4 Feb 13th 2024

License: MIT Puppet Forge Puppet Forge Downloads

deferlib

Table of Contents

  1. Description
  2. Setup - The basics of getting started with deferlib
  3. Usage
  4. Reference

Description

Control your resources parameters from client side without creating useless facts.

Provides functions library to use to get values for resource parameters from files/command executed on agent side (deferred), providing way to control resources from agent host local things without creating facts that executes on all servers.

Setup

Setup Requirements

Puppet agent/server >= 7

Beginning with deferlib

deferlib provides functions to use to modify resource parameters from host runnning puppet agent (instead of using facts for example).

Example:

# do not restart cron when maintenance flag file exists putting ensure to undef
# => function to read: unless file /etc/maintenance exists ensure running, (else ensure undef)
service { 'cron':
  ensure => deferlib::unless_file('/etc/maintenance', 'running'),
}

# noop mode when maintenance preventing starting service during operation
service { 'cron':
  ensure => 'running',
  noop   => deferlib::if_file('/etc/maintenance', true, false),
}

Usage

functions available:

deferlib::if_file()

deferlib::if_file(file, value, [default])

Description:

returns value if file exists else returns default (default: [])

Parameters:

file    : path to file to check existence
value   : value returned if file exists
default : value returned if file does not exist (default [])

Example:

# stop cron when cron_stop flag file exists
# => function to read: if file /etc/cron_stop exists ensure stopped else ensure running
service { 'cron':
  ensure => deferlib::if_file(/etc/cron_stop', 'stopped', 'running'),
}

deferlib::unless_file()

deferlib::unless_file(file, value, [default])

Description:

returns value if file does not exist else returns default (default: [])

Parameters:

file    : path to file to check existence
value   : value returned if file does not exist
default : value returned if file exists (default [])

Example:

# do not restart cron when maintenance flag file exists putting ensure to undef ([])
# => function to read: unless file /etc/maintenance exists ensure running, (else ensure undef)
service { 'cron':
  ensure => deferlib::unless_file('/etc/maintenance', 'running'),
}

deferlib::if_cmd()

deferlib::if_cmd(cmd, value, [options])

Description:

returns value if exit code of cmd is 0 else returns options[else] (default to [])

Parameters:

cmd     : shell code to execute
value   : value returned if exit status is 0
options : {
  'else'        => # value returned if exit status is not 0 (default: [])
  'user'        => # The user to run the command as 
  'group'       => # The group to run the command as
  'environment' => # A Hash of environment variables
}
options['environment'] : {
  '<variable name>' => # value of the environement variable
  ...
}

Example:

# ensure cron running if isproduction returns 0
service { 'cron':
  ensure => deferlib::if_cmd('/bin/isproduction', 'running', {
          'user'    => 'foo',
          'group'   => 'bar',
  }),
}

deferlib::unless_cmd()

deferlib::unless_cmd(cmd, value, [options])

Description:

returns value if exit code of cmd is not 0 else returns options[else] (default to [])

Parameters:

cmd     : shell code to execute
value   : value returned if exit status is not 0
options : Hash with optional settings
options : {
  'else'        => # value returned if exit status is 0 (default: [])
  'user'        => # The user to run the command as 
  'group'       => # The group to run the command as
  'environment' => # A Hash of environment variables
}
options['environment'] : {
  '<variable name>' => # value of the environement variable
  ...
}

Example:

# ensure cron running unless ismaintenance returns 0
service { 'cron':
  ensure => deferlib::unless_cmd('/bin/ismaintenance', 'running', {
          'user'    => 'foo',
          'group'   => 'bar',
  }),
}

deferlib::cmd()

deferlib::cmd(options)

Description:

returns output of options[command] if exit code is 0 else returns options['else'] (default to [])

parameters:

options : Hash with parameters
options : {
  'command'     => # Shell code to execute
  'match'       => # regexp to validate output (returns options['else'] if not)
  'else'        => # value returned if exit code is not 0
  'user'        => # The user to run the command as 
  'group'       => # The group to run the command as
  'environment' => # A Hash of environment variables
}
options['environment'] : {
  '<variable name>' => # value of the environement variable
  ...
}

Example:

# force ensure from local file content if exists, else ensure running
service { 'cron':
  ensure => deferlib::cmd'({
          'command' => 'cat /etc/cron_ensure',
          'else'    => 'running',
  }),
}
# use script cron_ensure from puppet module files
service { 'cron':
  ensure => deferlib::cmd({
          'command' => file("${module_name}/cron_ensure"),
  }),  
}

Reference

Reference