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

Use ansible-runner instead of ansible-playbook #17688

Merged
merged 7 commits into from
Jul 17, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is stderr not going to be json encoded?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we might do that later, right now it always returns "", so I am not sure what the format will be. Might be the same as the stdout

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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