Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use logic operators in jinja template on salt-stack (AND, OR)

Tags:

I am using a jinja template to generate a state file for salt. I added some conditionals and would like to express: if A or B. However, it seems I cannot get any logical operator working.

It doesn't like ||, |, &&(which I understand doesn't apply here), but also not and, or and not even grouping with (), which should be working according to the jinja documentation. I couldn't find any information on this in the salt docs, but I feel I must be making some stupid mistake?

My code:

{% if grains['configvar'] == 'value' OR grains['configvar'] == 'some other value' %} 

Error:

Data failed to compile: Rendering SLS 'base:mystate' failed: Jinja syntax error: expected token 'end of statement block', got 'OR'; line 3 
like image 384
hotfire42 Avatar asked Jan 26 '17 12:01

hotfire42


People also ask

Which data type we use for send values in Jinja?

Jinja can generate any text-based format (HTML, XML, CSV, LaTeX, etc.). A Jinja template doesn't need to have a specific extension: . html , . xml , or any other extension is just fine.

How do you initialize a variable in Jinja?

Instead, use the {% set %} template tag and then assign the value the same way you would in normal python code. It was great explanation and simple one.


1 Answers

You are doing it right but the logic operators need to be lower cased.

Try switching all your operators to lower case.

like image 124
alejdg Avatar answered Oct 07 '22 02:10

alejdg