Search code examples
jinja2salt-project

Replace and eval in Jinja


I have a string like this as Jinja variable:

foo-VERSION-bar

I want to replace VERSION with {{ grains.lsb_distrib_release }} and I want this to get evaluated.

if grains.lsb_distrib_release contains 123 I want the result to be foo-123-bar.

How to replace and eval in jinja?


Solution

  • Set value of your grain to a variable:

    {% set version = salt['grains.get']('lsb_distrib_release', {}) %}
    

    Use Jinja replace function:

    {{ "foo-VERSION-bar"|replace("VERSION", version) }}