Module: ForemanAnsible::AnsibleReportsHelper

Defined in:
app/helpers/foreman_ansible/ansible_reports_helper.rb

Overview

This module takes the config reports stored in Foreman for Ansible and modifies them to be properly presented in views

Instance Method Summary collapse

Instance Method Details

#ansible_module_message(log) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/helpers/foreman_ansible/ansible_reports_helper.rb', line 28

def ansible_module_message(log)
  msg_json = parsed_message_json(log)
  return _("Execution error: #{msg_json['msg']}") if msg_json['failed'].present?
  return msg_json['censored'] if msg_json['censored'].present?

  module_action = msg_json.fetch('module', '').delete_prefix('ansible.builtin.').delete_prefix('ansible.legacy.')
  case module_action
  when 'package'
    msg_json['results'].empty? ? msg_json['msg'] : msg_json['results']
  when 'template'
    get_results(msg_json) do |module_args, result|
      _("Rendered template #{module_args['_original_basename']} to #{result['dest']}")
    end
  when 'service'
    get_results(msg_json) do |_, result|
      _("Service #{result['name']} #{result['state']} (enabled: #{result['enabled']})")
    end
  when 'group'
    get_results(msg_json) do |_, result|
      _("User group #{result['name']} #{result['state']}, gid: #{result['gid']}")
    end
  when 'user'
    get_results(msg_json) do |_, result|
      _("User #{result['name']} #{result['state']}, uid: #{result['uid']}")
    end
  when 'cron'
    get_results(msg_json) do |module_args, _|
      _("Cron job: #{module_args['minute']} #{module_args['hour']} #{module_args['day']} #{module_args['month']} #{module_args['weekday']} #{module_args['job']} (disabled: #{module_args['disabled']})")
    end
  when 'copy'
    get_results(msg_json) do |module_args, result|
      _("Copy #{module_args['_original_basename']} to #{result['dest']}")
    end
  when 'command', 'shell'
    msg_json['stdout_lines']
  else
    no_data_message
  end
end

#ansible_module_name(log) ⇒ Object



7
8
9
10
11
# File 'app/helpers/foreman_ansible/ansible_reports_helper.rb', line 7

def ansible_module_name(log)
  source_value = log.source&.value
  name = source_value.split(':')[0].strip if source_value&.include?(':')
  name
end

#ansible_report?(log) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
# File 'app/helpers/foreman_ansible/ansible_reports_helper.rb', line 76

def ansible_report?(log)
  module_name(log).present?
rescue StandardError
  false
end

#ansible_report_origin_partialObject



72
73
74
# File 'app/helpers/foreman_ansible/ansible_reports_helper.rb', line 72

def ansible_report_origin_partial
  'foreman_ansible/config_reports/ansible'
end

#ansible_run_in_check_mode?(log) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'app/helpers/foreman_ansible/ansible_reports_helper.rb', line 20

def ansible_run_in_check_mode?(log)
  log.message&.value == 'check_mode_enabled' if check_mode_log?(log)
end

#ansible_task_name(log) ⇒ Object



13
14
15
16
17
18
# File 'app/helpers/foreman_ansible/ansible_reports_helper.rb', line 13

def ansible_task_name(log)
  source_value = log.source&.value
  return source_value || no_data_message unless source_value.include? ':'
  name = source_value.split(':')[1].strip if source_value.include?(':')
  name || no_data_message
end

#check_mode_log?(log) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'app/helpers/foreman_ansible/ansible_reports_helper.rb', line 24

def check_mode_log?(log)
  log.source&.value == 'check_mode'
end

#no_data_messageObject



68
69
70
# File 'app/helpers/foreman_ansible/ansible_reports_helper.rb', line 68

def no_data_message
  _('No additional data')
end

#show_full_error_message_value(message_value) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'app/helpers/foreman_ansible/ansible_reports_helper.rb', line 82

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