Calling Linux executable from automate


#1

Hi,
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.


#2

Hello,
This is an example of how to run an external command from ruby.
https://github.com/ramrexx/CloudFormsPOC/blob/801259906111c61f5449cc8d44afc8be42374b1c/Automate/CloudFormsPOC/Integration/VMware/VIX.class/methods/runprograminguest_linux.rb#L80

You can tweak it for your use case


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

#4


(taken from StackOverflow)

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


#5

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 = AwesomeSpawn.run(runner_cmd, :params => [fd.path, rec.name])
app/models/miq_action.rb:        command_result = AwesomeSpawn.run(fname)
app/models/miq_server/update_management.rb:      rescue AwesomeSpawn::CommandResultError
app/models/miq_worker.rb:    "#{AwesomeSpawn::CommandLineBuilder.new.build(cmd, options)} #{name}"
app/models/miq_worker.rb:    AwesomeSpawn.run("renice", :params =>  {:n => nice_increment, :p => pid })
lib/embedded_ansible/appliance_embedded_ansible.rb:    AwesomeSpawn.run!("source /etc/sysconfig/ansible-tower; echo $TOWER_SERVICES").output.split
lib/embedded_ansible/appliance_embedded_ansible.rb:      AwesomeSpawn.run!(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:    AwesomeSpawn.run!(executable, options).output
lib/tasks/evm_dba.rake:      rescue AwesomeSpawn::CommandResultError
lib/tasks/evm_dba.rake:      rescue AwesomeSpawn::CommandResultError
lib/tasks/evm_dba.rake:        AwesomeSpawn.run!("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 = AwesomeSpawn.run(diag[:cmd], :params => diag[:params]).output