Puppet Function: deferlib::cmd_
- Defined in:
- lib/puppet/functions/deferlib/cmd_.rb
- Function type:
- Ruby 4.x API
Overview
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'},
}]),
}
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( = {}) default = [] default = ['else'] if .key?('else') unless .key?('command') return default end command = ['command'] opts = {} opts[:uid] = ['user'] if .key?('user') opts[:gid] = ['group'] if .key?('group') opts[:environment] = ['environment'] if .key?('environment') result = Puppet::Util::Execution.execute(command, opts) out = result.to_s.chomp if result.exitstatus != 0 return default end if .key?('match') if ['match'].match(out) return out end return default end out end end |