bool and string evaluation gives mixed result if using curly brackets

3 views
Skip to first unread message

jepper

unread,
Dec 7, 2017, 5:18:04 AM12/7/17
to Ansible Project
The snippet below evaluates to true, then false with myvar=false. How come? ansible version 2.2.1.0 on OSX High Sierra

---
- name: do stuff without curlies
  become: no
  hosts: all
  tasks:
    - name: echo
      shell: echo x
      when: something == 'yes' and myvar

- name: do stuff with curlies
  become: no
  hosts: all
  tasks:
    - name: echo
      shell: echo x
      when: "{{ something == 'yes' and myvar }}"


$ ansible-playbook play.yml -c local -i 127.0.0.1, -e myvar=false -e something="yes"

PLAY [do stuff without curlies] ************************************************

TASK [echo] ********************************************************************
Thursday 07 December 2017  10:15:48 +0000 (0:00:00.080)       0:00:00.080 *****
changed: [127.0.0.1]

PLAY [do stuff with curlies] ***************************************************

TASK [echo] ********************************************************************
Thursday 07 December 2017  10:15:49 +0000 (0:00:00.470)       0:00:00.551 *****

PLAY RECAP *********************************************************************
127.0.0.1                  : ok=1    changed=1    unreachable=0    failed=0

Kai Stian Olstad

unread,
Dec 7, 2017, 5:23:58 AM12/7/17
to ansible...@googlegroups.com
On 07.12.2017 11:18, jepper wrote:
> The snippet below evaluates to true, then false with myvar=false. How
> come?
> ansible version 2.2.1.0 on OSX High Sierra
>
> ---
> - name: do stuff without curlies
> become: no
> hosts: all
> tasks:
> - name: echo
> shell: echo x
> when: something == 'yes' and myvar
>
> - name: do stuff with curlies
> become: no
> hosts: all
> tasks:
> - name: echo
> shell: echo x
> when: "{{ something == 'yes' and myvar }}"

You can't use double curly brackets in when, in when you are in template
mode already so the double curly brackets is implied.


--
Kai Stian Olstad

jepper

unread,
Dec 7, 2017, 6:04:42 AM12/7/17
to Ansible Project
The problem is that when: "{{ something == 'yes' and myvar }}" yields the desired result e.g.:

ansible-playbook play.yml -c local -i 127.0.0.1, -e myvar=true -e something="yes"
PLAY [do stuff with curlies] ***************************************************
changed: [127.0.0.1]

I will go ahead and use "myvar | bool" everywhere as that too works.

Kai Stian Olstad

unread,
Dec 7, 2017, 6:31:27 AM12/7/17
to ansible...@googlegroups.com
On Thursday, 7 December 2017 12.04.42 CET jepper wrote:
> The problem is that when: "{{ something == 'yes' and myvar }}" yields the
> desired result e.g.:
>
> ansible-playbook play.yml -c local -i 127.0.0.1, -e myvar=true -e
> something="yes"
> PLAY [do stuff with curlies]
> ***************************************************
> changed: [127.0.0.1]

That's just luck since Jinja doesn't support stacked curly brackets.


> I will go ahead and use "myvar | bool" everywhere as that too works.

Variables values on the command line become strings so you would need to filter it with bool or check for the actual string.


--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages