In the context of utilizing Jinja and Python, say, an error has surfaced while executing a playbook. Although there's an initial assumption that FortiSOAR might be struggling to parse elements, it's worth considering that this may not be the actual cause. Here's why.
The challenge often arises when attempting to mix Jinja, a templating engine commonly used in web frameworks like Flask, with Python code. Here, is a list of best practices to overcome the problem:
Best Practices:
Optimizing Jinja Usage:
Awareness of Evaluation Order:
Clean Code:
Now, let's apply these best practices to a sample code snippet where Jinja expressions are causing issues.
In this snippet, the focus is on providing inputs using Jinja, and unnecessary expressions have been removed. Additionally, unused variables and Jinja expressions within comments have been eliminated to prevent evaluation issues.
Code snippet with issues:
for ticket in output_list:
open_timestamp = "{{arrow.get(ticket['opened_at'],'YYYY-MM-DD HH:mm:ss').shift(hours=+8).format('MM/DD/YYYY, hh:mm:ss A')}}"
work_notes += f'{ticket["number"]} - {ticket["short_description"]} - Opened:'
Here, is the revised code, based on the best practices:
open_timestamp = "{{arrow.get(ticket['opened_at'],'YYYY-MM-DD HH:mm:ss').shift(hours=+8).format('MM/DD/YYYY, hh:mm:ss A')}}"
#more_notes = '\n'.join(f'{ticket['number']} - {ticket['short_description']}
Nominating a forum post submits a request to create a new Knowledge Article based on the forum post topic. Please ensure your nomination includes a solution within the reply.
We can comment to JINJA code like below.
{# open_timestamp is STRING variable defined in a SET VARIABLE STEP and its value generated by arrow filter. For more information refer https://arrow.readthedocs.io/en/latest/ . #}
open_timestamp = "{{arrow.get(ticket['opened_at'],'YYYY-MM-DD HH:mm:ss').shift(hours=+8).format('MM/DD/YYYY, hh:mm:ss A')}}"
Here, whatever we write inside {# #} , goes as a comment to understand the throughts behind JINJA code.
User | Count |
---|---|
6 | |
4 | |
4 | |
3 | |
2 |
The Fortinet Security Fabric brings together the concepts of convergence and consolidation to provide comprehensive cybersecurity protection for all users, devices, and applications and across all network edges.
Copyright 2024 Fortinet, Inc. All Rights Reserved.