Hello. I figured this out. I am not a ruby expert, on the contrary, I’ve never used it before Foreman so perhaps code can look better. But, it works, I haven’t noticed any performance issues as well.
Here it is.
1. Displaying Hostname, instead of ‘Search Query’ (host id) column in the Job invocations page:
From:
To:
So to have a real hostname on which task was executed you need to:
a) Open:
/app/helpers/job_invocations_helper.rb
Add following method:
def display_search_query(query)
if query =~ /id\s*\^\s*\((\d+)\)/
host_id = $1.to_i
host = Host.find_by_id(host_id)
return host ? host.name.split('.').first : query
end
query
end
b) Open following file:
/app/views/job_invocations/index.html.erb
change 24 line from:
<td><%= trunc_with_tooltip(invocation.targeting.search_query, 15) %></td>
to:
<td><%= trunc_with_tooltip(display_search_query(invocation.targeting.search_query)) %></td>
c) Restart foreman
systemctl restart foreman
2. Displaying an argument in the job description page, like Host Resource.
When your job template uses a Host Resource as an argument, and you want to display this argument in the desription:
like this:
a) Open:
/app/models/job_invocation.rb
b) Modify generate_description method as following:
def generate_description
template_invocation = pattern_template_invocations.first
input_hash = template_invocation.input_values.reduce({}) do |h, v|
value = v.value
value = '*' * 3 if v.template_input.respond_to?(:hidden_value) && v.template_input.hidden_value?
h.update("%{#{v.template_input.name}}" => value)
end
input_hash.update("%{job_category}" => job_category)
input_hash.update("%{template_name}" => template_invocation.template.name)
# Get the list of targeted hostnames, not used
# server_names = targeting.hosts.map(&:name).join(', ')
# input_hash.update("%{server_name}" => server_names)
# Fetch the user-provided Host resource input
user_host_input = template_invocation.input_values.find do |v|
v.template_input.value_type == 'resource' && v.template_input.resource_type == 'Host'
end
# Get the hostname from the user input
if user_host_input
selected_host = Host.find_by_id(user_host_input.value) # Assuming value contains the host ID
input_hash.update("%{user_selected_host}" => selected_host ? selected_host.name : 'Unknown')
else
input_hash.update("%{user_selected_host}" => 'None')
end
input_hash.default = "''"
self.description = description_format.gsub(/%{[^}]+}/) { |key| input_hash[key] }
self.description = self.description[0..(JobInvocation.columns_hash['description'].limit - 1)]
end
c) Then in the job description format add the following:
d) Restart Foreman
systemctl restart foreman