From 860b50cbbf049a05e4641ae7eee310204010fdf7 Mon Sep 17 00:00:00 2001 From: nofaralfasi Date: Tue, 29 Nov 2022 17:33:47 +0200 Subject: [PATCH] Fixes #35799 - fix ansible report returns 500 error code when job execution failed Handle execution failure for ansible reports, and display the error message in the UI --- .../foreman_ansible/ansible_reports_helper.rb | 13 +++++++++++++ .../config_reports/_ansible.html.erb | 8 ++++++-- test/unit/helpers/ansible_reports_helper_test.rb | 14 ++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/app/helpers/foreman_ansible/ansible_reports_helper.rb b/app/helpers/foreman_ansible/ansible_reports_helper.rb index 88cbdb601..b8a2722b0 100644 --- a/app/helpers/foreman_ansible/ansible_reports_helper.rb +++ b/app/helpers/foreman_ansible/ansible_reports_helper.rb @@ -27,6 +27,8 @@ def check_mode_log?(log) def ansible_module_message(log) msg_json = parsed_message_json(log) + return _("Execution error: #{msg_json['msg']}") if msg_json['failed'].present? + module_action = msg_json['module'] case module_action when 'package' @@ -67,6 +69,17 @@ def ansible_report?(log) false end + def show_full_error_message_value(message_value) + tag.div class: 'replace-hidden-value' do + link_to_function(icon_text('plus', '', class: 'small'), 'replace_value_control(this, "div")', + title: _('Show full value'), + class: 'replace-hidden-value pull-right') + + (tag.span class: 'full-value' do + message_value + end) + end + end + private def parsed_message_json(log) diff --git a/app/views/foreman_ansible/config_reports/_ansible.html.erb b/app/views/foreman_ansible/config_reports/_ansible.html.erb index c3c14c807..bc2b4706c 100644 --- a/app/views/foreman_ansible/config_reports/_ansible.html.erb +++ b/app/views/foreman_ansible/config_reports/_ansible.html.erb @@ -29,8 +29,12 @@ <% end %> <% else %> - <%= log_message %> - <% end %> + <% allowed_length = 150 %> +
+ <%= truncate(log_message, length: allowed_length) %> +
+ <%= show_full_error_message_value(log_message) if log_message.length > allowed_length %> + <% end %> <% end %> diff --git a/test/unit/helpers/ansible_reports_helper_test.rb b/test/unit/helpers/ansible_reports_helper_test.rb index 4b019afc6..28417904e 100644 --- a/test/unit/helpers/ansible_reports_helper_test.rb +++ b/test/unit/helpers/ansible_reports_helper_test.rb @@ -19,4 +19,18 @@ class AnsibleReportsHelperTest < ActiveSupport::TestCase ansible_module_message(log).to_s ) end + + test 'module message extraction with error' do + log_value = <<-ANSIBLELOG.strip_heredoc + {"msg": "AnsibleUndefinedVariable", "changed": false, "_ansible_no_log": false, "failed": true, "module": "template", "exception": "raise AnsibleUndefinedVariable"} +ANSIBLELOG + message = FactoryBot.build(:message, value: log_value) + log = FactoryBot.build(:log, message: message) + log.message = message + + assert_match( + 'Execution error: AnsibleUndefinedVariable', + ansible_module_message(log).to_s + ) + end end