Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code blocks in Jinja templates in salt

I have a block that looks like this:

{% if grains['function'] == 'production' %}
{% set conf_src = "prod.yml.ninja" %}
{% elif grains['function'] == 'staging'] %}
{% set conf_src = "staging.yml.ninja" %}
{% elif grains['function'] == 'dev'] %}
{% set conf_src = "dev.yml.ninja" %}
{% endif %}

Is there any way to do something like

{% 
    if grains['function'] == 'production'
        set conf_src = "prod.yml.ninja"
    elif grains['function'] == 'staging'
        set conf_src = "staging.yml.ninja"
    elif grains['function'] == 'dev'
        set conf_src = "dev.yml.ninja"
    endif
%}

So I can just open the block once?

like image 434
jcity Avatar asked Dec 13 '25 09:12

jcity


1 Answers

You can define a look-up dictionary, and only include non-trivial cases:

html = '''
{% set lookup = dict(production='prod') %}
{% set conf_src = lookup.get(grains['function'], grains['function']) 
                + '.yml.ninja' %}
'''

Here, since dev and staging are not modified, you may use dict.get fall-back argument.

like image 163
behzad.nouri Avatar answered Dec 15 '25 21:12

behzad.nouri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!