Getting last_report value in a "Report Template"

I’m trying to make a Report Template that includes the host’s “last_report” time, and nothing I’ve come up with has worked. Could anyone nudge me in the right direction?

This is the entirety of the template:

<%#
name: OS Updates Report
snippet: false
model: ReportTemplate
-%>
<%- load_hosts().each_record do |host| -%>
<%-   report_row({
        'name': host.name,
        'status': host_status(host, "Configuration"),
        'last_report': "I have no idea what to put here",
        'os::name': host.facts["os::name"],
        'uptime_days': host.facts["system_uptime::days"],
        'pending_updates::total': host.facts["pending_updates::total"],
        'pending_updates::security': host.facts["pending_updates::security"],
        'pending_updates::reboot_required': host.facts["pending_updates::reboot_required"],
      }) -%>
<%- end -%>
<%= report_render -%>

Also, is there any way to run this report automatically (cron, etc), as opposed to manually through the web UI?

Hello,

last_report attribute is not allowed in safe mode. I’ve sent a PR to fix this. You can still use host.last_report and disable safe mode rendering in Settings -> Provision. Note that this would allow users to execute arbitrary ruby code in your templates, so only do it, if your users are trusted.

To generate the report, you can use rest API or hammer (CLI). Then just put curl/hammer command to cron. Note that you query facts table, which can be quite expensive if you have a lot of records. You could replace some of it, e.g. uptime has been recently added (in seconds) and on nightly can be fetched using host_uptime_seconds(host). The OS can be probably loaded from DB using things like

host.operatingsystem.family
host.operatingsystem.name 
host.operatingsystem.major.to_i
host.operatingsystem.minor.to_i

Ares pending_updates facts reported by subscription manager? Perhaps we could add also explicit loaders for them.

Also once your template is working, please consider sharing at https://github.com/theforeman/community-templates/tree/develop/report_templates if you think it’s valuable for more users.

Hope this helps

Since I’m the only user, I think turning safe mode rendering off is acceptable. This works to get host.last_report now.

I changed to using host.operatingsystem.name, which looks like exactly the same data I was getting from facts[os::name]. Good tip.

pending_updates is a custom fact I’ve made for our environment. With the number of hosts I have (~50), the report only takes 2-3 seconds, which is fine.

And I found the ‘hammer report-template’ subcommand. Not sure how I missed that before…

Thanks!

Great, I guess if the report relies on custom facts, it doesn’t make much sense to share. I’m glad it works for you!