Hello,
I’m having problems using a Jinja2 function in an Ansible conditional and was hoping someone could please help me find a way around it.
As far as I can tell I must use both parentheses and single quotes in the Jinja2 function I’m using but these mean that I cannot avoid Ansible reporting template errors.
- I want a role to run at all times EXCEPT between 01:45 to 02:20 UTC.
- I cannot ensure that the OS uses UTC timezone, so found the Jinja2
now()
function is the best way to determine this.
I’ve tried a few different varients of the conditionals below but this is the easiest format to read:
- name: workstation role
hosts: localhost
connection: local
become: true
tasks:
- name: WORKSTATION_ROLE
include_role:
name: workstation
# Skip role execution during daily patch window to avoid conflicts with apt lock. Do not run between 01:45 and 02:20
when:
- not ( now('utc').hour == 01 and now('utc').minute >= 45 )
- not ( now('utc').hour == 02 and now('utc').minute < 20 )
Ansible returns the error:
The error was: template error while templating string: expected token ')', got 'integer'. String: {% if not ( now('utc').hour == 01 and now('utc').minute >= 45 ) %} True {% else %} False {% endif %}
but I need to use those parentheses for the now() function and also cannot use single quotes to escape them as they are required for the ‘utc’ argument.
EDIT: I’ve also tried negating the whole conditional, but then need to use parentheses because I need to use a logical OR instead of a logical AND.
Any tips for what I should try please?
Thank you in advance!
PS: I could not find any complex examples of the use of parentheses in official Ansible documentation and nothing like the problem I have with needing to use them inside the conditional “template”.