Version information
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
Add this module to your Puppetfile:
mod 'joknarf-deferlib', '2.0.4'
Learn more about managing modules with a PuppetfileDocumentation
deferlib
Table of Contents
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
Table of Contents
Functions
deferlib::cmd
: returns output of deferred shell command execution or options[else] if commmand exit code not 0 or output does not match options[match]deferlib::if_cmd
: returns value if deferred shell command execution exit code 0 else returns options[else]deferlib::if_file
: returns value if file exists else returns defaultdeferlib::unless_cmd
: returns value if deferred shell command execution exit code not 0 else returns options[else]deferlib::unless_file
: returns value if file does not exist else returns default
Functions
deferlib::cmd
Type: Puppet Language
returns output of deferred shell command execution or options[else] if commmand exit code not 0 or output does not match options[match]
Examples
ensure cron from file content on agent side
service { 'cron':
ensure => deferlib::cmd({
'command' => 'cat /etc/cron_ensure',
'match' => '^(running|stopped)$',
'else' => 'running',
'user' => 'foo',
'group' => 'bar',
'environment' => { 'myvar' => 'myvalue' },
}),
}
deferlib::cmd(Hash $options)
The deferlib::cmd function.
Returns: Any
output of command or options[else]
Examples
ensure cron from file content on agent side
service { 'cron':
ensure => deferlib::cmd({
'command' => 'cat /etc/cron_ensure',
'match' => '^(running|stopped)$',
'else' => 'running',
'user' => 'foo',
'group' => 'bar',
'environment' => { 'myvar' => 'myvalue' },
}),
}
options
Data type: Hash
options of shell execution
Options:
- 'command'
String
: shell command to execute - 'match'
String
: regexp to validate output of command - 'else'
Any
: returned value if command exit code not 0 - 'user'
Variant[String,Integer]
: The user to run the command as - 'group'
Variant[String,Integer]
: The group to run the command as - 'environment'
Hash
: Hash of environment variable names / variable values
deferlib::if_cmd
Type: Puppet Language
returns value if deferred shell command execution exit code 0 else returns options[else]
Examples
if command /bin/ismaintenance ensure cron service stopped else ensure running
service { 'cron':
ensure => deferlib::if_cmd('/bin/ismaintenance', 'stopped', {
'else' => 'running',
'user' => 'foo',
'group' => 'bar',
'environment' => { 'myvar' => 'myvalue' },
}),
}
deferlib::if_cmd(String $cmd, Any $value, Optional[Hash] $options = {})
The deferlib::if_cmd function.
Returns: Any
value or options[else]
Examples
if command /bin/ismaintenance ensure cron service stopped else ensure running
service { 'cron':
ensure => deferlib::if_cmd('/bin/ismaintenance', 'stopped', {
'else' => 'running',
'user' => 'foo',
'group' => 'bar',
'environment' => { 'myvar' => 'myvalue' },
}),
}
cmd
Data type: String
shell code to execute
value
Data type: Any
value to return if command exit code is 0
options
Data type: Optional[Hash]
shell execution options
Options:
- 'else'
Any
: returned value if command exit code not 0, default [] - 'user'
Variant[String,Integer]
: The user to run the command as - 'group'
Variant[String,Integer]
: The group to run the command as - 'environment'
Hash
: Hash of environment variable names / variable values
deferlib::if_file
Type: Puppet Language
returns value if file exists else returns default
Examples
ensure service cron is stopped if file /etc/maintenance exists, else ensure running
service { 'cron':
ensure => deferlib::if_file('/etc/maintenance', 'stopped', 'running')
}
deferlib::if_file(String $file, Any $value, Any $default = undef)
The deferlib::if_file function.
Returns: Any
Examples
ensure service cron is stopped if file /etc/maintenance exists, else ensure running
service { 'cron':
ensure => deferlib::if_file('/etc/maintenance', 'stopped', 'running')
}
file
Data type: String
path to file to check existence
value
Data type: Any
value to return if file exists
default
Data type: Any
value to return if file does not exist (default: [])
deferlib::unless_cmd
Type: Puppet Language
returns value if deferred shell command execution exit code not 0 else returns options[else]
Examples
ensure cron service running unless command /bin/ismaintenance returns 0
service { 'cron':
ensure => deferlib::unless_cmd('/bin/ismaintenance', 'running'),
}
deferlib::unless_cmd(String $cmd, Any $value, Optional[Hash] $options = {})
The deferlib::unless_cmd function.
Returns: Any
value or options[else]
Examples
ensure cron service running unless command /bin/ismaintenance returns 0
service { 'cron':
ensure => deferlib::unless_cmd('/bin/ismaintenance', 'running'),
}
cmd
Data type: String
shell code to execute
value
Data type: Any
value to return if command exit code is not 0
options
Data type: Optional[Hash]
shell execution options
Options:
- 'else'
Any
: returned value if command exit code is 0, default [] - 'user'
Variant[String,Integer]
: The user to run the command as - 'group'
Variant[String,Integer]
: The group to run the command as - 'environment'
Hash
: Hash of environment variable names / variable values
deferlib::unless_file
Type: Puppet Language
returns value if file does not exist else returns default
Examples
ensure service cron is running unless file /etc/maintenance exists
service { 'cron':
ensure => deferlib::unless_file('/etc/maintenance', 'running')
}
deferlib::unless_file(String $file, Any $value, Any $default = undef)
The deferlib::unless_file function.
Returns: Any
Examples
ensure service cron is running unless file /etc/maintenance exists
service { 'cron':
ensure => deferlib::unless_file('/etc/maintenance', 'running')
}
file
Data type: String
path to file to check existence
value
Data type: Any
value to return if file does not exist
default
Data type: Any
value to return if file exists (default: [])