Problem with "when" statement

994 views
Skip to first unread message

MKPhil

unread,
Sep 13, 2019, 5:42:14 AM9/13/19
to Ansible Project

I'm trying to compare two variables in a "when" statement - when x = y do something.

This code works:

when: unit_number == {{ disk_unit }}


BUT I get a warning:

 [WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: unit_number == {{ disk_unit }}


If I remove the {{ }} the statement no longer works.  How should I write the statement so it works and doesn't give the warning?  I've tried various combinations of double and single quotes and round brackets.


Shivharsh Singh

unread,
Sep 13, 2019, 7:44:40 AM9/13/19
to Ansible Project
Hey,

This is a genuine warning and was added to the 2.3 release purposefully. 
You don't need to use any special character at all and it will get rid of the warning. 

Simply, use the when statement as below:
---
- hosts: localhost
  connection
: local
  gather_facts
: false
  vars
:
    unit_number
: 10
    disk_unit
: 10
  tasks
:
 
- debug:
     msg
: "Hola, it works"
   
when: unit_number == disk_unit

Below is the output:
[root@oss-awx-01 AnsQuery]# ansible-playbook query.yml

PLAY
[localhost] *********************************************************************************************************************

TASK
[debug] *************************************************************************************************************************
ok
: [localhost] => {
   
"msg": "Hola, it works"
}

PLAY RECAP
***************************************************************************************************************************
localhost                  
: ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Hope it is helpful !

Thanks,
Shivharsh

Karl Auer

unread,
Sep 13, 2019, 7:58:10 AM9/13/19
to ansible-project
What does "no longer works" mean? What did you expect, what did you get?

The warning is correct - you should not use the braces in a when statement.

I'm not sure, but it sounds a bit as if the things you are comparing do not contain the values you think they do....

Regards, K.


--
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-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/92770bd7-d206-4a8c-83a8-0c949313557d%40googlegroups.com.


--
Karl Auer

Email  : ka...@2pisoftware.com
Website: http://2pisoftware.com


GPG/PGP : 301B 1F4E 624D AD99 242C 7A68 EC24 7113 E854 4A4E
Previous:
958A 2647 6C44 D376 3D63 86A5 FFB2 20BC 0257 5816

MKPhil

unread,
Sep 13, 2019, 8:28:37 AM9/13/19
to Ansible Project
I'm not sure, but it sounds a bit as if the things you are comparing do not contain the values you think they do....

That was the problem!  Even though both variables looked like ints when displayed with debug one or other of them wasn't.  I explicitly converted them to int and it now works

when: item.unit_number|int == disk_unit|int



On Friday, 13 September 2019 12:58:10 UTC+1, Karl Auer wrote:
What does "no longer works" mean? What did you expect, what did you get?

The warning is correct - you should not use the braces in a when statement.

I'm not sure, but it sounds a bit as if the things you are comparing do not contain the values you think they do....

Regards, K.


On Fri, Sep 13, 2019 at 7:42 PM MKPhil <phil....@gmail.com> wrote:

I'm trying to compare two variables in a "when" statement - when x = y do something.

This code works:

when: unit_number == {{ disk_unit }}


BUT I get a warning:

 [WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: unit_number == {{ disk_unit }}


If I remove the {{ }} the statement no longer works.  How should I write the statement so it works and doesn't give the warning?  I've tried various combinations of double and single quotes and round brackets.


--
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...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages