Skip to content

Commit d4e15b7

Browse files
rpluem-vfekohl
andcommitted
fixes #38523 - Add the parameter ansible_roles_diff_mode to enable Ansible diff mode
Add the parameter ansible_roles_diff_mode to enable Ansible diff mode. Furthermore add the needed functionality to the reports helper to show these diffs in reports. * app/helpers/foreman_ansible/ansible_reports_helper.rb: Check if the result hash of a task contains a 'report_diff' key and make its value, the unified diff, available via a link. * app/models/foreman_ansible/ansible_provider.rb: Set the diff_mode smart proxy parameter based on the parameter ansible_roles_diff_mode. * db/seeds.d/100_common_parameters.rb: Create the global parameter ansible_roles_diff_mode with a default of false. * test/fixtures/report.json: Add fixture data for new test case * test/unit/helpers/ansible_reports_helper_test.rb: - Adjust expected strings existing test cases - Add new test case for diff_mode - Include further modules used by ansible_reports_helper.rb Signed-off-by: Ruediger Pluem <[email protected]> Improve hash lookup in app/helpers/foreman_ansible/ansible_reports_helper.rb Co-authored-by: Ewoud Kohl van Wijngaarden <[email protected]> Fix rubocop errors in app/helpers/foreman_ansible/ansible_reports_helper.rb Co-authored-by: Ewoud Kohl van Wijngaarden <[email protected]>
1 parent 3b15276 commit d4e15b7

File tree

5 files changed

+49
-10
lines changed

5 files changed

+49
-10
lines changed

app/helpers/foreman_ansible/ansible_reports_helper.rb

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def ansible_module_message(log)
3636
msg_json['results'].empty? ? msg_json['msg'] : msg_json['results']
3737
when 'template'
3838
get_results(msg_json) do |module_args, result|
39-
_("Rendered template #{module_args['_original_basename']} to #{result['dest']}")
39+
add_diff(_("Rendered template #{module_args['_original_basename']} to #{result['dest']}. "), result['dest'], result)
4040
end
4141
when 'service'
4242
get_results(msg_json) do |_, result|
@@ -56,12 +56,19 @@ def ansible_module_message(log)
5656
end
5757
when 'copy'
5858
get_results(msg_json) do |module_args, result|
59-
_("Copy #{module_args['_original_basename']} to #{result['dest']}")
59+
add_diff(_("Copy #{module_args['_original_basename']} to #{result['dest']}. "), result['dest'], result)
6060
end
6161
when 'command', 'shell'
6262
msg_json['stdout_lines']
6363
else
64-
no_data_message
64+
have_diff = false
65+
get_results(msg_json) do |module_args, result|
66+
if result['report_diff'].present?
67+
have_diff = true
68+
add_diff('', module_args['path'] || '', result)
69+
end
70+
end
71+
no_data_message unless have_diff
6572
end
6673
end
6774

@@ -95,8 +102,10 @@ def show_full_error_message_value(message_value)
95102
def get_results(msg_json)
96103
results = msg_json.key?('results') ? msg_json['results'] : [msg_json]
97104
results.map do |result|
98-
module_args = result.fetch('invocation', {}).fetch('module_args', {})
99-
yield module_args, result
105+
if result.is_a?(Hash)
106+
module_args = result.dig('invocation', 'module_args') || {}
107+
yield module_args, result
108+
end
100109
end
101110
end
102111

@@ -106,5 +115,19 @@ def parsed_message_json(log)
106115
Foreman::Logging.exception('Error while parsing ansible message json', e)
107116
{}
108117
end
118+
119+
def add_diff(message, title, result)
120+
diff = if result['report_diff'].blank?
121+
_('No Diff')
122+
else
123+
# Add '\n' before the unified diff as the Javascript displaying the
124+
# diff expects this.
125+
link_to(_('Show Diff'), '#', data: { diff: "\n#{result['report_diff']}", title: title }, onclick: 'tfm.configReportsModalDiff.showDiff(this);')
126+
end
127+
# diff is already HTML safe and should not be escaped again. Hence escape
128+
# the contents of message and concatenate it with diff and
129+
# declare it HTML safe.
130+
(h(message) + diff).html_safe # rubocop:disable Rails/OutputSafety
131+
end
109132
end
110133
end

app/models/foreman_ansible/ansible_provider.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def proxy_command_options(template_invocation, host)
3434
:name => host.name,
3535
:check_mode => host.host_param('ansible_roles_check_mode'),
3636
:job_check_mode => template_invocation.template.ansible_check_mode,
37+
:diff_mode => host.host_param('ansible_roles_diff_mode'),
3738
:cleanup_working_dirs => cleanup_working_dirs?(host)
3839
)
3940
end

db/seeds.d/100_common_parameters.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
CommonParameter.without_auditing do
22
params = [
3-
{ name: 'ansible_roles_check_mode', key_type: 'boolean', value: false }
3+
{ name: 'ansible_roles_check_mode', key_type: 'boolean', value: false },
4+
{ name: 'ansible_roles_diff_mode', key_type: 'boolean', value: false }
45
]
56

67
params.each { |param| CommonParameter.find_or_create_by(param) }

test/fixtures/report.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@
105105
"level": "notice"
106106
}
107107
},
108+
{
109+
"log": {
110+
"sources": {
111+
"source": "Copy one local file with diff"
112+
},
113+
"messages": {
114+
"message": "{\"_ansible_no_log\": false, \"changed\": true, \"checksum\": \"ec4cddb45c3ce640bed61b3d8ab6c18e715dac78\", \"dest\": \"/tmp/test7.txt\", \"report_diff\": \"--- /tmp/test7.txt\\n+++ /tmp/test7.txt\\n@@ -0,0 +1 @@\\n+Hello\\n\", \"failed\": false, \"gid\": 0, \"group\": \"root\", \"invocation\": {\"module_args\": {\"_original_basename\": \"test7.txt\", \"attributes\": null, \"backup\": false, \"checksum\": \"ec4cddb45c3ce640bed61b3d8ab6c18e715dac78\", \"content\": null, \"delimiter\": null, \"dest\": \"/tmp/test7.txt\", \"directory_mode\": null, \"follow\": false, \"force\": true, \"group\": \"root\", \"local_follow\": null, \"mode\": 256, \"owner\": \"root\", \"regexp\": null, \"remote_src\": null, \"selevel\": null, \"serole\": null, \"setype\": null, \"seuser\": null, \"src\": \"/root/.ansible/tmp/ansible-tmp-1671670677.05-7408-75605497546833/source\", \"unsafe_writes\": false, \"validate\": null}}, \"md5sum\": null, \"mode\": \"0400\", \"module\": \"copy\", \"owner\": \"root\", \"secontext\": \"unconfined_u:object_r:admin_home_t:s0\", \"size\": 6, \"src\": \"/root/.ansible/tmp/ansible-tmp-1671670677.05-7408-75605497546833/source\", \"state\": \"file\", \"uid\": 0}"
115+
},
116+
"level": "notice"
117+
}
118+
},
108119
{
109120
"log": {
110121
"sources": {

test/unit/helpers/ansible_reports_helper_test.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
class AnsibleReportsHelperTest < ActiveSupport::TestCase
66
include ForemanAnsible::AnsibleReportsHelper
77
include ActionView::Helpers::TagHelper
8+
include ActionView::Helpers::UrlHelper
9+
include ERB::Util
810

911
test 'module message extraction' do
1012
log_value = <<-ANSIBLELOG.strip_heredoc
@@ -80,10 +82,11 @@ class AnsibleReportsHelperTest < ActiveSupport::TestCase
8082
'No additional data',
8183
['Cron job: 0 5,2 * * * date > /dev/null (disabled: false)', 'Cron job: 0 5,2 * * * df > /dev/null (disabled: false)'],
8284
['Cron job: 0 5,2 * * * hostname > /dev/null (disabled: false)'],
83-
['Rendered template test1.txt.j2 to /tmp/test1.txt', 'Rendered template test2.txt.j2 to /tmp/test2.txt'],
84-
['Rendered template test3.txt.j2 to /tmp/test3.txt'],
85-
['Copy test4.txt to /tmp/test4.txt', 'Copy test5.txt to /tmp/test5.txt'],
86-
['Copy test6.txt to /tmp/test6.txt'],
85+
['Rendered template test1.txt.j2 to /tmp/test1.txt. No Diff', 'Rendered template test2.txt.j2 to /tmp/test2.txt. No Diff'],
86+
['Rendered template test3.txt.j2 to /tmp/test3.txt. No Diff'],
87+
['Copy test4.txt to /tmp/test4.txt. No Diff', 'Copy test5.txt to /tmp/test5.txt. No Diff'],
88+
['Copy test6.txt to /tmp/test6.txt. No Diff'],
89+
["Copy test7.txt to /tmp/test7.txt. <a data-diff=\"\n--- /tmp/test7.txt\n+++ /tmp/test7.txt\n@@ -0,0 +1 @@\n+Hello\n\" data-title=\"/tmp/test7.txt\" onclick=\"tfm.configReportsModalDiff.showDiff(this);\" href=\"#\">Show Diff</a>"],
8790
['Service chronyd started (enabled: )', 'Service firewalld started (enabled: )'],
8891
['Service chronyd started (enabled: )']
8992
]

0 commit comments

Comments
 (0)