From 80a2352a995fed07e90f4a2e5626b79f2d109337 Mon Sep 17 00:00:00 2001 From: Ladislav Smola Date: Fri, 13 Jul 2018 12:55:07 +0200 Subject: [PATCH] Deal with output which is JSON per line Deal with output which is JSON per line and for now, deal with possible non json lines. --- lib/ansible/runner.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/ansible/runner.rb b/lib/ansible/runner.rb index 20d9e3f09e4..37a44d21111 100644 --- a/lib/ansible/runner.rb +++ b/lib/ansible/runner.rb @@ -12,7 +12,21 @@ def run_via_cli(env_vars, extra_vars, playbook_path) mkdir(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}]) - JSON.parse(result.output) + + 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 end end