Skip to main content

Broadcast Targeting

Send operations to multiple agents and access per-host results.

Targets

TargetBehavior
_anyAny single agent (load balanced)
_allEvery registered agent
hostnameSpecific host
key:valueAgents matching a label

_all and label selectors (key:value) are broadcast targets — the job runs on every matching agent and per-host results are available via HostResults.

Usage

getAll := plan.TaskFunc("get-hostname-all",
func(
ctx context.Context,
c *client.Client,
) (*orchestrator.Result, error) {
resp, err := c.Node.Hostname(ctx, "_all")
if err != nil {
return nil, err
}

return orchestrator.CollectionResult(
resp.Data,
func(r client.HostnameResult) orchestrator.HostResult {
return orchestrator.HostResult{
Hostname: r.Hostname,
Changed: r.Changed,
Error: r.Error,
}
},
), nil
},
)

// Access per-host results via TaskFuncWithResults.
printHosts := plan.TaskFuncWithResults("print-hosts",
func(
_ context.Context,
_ *client.Client,
results orchestrator.Results,
) (*orchestrator.Result, error) {
r := results.Get("get-hostname-all")
for _, hr := range r.HostResults {
fmt.Printf(" %s changed=%v\n",
hr.Hostname, hr.Changed)
}
return &orchestrator.Result{Changed: false}, nil
},
)
printHosts.DependsOn(getAll)

Example

See examples/sdk/orchestrator/features/broadcast.go for a complete working example.