Calling Linux executable from automate


Is it possible to call a linux command from an automate task and capture its output?

What I want to do is to get the Automate->Ansible->Credentials, Machine credentials, retrieve the Private Key and use:

ssh-keygen -f private-key -y > public-key

To create a public key. This key then can be added to the cloud-init templates for VM provisioning.


This is an example of how to run an external command from ruby.

You can tweak it for your use case

# Description: Sample method to run native OS commands on the appliance
require 'awesome_spawn'
result =!('/bin/ls', :params => ['-a'])
if result.exit_status == 0
  $evm.root['method_output'] = result.output
  $evm.root['method_output'] = result.error
$evm.log(:info, "Result #{result.output}")
$evm.log(:info, "Exit Status #{result.exit_status}")
$evm.log(:info,"Error #{result.error}")


(taken from StackOverflow)

Best match seems to be %x{} or IO.popen() for you.


It’s so common for us to want to capture output/error/exit code and possibly raise on non-zero exit code that we wrote awesome spawn:

It’s already on the appliances and should be accessible from automate, @mkanoor correct me if I’m wrong.

Some examples of how to invoke it for various cases (and how to capture any possible exceptions) can be found in manageiq:

app/models/miq_action.rb:        command_result =, :params => [fd.path,])
app/models/miq_action.rb:        command_result =
app/models/miq_server/update_management.rb:      rescue AwesomeSpawn::CommandResultError
app/models/miq_worker.rb:    "#{, options)} #{name}"
app/models/miq_worker.rb:"renice", :params =>  {:n => nice_increment, :p => pid })
lib/embedded_ansible/appliance_embedded_ansible.rb:!("source /etc/sysconfig/ansible-tower; echo $TOWER_SERVICES").output.split
lib/embedded_ansible/appliance_embedded_ansible.rb:!(SETUP_SCRIPT, :params => params)
lib/embedded_ansible/appliance_embedded_ansible.rb:  rescue AwesomeSpawn::CommandResultError => e
lib/miq_cockpit.rb:      AwesomeSpawn.build_command_line(COCKPIT_WS_PATH, args)
lib/pdf_generator/prince_pdf_generator.rb:      command = AwesomeSpawn.build_command_line(executable, options[:params])
lib/pdf_generator/prince_pdf_generator.rb:!(executable, options).output
lib/tasks/evm_dba.rake:      rescue AwesomeSpawn::CommandResultError
lib/tasks/evm_dba.rake:      rescue AwesomeSpawn::CommandResultError
lib/tasks/evm_dba.rake:!("bin/rails runner", :params => ["MiqDatabase.seed; MiqRegion.seed"])
lib/tasks/evm_dba.rake:        message = err.kind_of?(AwesomeSpawn::CommandResultError) ? err.result.error : err.message
lib/vmdb/appliance.rb:            res =[:cmd], :params => diag[:params]).output