guettli
guettli

Reputation: 27139

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?

Upvotes: 0

Views: 2027

Answers (2)

daks
daks

Reputation: 883

Without using replace Jinja filter, you can use its concatenation possibilities

{{ 'foo-' ~ salt['grains.get']('lsb_distrib_release') ~ '-bar' }}

Upvotes: 0

A189198
A189198

Reputation: 396

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) }}

Upvotes: 1

Related Questions