> 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. On Thu, Mar 26, 2020 at 6:02 PM Davide Scrimieri <[email protected]> wrote: > > 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 > > > On Thursday, March 26, 2020 at 7:54:58 AM UTC+1, Davide Scrimieri wrote: >> >> 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. >> >> >> -- > You received this message because you are subscribed to the Google Groups > "Ansible Project" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/ansible-project/3e0c7994-6e18-44bb-afc5-807607fa2a88%40googlegroups.com > <https://groups.google.com/d/msgid/ansible-project/3e0c7994-6e18-44bb-afc5-807607fa2a88%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- *Karl Auer* { manager, systems support } P: 1300 759 975 E: [email protected] 2pisoftware.com GPG/PGP : DF6A 43E5 FB9D D884 C6CA CD7F 5278 212D E80F C107 Previous: 301B 1F4E 624D AD99 242C 7A68 EC24 7113 E854 4A4E -- You received this message because you are subscribed to the Google Groups "Ansible Project" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CA%2B%2BT08Q2W5f9OZXdgdLBDHZYW8WmpyDW%3D2%3Dw1wu45kz2ji%3DE5Q%40mail.gmail.com.
