# What's correct Jinja2 operator to do "less than" on a string?

**URL:** <https://forum.ansible.com/t/whats-correct-jinja2-operator-to-do-less-than-on-a-string/21550>\
**Category:** Ansible Project\
**Created:** [May 24, 2016, 5:29pm UTC](https://forum.ansible.com/t/whats-correct-jinja2-operator-to-do-less-than-on-a-string/21550 "2016-05-24T17:29:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robert\_F](https://avatars.discourse-cdn.com/v4/letter/r/9d8465/32.png) [@Robert\_F](https://forum.ansible.com/u/Robert_F)\
**Post date:** [May 24, 2016, 5:29pm UTC](https://forum.ansible.com/t/whats-correct-jinja2-operator-to-do-less-than-on-a-string/21550/1 "2016-05-24T17:29:13Z")

</div>

I’m setting an Ansible fact like this:

set\_fact: server\_num={{ ansible\_hostname[-2:] }}

server\_num can take these values:

‘00’  
‘01’  
‘02’  
‘03’  
‘04’  
ad infinitum

If I want a conditional that will evaluate to True for all server numbers less than a particular server number ‘n’ I’d like to be able to do this:

when: server\_num \< ‘n’

However, this throws the error "error while evaluating conditional: server\_num \< ‘n’

I could do this but clearly it doesn’t scale:

when: server\_num == ‘00’ or server\_num == ‘01’ or … or server\_num == ‘n-1’

Is there a more succinct way to do this? I can’t seem to find a Jinja2 “less than” operator.

Thanks.

---

<div class="post-metadata">

**Author:** ![Josh\_Smift1](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@Josh\_Smift1](https://forum.ansible.com/u/Josh_Smift1)\
**Post date:** [May 24, 2016, 5:39pm UTC](https://forum.ansible.com/t/whats-correct-jinja2-operator-to-do-less-than-on-a-string/21550/2 "2016-05-24T17:39:28Z")

</div>

Hmm, this trivial playbook works for me:

&nbsp;&nbsp;- hosts: localhost  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- debug: msg="Yep!"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: server\_num \< '03'

With '--extra-vars server\_num=02', I get

&nbsp;&nbsp;TASK: [debug msg="Yep!"]

---

<div class="post-metadata">

**Author:** ![Robert\_F](https://avatars.discourse-cdn.com/v4/letter/r/9d8465/32.png) [@Robert\_F](https://forum.ansible.com/u/Robert_F)\
**Post date:** [May 24, 2016, 5:56pm UTC](https://forum.ansible.com/t/whats-correct-jinja2-operator-to-do-less-than-on-a-string/21550/3 "2016-05-24T17:56:41Z")

</div>

Yea, it’s weird. Sometimes “\<” works for me and sometimes it doesn’t. I couldn’t find this operator documented in the Jinja2 or Ansible docs and I don’t recall where I first learned it so I figured maybe it’s an invalid operator. I put a debug statement right before the line where I’m referencing server\_num and it said it was set as I expected it to be but the statement still failed.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [May 24, 2016, 6:01pm UTC](https://forum.ansible.com/t/whats-correct-jinja2-operator-to-do-less-than-on-a-string/21550/4 "2016-05-24T18:01:30Z")

</div>

The less than operator is documented at [http://jinja.pocoo.org/docs/dev/templates/#comparisons](http://jinja.pocoo.org/docs/dev/templates/#comparisons)

Although, trying to use mathematical operations on string values isn’t exactly always straight forward.

You might get better results from the `version_compare` filter such as:

when: server\_num|version\_compare(‘100’, ‘lt’)

See [http://docs.ansible.com/ansible/playbooks\_filters.html#version-comparison-filters](http://docs.ansible.com/ansible/playbooks_filters.html#version-comparison-filters)

---

<div class="post-metadata">

**Author:** ![Robert\_F](https://avatars.discourse-cdn.com/v4/letter/r/9d8465/32.png) [@Robert\_F](https://forum.ansible.com/u/Robert_F)\
**Post date:** [May 24, 2016, 6:07pm UTC](https://forum.ansible.com/t/whats-correct-jinja2-operator-to-do-less-than-on-a-string/21550/5 "2016-05-24T18:07:10Z")

</div>

I see. I agree that doing math on strings is somewhat questionable. I’ll check out version\_compare. Thanks Matt.

---

<div class="post-metadata">

**Author:** ![Josh\_Smift1](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@Josh\_Smift1](https://forum.ansible.com/u/Josh_Smift1)\
**Post date:** [May 24, 2016, 6:13pm UTC](https://forum.ansible.com/t/whats-correct-jinja2-operator-to-do-less-than-on-a-string/21550/6 "2016-05-24T18:13:42Z")

</div>

> I see. I agree that doing math on strings is somewhat questionable.

Well, strings can be sorted, and compared in that way. I wouldn't bet on  
what adding 1 to a string would do, but comparisons seem legit.

[http://jinja.pocoo.org/docs/dev/templates/#comparisons](http://jinja.pocoo.org/docs/dev/templates/#comparisons) is where I found  
the comparisons too, FWIW.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-Josh (jbs@care.com)

This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.
