# ternary clarification needed

**URL:** <https://forum.ansible.com/t/ternary-clarification-needed/32535>\
**Category:** Ansible Project\
**Created:** [March 26, 2020, 6:54am UTC](https://forum.ansible.com/t/ternary-clarification-needed/32535 "2020-03-26T06:54:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Davide\_Scrimieri](https://avatars.discourse-cdn.com/v4/letter/d/8491ac/32.png) [@Davide\_Scrimieri](https://forum.ansible.com/u/Davide_Scrimieri)\
**Post date:** [March 26, 2020, 6:54am UTC](https://forum.ansible.com/t/ternary-clarification-needed/32535/1 "2020-03-26T06:54:57Z")

</div>

Hello,

Whenever a ternary is used are both conditions evaluated regardless ?

For example, I want to convert “my\_other\_var” to an object only if it is passed as as string, otherwise I don’t want to touch the object, and leave it as it is.

- set\_fact:  
my\_var: “{{ ( my\_other\_var | type\_debug == ‘str’ ) | ternary (my\_other\_var | from\_json, my\_other\_var }}”

What happens right now is this:

if my\_other\_var is str → It works  
if my\_other\_var is dict → it gives me the error saying that my\_other\_var should be a string to be converted from\_json. But I would expect that being the condition on the left = False, it would ignore the left condition on the ternary and only consider the rightmost condition.

I also tried using when:

– name: Set fact for string object  
set\_fact:  
my\_var: “{{ my\_other\_var | from\_json }}”  
when: (my\_other\_var | type\_debug == ‘str’)

- name: Set fat for dict object  
set\_fact:  
my\_var: “{{ my\_other\_var | from\_json }}”  
when: (my\_other\_var | type\_debug == ‘dict’)

However, if “my\_other\_var” is dict, the first block (named set fact for string object) gives me the same error as before. I was actually expecting that being the when statement equals to false, it would simply ignore it.

---

<div class="post-metadata">

**Author:** ![Davide\_Scrimieri](https://avatars.discourse-cdn.com/v4/letter/d/8491ac/32.png) [@Davide\_Scrimieri](https://forum.ansible.com/u/Davide_Scrimieri)\
**Post date:** [March 26, 2020, 7:01am UTC](https://forum.ansible.com/t/ternary-clarification-needed/32535/2 "2020-03-26T07:01:48Z")

</div>

UPDATE: PLEASE, DISCARD what I said about the when statement at the end of my post, in my original code I had a typo, and was not working for other reasons. The first part of the post about the ternary statement still stands

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [March 26, 2020, 7:07am UTC](https://forum.ansible.com/t/ternary-clarification-needed/32535/3 "2020-03-26T07:07:05Z")

</div>

> Whenever a ternary is used are both conditions evaluated regardless ?

A ternary filter consists of three parts:

a) the condition  
b) the statement to be evaluated if the condition is true  
c) the statement to be evaluted if the condition is false.

Only one of b) and c) will be evaluated, but the condition will always be evaluated IF the ternary statement itself is evaluated.

Regards, K.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [March 26, 2020, 8:13am UTC](https://forum.ansible.com/t/ternary-clarification-needed/32535/4 "2020-03-26T08:13:02Z")

</div>

I'd rather use "from\_yaml". JSON is a subset of YAML. How to test types see  
[https://jinja.palletsprojects.com/en/master/templates/#list-of-builtin-tests](https://jinja.palletsprojects.com/en/master/templates/#list-of-builtin-tests)

For example

&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;my\_other\_var1: "'my': {'other': {'var1': 'var1'}}"  
&nbsp;&nbsp;&nbsp;&nbsp;my\_other\_var2:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;other:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var2: var2

&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "{{ item is string|ternary(item|from\_yaml, item) }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ my\_other\_var1 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ my\_other\_var2 }}"

gives

ok: [localhost] =\> (item='my': {'other': {'var1': 'var1'}}) =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"my": {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"other": {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"var1": "var1"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;}  
}  
ok: [localhost] =\> (item={u'my': {u'other': {u'var2': u'var2'}}}) =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;"msg": {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"my": {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"other": {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"var2": "var2"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;}  
}

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Martin\_Krizek](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@Martin\_Krizek](https://forum.ansible.com/u/Martin_Krizek)\
**Post date:** [March 26, 2020, 8:26am UTC](https://forum.ansible.com/t/ternary-clarification-needed/32535/5 "2020-03-26T08:26:34Z")

</div>

> Hello,
> 
> Whenever a ternary is used are both conditions evaluated regardless ?
> 
> For example, I want to convert "my\_other\_var" to an object only if it is passed as as string, otherwise I don't want to touch the object, and leave it as it is.
> 
> - set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;my\_var: "{{ ( my\_other\_var | type\_debug == 'str' ) | ternary (my\_other\_var | from\_json, my\_other\_var }}"

Yes, this is how the ternary filter works. To work around that you can  
use a ternary statement:

- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_var: "{{ my\_other\_var | from\_json if (my\_other\_var |  
type\_debug == 'str') else my\_other\_var }}"

That should result in desired behavior.

M.

---

<div class="post-metadata">

**Author:** ![Davide\_Scrimieri](https://avatars.discourse-cdn.com/v4/letter/d/8491ac/32.png) [@Davide\_Scrimieri](https://forum.ansible.com/u/Davide_Scrimieri)\
**Post date:** [March 26, 2020, 8:32am UTC](https://forum.ansible.com/t/ternary-clarification-needed/32535/6 "2020-03-26T08:32:15Z")

</div>

Thank you all for the answers. I used the solution “from\_yaml” provided by Vladimir and it works like a charm, and it’s also cleaner. However, if this is the case, I don’t believe this is an issue of how ternary works, but of how “from\_json” works. My gut feeling is that it shouldn’t behave that way.
