Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
378 views
in Technique[技术] by (71.8m points)

if else and if else not working in ansible template

I am checking for pushing data based upon variables set true or false. My varaibles file which is json file has { "isfs" : True }

and my template (t2.j2)has below condition

{% if  '{ isfs | d() | bool }' -%} 
  <perform operation >
{% else -%}
  <perform 2 operation

Every time it executes <perform opertaion >even if change value of json file "isfs": False.

Please pour some suggestion.

I tried d(true) and also I tried with elif condition nothing helping me


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

just test the variable. This shoud do what you want. Any True or true value is supposed to return true.

{% if isfs %}

cast if needed:

{% if isfs | bool %}

edit: I think i misunderstood that you are evaluating against a json file that contains '{ "isfs" : True }'. You'll need to parse the json file to get the value. Considering the json file content is in the json_file variable:

{% if json_file.isfs %}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...