Re: [ansible-project] ternary clarification needed

2020-03-26 Thread Davide Scrimieri
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.

On Thu, Mar 26, 2020 at 9:27 AM Martin Krizek  wrote:

> On Thu, Mar 26, 2020 at 7:55 AM 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 }}"
> >
>
> Yes, this is how the ternary filter works. To work around that you can
> use a ternary statement:
>
> - set_fact:
> 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.
>
> > 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 ansible-project+unsubscr...@googlegroups.com.
> > To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/8e08a7c9-9f2a-4c83-a7c4-ab9aa389b4e2%40googlegroups.com
> .
>
> --
> 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 ansible-project+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/CADDq2ENmBvy7%2B1rXhJ--G1c2aYDPiwJnCDU2%3D3TiC8%3D-Ergxww%40mail.gmail.com
> .
>
>

-- 
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 ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAEOhr79iT18g%2Bmu%3DUZB%2B_PA7YzvNzf5jpD-aFFb0cruXBJPj8w%40mail.gmail.com.


Re: [ansible-project] ternary clarification needed

2020-03-26 Thread Martin Krizek
On Thu, Mar 26, 2020 at 7:55 AM 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 }}"
>

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

- set_fact:
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.

> 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 ansible-project+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/ansible-project/8e08a7c9-9f2a-4c83-a7c4-ab9aa389b4e2%40googlegroups.com.

-- 
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 ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CADDq2ENmBvy7%2B1rXhJ--G1c2aYDPiwJnCDU2%3D3TiC8%3D-Ergxww%40mail.gmail.com.


Re: [ansible-project] ternary clarification needed

2020-03-26 Thread Vladimir Botka
On Wed, 25 Mar 2020 23:54:57 -0700 (PDT)
Davide Scrimieri  wrote:

> 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 }}"

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

For example

  vars:
my_other_var1: "'my': {'other': {'var1': 'var1'}}"
my_other_var2:
  my:
other:
  var2: var2

  tasks:
- debug:
msg: "{{ item is string|ternary(item|from_yaml, item) }}"
  loop:
- "{{ my_other_var1 }}"
- "{{ my_other_var2 }}"

gives

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

HTH,

-vlado

-- 
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 ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/20200326091302.150b60ad%40gmail.com.


pgpLS1bWwMVxu.pgp
Description: OpenPGP digital signature