Render_template syntax

I have the below lines of code in a job template.

CURRENT_DATE=$(date +%F)

<%= render_template('CDA - Content View Update - Katello SSH Default', :org => 'CDA', :view => 'Test-Content-View', :filter => 'Test-Exclude-Erratum-After-Date-Filter', :rule_id => '2', :update_start_date => 'Y', :update_end_date => 'N', :start_date => '$CURRENT_DATE', :promote_to => 'ALL') %>

I am trying to pass the current date to the second command. If I run the above job, it runs successfully. However, it does not substitute the value for $CURRENT_DATE. So, effectively it runs the command as:

hammer content-view filter … --start-date ‘$CURRENT_DATE’

instead of

hammer content-view filter … --start-date ‘2021-10-28’

– If I remove the single quotes around $CURRENT_DATE, it gives me an error:

Problem with previewing the template: error during rendering: Safemode doesn’t allow to access ‘global variable’ on $CURRENT_DATE.

– If I disable the Safemode for testing, the preview works fine. But the command runs as:

hammer content-view filter … --start-date ’ ’

i.e it does not get the value from the variable so puts null in there.

Does anyone know what the correct syntax for this should be?

Job templates are evaluated as Ruby ERB.

Without safe mode, I think

<%- current_date = Date.today.strftime -%>

would do the trick, followed by

hammer content-view filter … --start-date <%= current_date %>

But with safe mode you may not have access to the today method on the Date class… @Marek_Hulan any ideas?

For more info on what is available, go to the Help tab when editing a job template.

Thanks for the response. Actually, if I was running a hammer command directly, I could use the

CURRENT_DATE=$(date +%F)
hammer content-view filter … $CURRENT_DATE

syntax just fine (I have it in another template and it works fine).

The issue in this particular case is that I am using render_template, which needs to be within the <% %> tags, which seem to be causing the issue.

This can’t work because the ERB is evaluated on the server when the job is triggered, Everything in <% … %> gets replaced by its value. Then the result is delivered to the target machine and executed. So only at that point, the bash part gets executed. I think that @jeremylenz has a good suggestion, you must modify the template in a way the current date is calculated during the ERB rendering phase and then use that value.

The issue is, safe mode does not allow any method that gives a current time or date. That would be trivial to add in this file, see the format_time for an example.

@Marek_Hulan Thanks for the feedback. I’m not a programmer or developer, so it took me a while to understand what Jeremy is suggesting. But finally I was able to use the Date.today with safemode OFF to get the desired result.

Now to get the same result with safemode ON, I’m trying to understand what you are suggesting. Correct me if I’m wrong but you are saying that I could add a method to the helpers.rb file and that would allow me to use that method with safemode ON? If yes, would you happen to have the code for such a method (or similar) available? Or, any idea where I could find the code for the Date.today method - maybe I could add it to the helpers.rb file?
I tried to review the format_time section of the file and wasn’t able to understand what’s going on in there.

1 Like

If Jeremy’s code to get the current date works for you, I’ve created a PR [1] which adds this macro to use with safe mode enabled. You can also pass an optional argument to format the date for your needs.

[1] - https://github.com/theforeman/foreman/pull/8919

2 Likes

@ofedoren Thank you for the code. It worked perfectly.

3 Likes