# Why does 'when' conditional test fail without jinja2 templating delimiters?

**URL:** <https://forum.ansible.com/t/why-does-when-conditional-test-fail-without-jinja2-templating-delimiters/27638>\
**Category:** Ansible Project\
**Created:** [May 26, 2018, 4:24pm UTC](https://forum.ansible.com/t/why-does-when-conditional-test-fail-without-jinja2-templating-delimiters/27638 "2018-05-26T16:24:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Darren\_S](https://avatars.discourse-cdn.com/v4/letter/d/59ef9b/32.png) [@Darren\_S](https://forum.ansible.com/u/Darren_S)\
**Post date:** [May 26, 2018, 4:24pm UTC](https://forum.ansible.com/t/why-does-when-conditional-test-fail-without-jinja2-templating-delimiters/27638/1 "2018-05-26T16:24:30Z")

</div>

ansible 2.5.3 on OpenBSD 6.3 amd64  
&nbsp;&nbsp;config file = /etc/ansible/ansible.cfg  
&nbsp;&nbsp;configured module search path =  
[u'/home/dspruell/.ansible/plugins/modules',  
u'/usr/share/ansible/plugins/modules']  
&nbsp;&nbsp;ansible python module location =  
/home/dspruell/.local/lib/python2.7/site-packages/ansible  
&nbsp;&nbsp;executable location = /home/dspruell/.local/bin/ansible  
&nbsp;&nbsp;python version = 2.7.14 (default, Mar 27 2018, 09:57:43) [GCC 4.2.1  
Compatible OpenBSD Clang 5.0.1 (tags/RELEASE\_501/final)]

Test playbook: the 'when' conditions on the sysctl tasks are how I  
figured were proper to format the tests and avoid using jinja2  
templating (and avoid subsequent warning from doing so):

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 26, 2018, 6:26pm UTC](https://forum.ansible.com/t/why-does-when-conditional-test-fail-without-jinja2-templating-delimiters/27638/2 "2018-05-26T18:26:51Z")

</div>

> Test playbook: the 'when' conditions on the sysctl tasks are how I  
> figured were proper to format the tests and avoid using jinja2  
> templating (and avoid subsequent warning from doing so):
> 
> ---  
> - hosts: molodetz  
> &nbsp;&nbsp;become: yes  
> &nbsp;&nbsp;vars:  
> &nbsp;&nbsp;&nbsp;&nbsp;sysctl\_limit\_semmns: kern.seminfo.semmns  
> &nbsp;&nbsp;&nbsp;&nbsp;sysctl\_limit\_semmni: kern.seminfo.semmni  
> &nbsp;&nbsp;&nbsp;&nbsp;postgres\_limit\_semmni: 60  
> &nbsp;&nbsp;&nbsp;&nbsp;postgres\_limit\_semmns: 1024
> 
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: check sysv semaphore system limits  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: sysctl -n {{ sysctl\_limit\_semmni |quote }} {{  
> sysctl\_limit\_semmns |quote }}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: sem\_limits  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;changed\_when: False
> 
> &nbsp;&nbsp;&nbsp;&nbsp;- set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;semmni\_cur\_limit: '{{ sem\_limits.stdout\_lines[0]|int }}'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;semmns\_cur\_limit: '{{ sem\_limits.stdout\_lines[1]|int }}'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;changed\_when: False
> 
> &nbsp;&nbsp;&nbsp;&nbsp;- debug:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: '{{ item }}'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- semmni\_cur\_limit  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- semmns\_cur\_limit  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- postgres\_limit\_semmni  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- postgres\_limit\_semmns  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- 'postgres\_limit\_semmni \> {{ semmni\_cur\_limit }}'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- 'postgres\_limit\_semmns \> {{ semmns\_cur\_limit }}'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- 'postgres\_limit\_semmni \> 100000'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- 'postgres\_limit\_semmns \> 100000'
> 
> &nbsp;&nbsp;&nbsp;&nbsp;- name: set sysv max semaphore identifier system limit  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sysctl:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: '{{ sysctl\_limit\_semmni }}'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value: '{{ postgres\_limit\_semmni }}'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sysctl\_set: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: postgres\_limit\_semmni \> semmni\_cur\_limit
> 
> &nbsp;&nbsp;&nbsp;&nbsp;- name: set sysv max semaphore count system limit  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sysctl:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: '{{ sysctl\_limit\_semmns }}'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value: '{{ postgres\_limit\_semmns }}'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sysctl\_set: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: postgres\_limit\_semmns \> semmns\_cur\_limit
> 
> This playbook results in the tasks being skipped because of evaluating  
> to false. The debug task shows the tests evaluating to true when using  
> jinja2 templating on the compared variable.

The semmni\_cur\_limit and semmns\_cur\_limit is strings and you need to cast/filter then through int each time you use them.  
This is how Ansible/Jinga combo works at the time.

> Here's how the second test playbook is modified to use templating  
> delimiters in the conditional:
> 
> $ diff -u test\*.yml  
> --- test.yml Sat May 26 08:47:09 2018  
> +++ test2.yml Sat May 26 08:46:47 2018  
> @@ -35,12 +35,12 @@  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: '{{ sysctl\_limit\_semmni }}'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value: '{{ postgres\_limit\_semmni }}'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sysctl\_set: yes  
> - when: postgres\_limit\_semmni \> semmni\_cur\_limit  
> + when: postgres\_limit\_semmni \> {{ semmni\_cur\_limit }}
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: set sysv max semaphore count system limit  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sysctl:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: '{{ sysctl\_limit\_semmns }}'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value: '{{ postgres\_limit\_semmns }}'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sysctl\_set: yes  
> - when: postgres\_limit\_semmns \> semmns\_cur\_limit  
> + when: postgres\_limit\_semmns \> {{ semmns\_cur\_limit }}

This is not supported in Jinja you can't use {{ }} inside {{ }}.

> With jinja2 templating delimiters the comparison evaluates to true and  
> executes the tasks, but results in the warning. Is there a different  
> way to structure the conditional properly?

&nbsp;&nbsp;&nbsp;when: postgres\_limit\_semmns \> semmns\_cur\_limit | int

> Also would like to understand why do these set\_facts still result in  
> the values being strings instead of integers?
> 
> - set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;semmni\_cur\_limit: '{{ sem\_limits.stdout\_lines[0]|int }}'  
> &nbsp;&nbsp;&nbsp;&nbsp;semmns\_cur\_limit: '{{ sem\_limits.stdout\_lines[1]|int }}'

That is just how Jinja work.  
In Jinja 2.10 some new feature is added to support native type, support in Ansible is probably coming in 2.7.

[https://github.com/ansible/ansible/pull/32738](https://github.com/ansible/ansible/pull/32738)

---

<div class="post-metadata">

**Author:** ![Darren\_S](https://avatars.discourse-cdn.com/v4/letter/d/59ef9b/32.png) [@Darren\_S](https://forum.ansible.com/u/Darren_S)\
**Post date:** [May 27, 2018, 7:34am UTC](https://forum.ansible.com/t/why-does-when-conditional-test-fail-without-jinja2-templating-delimiters/27638/3 "2018-05-27T07:34:40Z")

</div>

This is enlightening and helped make adjustments. Thanks!
