4c74356b41
4c74356b41

Reputation: 72141

Jinja: variable inside string inside if statement

I'm trying to create the following loop in jinja:

variable: >
[
    {% for replaceme in list %}
    {
        'name': "{{ "string-{{replaceme}}" if replaceme == 'somevalue' else "string-something-{{replaceme}}" }}",
        'sshKey': "{{ lookup(...) }}"
    }
    {% if not loop.last %},{% endif %}
    {% endfor %}
]

but this doesn't work, any ideas? i tried different quotes, different combinations of curlies, etc.

Upvotes: 9

Views: 20694

Answers (2)

Kinjal Dixit
Kinjal Dixit

Reputation: 7945

you can use string formatting operator.

[
    {% for item in list %}
        {'name': {{ "s1-%s" % item }} },
    {% endfor %}
]

Upvotes: 1

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68339

Nested {{..}} are not allowed.

Try: {{ "string-"+replaceme if replaceme == 'somevalue' else "string-something"+replaceme }}

Upvotes: 10

Related Questions