jertel / elastalert2

ElastAlert 2 is a continuation of the original yelp/elastalert project. Pull requests are appreciated!
https://elastalert2.readthedocs.org
Apache License 2.0
914 stars 287 forks source link

Absolute Jinja2 template path doesn't work #1360

Open gethvi opened 8 months ago

gethvi commented 8 months ago

Hi,

using elastalert2==2.15.0 we encountered an error with jinja2 absolute template path not working:

jinja_template_path: /opt/elastalert/config/jinja_template.txt

It raises the following exception:

jinja2.exceptions.TemplateNotFound: /opt/elastalert/config/jinja_template.txt

Elastalert is installed like this:

python3 -m venv /opt/elastalert/venv
source /opt/elastalert/venv/bin/activate
pip install elastalert2

And used with custom systemd unit file:

[Unit]
Description=ElastAlert2
After=elasticsearch.service

[Service]
User=gitlab-runner
Group=gitlab-runner
WorkingDirectory=/opt/elastalert/
ExecStart=/opt/elastalert/venv/bin/elastalert --config /opt/elastalert/config/config.yml --verbose
Restart=on-failure

[Install]
WantedBy=multi-user.target

After some digging through the source code of elastalert2 and experimentation, it works with the following rule settings:

jinja_template_path: config/jinja_template.txt

Using the relative path (considering the working directory set by the systemd unit file) it works. However for a regular user it is not very intuitive to set the jinja_template_path to a relative path and the documentation doesn't mention it.

jertel commented 8 months ago

Yes, it requires relative paths. I've updated the documentation to reflect this. We cannot change it to use absolute paths since it will break existing users configurations. However, if you're interested in adding an additional field named jinja_template_base_path then that value could allow users to change the behavior:

The value would be supplied on line 149 of loaders.py.

Thanks for raising the issue.

github-actions[bot] commented 2 months ago

This issue is stale because it has been open for 6 months with no activity. Stale issues convey that the issue, while important to someone, is not critical enough for the author, or other community members to work on, sponsor, or otherwise shepherd the issue through to a resolution.