Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

run_command: support an array of commands #3336

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions bolt-modules/boltlib/lib/puppet/functions/run_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@
return_type 'ResultSet'
end

# Run multiple commands.
# @param commands Commands to run on target.
# @param targets A pattern identifying zero or more targets. See {get_targets} for accepted patterns.
# @param options A hash of additional options.
# @option options [Boolean] _catch_errors Whether to catch raised errors.
# @option options [String] _run_as User to run as using privilege escalation.
# @option options [Hash[String, Any]] _env_vars Map of environment variables to set
# @return A list of results, one entry per target.
# @example Run commands on targets
# run_command(['hostname', 'whoami'], $targets, '_catch_errors' => true)
dispatch :run_commands do
param 'Array', :commands
param 'Boltlib::TargetSpec', :targets
optional_param 'Hash[String[1], Any]', :options
return_type 'ResultSet'
end

# Run a command, logging the provided description.
# @param command A command to run on target.
# @param targets A pattern identifying zero or more targets. See {get_targets} for accepted patterns.
Expand All @@ -48,6 +65,10 @@ def run_command(command, targets, options = {})
run_command_with_description(command, targets, nil, options)
end

def run_commands(commands, targets, options = {})
run_command_with_description(commands.join(' && '), targets, nil, options)
end

def run_command_with_description(command, targets, description = nil, options = {})
unless Puppet[:tasks]
raise Puppet::ParseErrorWithIssue
Expand Down
Loading