Skip to content

Commit

Permalink
Fixes #35799 - fix ansible report returns 500 error code when job exe…
Browse files Browse the repository at this point in the history
…cution failed

Handle execution failure for ansible reports, and display the error message in the UI
  • Loading branch information
nofaralfasi authored and Ron-Lavi committed Jan 8, 2023
1 parent 356b1c4 commit 860b50c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
13 changes: 13 additions & 0 deletions app/helpers/foreman_ansible/ansible_reports_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions app/views/foreman_ansible/config_reports/_ansible.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@
<% end %>
</ul>
<% else %>
<%= log_message %>
<% end %>
<% allowed_length = 150 %>
<div class='pull-left'>
<%= truncate(log_message, length: allowed_length) %>
</div>
<%= show_full_error_message_value(log_message) if log_message.length > allowed_length %>
<% end %>
</td>
</tr>
<% end %>
Expand Down
14 changes: 14 additions & 0 deletions test/unit/helpers/ansible_reports_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 860b50c

Please sign in to comment.