elastalert2 icon indicating copy to clipboard operation
elastalert2 copied to clipboard

Absolute Jinja2 template path doesn't work

Open gethvi opened this issue 2 years ago • 2 comments

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.

gethvi avatar Jan 23 '24 15:01 gethvi

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:

  • jinja_template_base_path: "" => Templates will be provided via relative paths under the current working directory (existing default behavior)
  • jinja_template_base_path: "/" => Templates will be provided via absolute paths
  • jinja_template_base_path: "/some/other/path" => Templates will be provided via relative paths under the given parent directory

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

Thanks for raising the issue.

jertel avatar Jan 24 '24 13:01 jertel

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.

github-actions[bot] avatar Jul 22 '24 20:07 github-actions[bot]