Skip to content

Commit

Permalink
Merge pull request #17688 from Ladas/use_ansible_runner
Browse files Browse the repository at this point in the history
Use ansible-runner instead of ansible-playbook
  • Loading branch information
agrare committed Jul 17, 2018
2 parents 2c27c3c + 5e9738f commit 3dc7d6d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
25 changes: 21 additions & 4 deletions lib/ansible/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,30 @@ def run_queue(env_vars, extra_vars, playbook_path, user_id, queue_opts)
private

def run_via_cli(env_vars, extra_vars, playbook_path)
result = AwesomeSpawn.run!(ansible_command, :env => env_vars, :params => [{:extra_vars => JSON.dump(extra_vars)}, playbook_path])
JSON.parse(result.output)
Dir.mktmpdir("ansible-runner") do |base_dir|
Dir.mkdir(File.join(base_dir, 'project')) # without this, there is a silent fail of the ansible-runner command see https://github.com/ansible/ansible-runner/issues/88

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

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")).to_i
rescue
_log.warn("Couldn't find ansible-runner return code")
1
end

def ansible_command
def ansible_command(base_dir)
# TODO add possibility to use custom path, e.g. from virtualenv
"ansible-playbook"
"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 3dc7d6d

Please sign in to comment.