I have a text file and a yml file the text file has a variable
is_true = true
And the .yml file has another variable my_value which I want to assign it a value based on is_true condition I have tried many solutions online but none of them works For example
Example #1:
{% if is_true == true %}
my_value:'A'
{% elif is_true == false %}
my_value:'B'
{% endif %}
Example #2
my_value: $[if(is_true, 'A', 'B')]
I did try other examples as well but I wasn't able to properly set the value. I am not sure if this is a syntax issue or not.
BTW I do not want to write an ansible task to do this, just a yml file in which I am trying to set a value to a variable
CodePudding user response:
Try using the ternary filter:
- set_fact:
my_value: "{{ (is_true == true) | ternary ('positive', 'negative') }}"
when: is_true is defined
If the condition in parentheses is true the first value is assigned to my_value i.e. 'positive' - else if the condition is false the second value i.e. 'negative'
The when statement covers the case of is_true setting missing from your config.