Puppet Function: choria::on_success

Defined in:
lib/puppet/functions/choria/on_success.rb
Function type:
Ruby 4.x API

Overview

choria::on_success(Choria::TaskResults $results, Callable &$block)Choria::TaskResults

Handles successes contained in Choria::TaskResults

This is a helper to create success handlers for plans and tasks that returns Choria::TaskResults, it’s primarily aimed to be used in long playbooks with many tasks being run where creating many uniquely named result variables is tedious.

The lamba will only be called when the result is a Choria::TaskResults instance and if there were no exceptions and when error_set is empty. This means setting fail_ok on a task will not still trigger this handler if the task failed.

Non Choria::TaskResults will simply result in this lambda never being called

This function returns the result so it can be chained with other ‘on_error` or `on_success` calls and will return the results from the plan if it’s the last statement in the plan.

Examples:

invoke a task and handle results without temporary variables


choria::task("mcollective", _catch_errors => true,
   "action" => "rpcutil.ping",
   "nodes" => $nodes
)
 .on_error |$err| {
   choria::run_playbook("example::rollback")
 }

 .on_success |$succ| {
   choria::run_playbook("example::notifier", "progress" => "Step ping completed succesfully")
 }

invoke a task and handle results with temporary variables


$result choria::task("mcollective",
   "action" => "rpcutil.ping",
   "nodes" => $nodes
)

$result.on_success |$succ| {
   choria::run_playbook("example::notifier", "progress" => "Step ping completed succesfully")
}

Parameters:

  • results (Choria::TaskResults)
  • &block (Callable)

Returns:

  • (Choria::TaskResults)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/puppet/functions/choria/on_success.rb', line 43

Puppet::Functions.create_function(:"choria::on_success", Puppet::Functions::InternalFunction) do
  dispatch :handler do
    param "Choria::TaskResults", :results
    block_param
    return_type "Choria::TaskResults"
  end

  def handler(results)
    if results.is_a?(MCollective::Util::BoltSupport::TaskResults) && (results.error_set.empty && !results.exception)
      yield(results)
    end

    results
  end
end