Puppet Function: deferlib::cmd_

Defined in:
lib/puppet/functions/deferlib/cmd_.rb
Function type:
Ruby 4.x API

Overview

deferlib::cmd_(Hash $options)Any

returns <command> output if <command> exit code <> 0 returns <else> <else> default is [] to be called as Deferred type to be run on client to be used on resource parameter value

Example: service { ‘cron’:

ensure => Deferred('deferlib::cmd_', [{
      'command'     => 'cat /etc/cron_local_ensure',
      'match'       => '^(running|stopped)$',
      'else'        => 'running',
      'user'        => 'foo',
      'environment' => {'PATH' => '/bin:/usr/bin'},
}]),

}

Parameters:

  • options (Hash)

Returns:

  • (Any)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/puppet/functions/deferlib/cmd_.rb', line 19

Puppet::Functions.create_function(:'deferlib::cmd_') do
  dispatch :cmd do
    param 'Hash', :options
  end

  def cmd(options = {})
    default = []
    default = options['else'] if options.key?('else')
    unless options.key?('command')
      return default
    end
    command = options['command']
    opts = {}
    opts[:uid] = options['user'] if options.key?('user')
    opts[:gid] = options['group'] if options.key?('group')
    opts[:environment] = options['environment'] if options.key?('environment')
    result = Puppet::Util::Execution.execute(command, opts)
    out = result.to_s.chomp
    if result.exitstatus != 0
      return default
    end
    if options.key?('match')
      if options['match'].match(out)
        return out
      end
      return default
    end
    out
  end
end