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
<%- load_hosts().each_record do |host| -%>
'status': host_status(host, "Configuration"),
'last_report': "I have no idea what to put here",
<%- end -%>
<%= report_render -%>
Also, is there any way to run this report automatically (cron, etc), as opposed to manually through the web UI?
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
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…
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!