Skip to content

Commit

Permalink
Add a result object so we can easily extend it in the future
Browse files Browse the repository at this point in the history
Add a result object so we can easily extend it in the future
  • Loading branch information
Ladas committed Jul 17, 2018
1 parent 2d8040a commit ea444a2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
21 changes: 6 additions & 15 deletions lib/ansible/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,21 @@ def run_via_cli(env_vars, extra_vars, playbook_path)

result = AwesomeSpawn.run!(ansible_command(base_dir), :env => env_vars, :params => [{:cmdline => "--extra-vars '#{JSON.dump(extra_vars)}'", :playbook => playbook_path}])

parsed_stdout = []

# output is JSON per new line
result.output.each_line do |line|
# TODO(lsmola) we can remove exception handling when this is fixed
# https://github.com/ansible/ansible-runner/issues/89#issuecomment-404236832 , so it fails early if there is
# a non json line
begin
parsed_stdout << JSON.parse(line)
rescue => e
_log.warn("Couldn't parse JSON from: #{e}")
end
end
parsed_stdout
return Ansible::Runner::Response.new(:return_code => return_code(base_dir), :stdout => result.output, :stderr => result.error)
end
end

def return_code(base_dir)
File.read(File.join(base_dir, "artifacts/result/rc"))
end

def mkdir(base_dir)
Dir.mkdir(base_dir) unless Dir.exist?(base_dir)
end

def ansible_command(base_dir)
# TODO add possibility to use custom path, e.g. from virtualenv
"ansible-runner run #{base_dir} --json"
"ansible-runner run #{base_dir} --json -i result"
end
end
end
Expand Down
36 changes: 36 additions & 0 deletions lib/ansible/runner/response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module Ansible
class Runner
class Response
include Vmdb::Logging

attr_reader :return_code, :stdout, :stderr, :parsed_stdout

def initialize(return_code:, stdout:, stderr:)
@return_code = return_code
@stdout = stdout
@parsed_stdout = parse_stdout(stdout)
@stderr = stderr
end

private

def parse_stdout(stdout)
parsed_stdout = []

# output is JSON per new line
stdout.each_line do |line|
# TODO(lsmola) we can remove exception handling when this is fixed
# https://github.com/ansible/ansible-runner/issues/89#issuecomment-404236832 , so it fails early if there is
# a non json line
begin
parsed_stdout << JSON.parse(line)
rescue => e
_log.warn("Couldn't parse JSON from: #{e}")
end
end

parsed_stdout
end
end
end
end

0 comments on commit ea444a2

Please sign in to comment.