# Comparing part of a string as an integer

**URL:** <https://forum.ansible.com/t/comparing-part-of-a-string-as-an-integer/28405>\
**Category:** Ansible Project\
**Created:** [August 23, 2018, 3:10pm UTC](https://forum.ansible.com/t/comparing-part-of-a-string-as-an-integer/28405 "2018-08-23T15:10:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jameson](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jameson](https://forum.ansible.com/u/Jameson)\
**Post date:** [August 23, 2018, 3:10pm UTC](https://forum.ansible.com/t/comparing-part-of-a-string-as-an-integer/28405/1 "2018-08-23T15:10:45Z")

</div>

I’m trying to write a conditional to only act on members of a list of strings when two characters in the string are less than a given number. Does anyone know if this is possible? What I’m trying now looks like:  
set\_fact:  
interfaces:

- TenGigabitEthernet 0/32
- TenGigabitEthernet 0/33

debug: var={{ interfaces }}  
loop: “{{ facts }}”  
when: item[21:22]|int \<= 32

When I attempt this, I get, “template error while templating string: expected token ‘end of print statement’, got ‘integer’. String: {{TenGigabitEthernet 0/32}}”

Any thoughts how how to achieve this are appreciated.

Thanks,  
Jameson

---

<div class="post-metadata">

**Author:** ![jean-yves](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jean-yves](https://forum.ansible.com/u/jean-yves)\
**Post date:** [August 23, 2018, 3:41pm UTC](https://forum.ansible.com/t/comparing-part-of-a-string-as-an-integer/28405/2 "2018-08-23T15:41:01Z")

</div>

Hi,

Does'nt sound good... Explain a little bit what you want if you want some good help.

To be able to test item against an integer, use something like this (not tested, but you should have the idea)  
item\>regex\_replace('^.\* 0/','')|int

Regards,

JYL

---

<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:** [August 23, 2018, 4:01pm UTC](https://forum.ansible.com/t/comparing-part-of-a-string-as-an-integer/28405/3 "2018-08-23T16:01:54Z")

</div>

> I'm trying to write a conditional to only act on members of a list of  
> strings when two characters in the string are less than a given number.  
> Does anyone know if this is possible? What I'm trying now looks like:  
> set\_fact:  
> &nbsp;&nbsp;interfaces:  
> &nbsp;&nbsp;&nbsp;&nbsp;- TenGigabitEthernet 0/32  
> &nbsp;&nbsp;&nbsp;&nbsp;- TenGigabitEthernet 0/33

Your set\_fact is missing a dash in front

> debug: var={{ interfaces }}  
> &nbsp;&nbsp;loop: "{{ facts }}"  
> &nbsp;&nbsp;when: item[21:22]|int \<= 32

The same does this debug.  
And loop and when is indented to far in.

var= takes a variable name and not the content of one, a valid expression would be var=interfaces

Why are you using {{ facts }} it doesn't exist anywhere else in your code.

Slicing in Python is [from and including:up to but not including] so your [21:22] need to be [21:23] or just [21:] which is to the end of the line.

> When I attempt this, I get, "template error while templating string:  
> expected token 'end of print statement', got 'integer'. String:  
> {{TenGigabitEthernet 0/32}}"

Because of all errors you get this message.  
So this should work

- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;interfaces:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- TenGigabitEthernet 0/32  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- TenGigabitEthernet 0/33

- debug: var=item  
&nbsp;&nbsp;loop: "{{ interfaces }}"  
&nbsp;&nbsp;when: item[21:23]|int \<= 32

---

<div class="post-metadata">

**Author:** ![Jameson](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jameson](https://forum.ansible.com/u/Jameson)\
**Post date:** [August 23, 2018, 5:22pm UTC](https://forum.ansible.com/t/comparing-part-of-a-string-as-an-integer/28405/4 "2018-08-23T17:22:59Z")

</div>

I apologize, guys. There were some typeos in my example. This isn’t the actual code I’m using because I tried to simplify the example to only include the problem I’m having. This is a copy of the playbook that includes on this scenario. Obviously, production playbook I’m putting this in will have the variable for the interfaces pulled from the device:

---

<div class="post-metadata">

**Author:** ![Jameson](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jameson](https://forum.ansible.com/u/Jameson)\
**Post date:** [August 23, 2018, 5:26pm UTC](https://forum.ansible.com/t/comparing-part-of-a-string-as-an-integer/28405/5 "2018-08-23T17:26:59Z")

</div>

Also, I should mention, that I am trying to specifically test the last two characters in the string, so there is no need for filtering the /.

---

<div class="post-metadata">

**Author:** ![flowerysong1](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@flowerysong1](https://forum.ansible.com/u/flowerysong1)\
**Post date:** [August 23, 2018, 6:26pm UTC](https://forum.ansible.com/t/comparing-part-of-a-string-as-an-integer/28405/6 "2018-08-23T18:26:15Z")

</div>

There’s nothing wrong with your condition, you’re just using debug incorrectly. ‘var’ expects a var name, so you’re double-interpolating and telling it to look up the value of the variable named ‘TenGigabitEthernet 0/32’, which fails.

---

<div class="post-metadata">

**Author:** ![Jameson](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jameson](https://forum.ansible.com/u/Jameson)\
**Post date:** [August 23, 2018, 7:14pm UTC](https://forum.ansible.com/t/comparing-part-of-a-string-as-an-integer/28405/7 "2018-08-23T19:14:06Z")

</div>

Gotcha, so debug can’t be used to debug the output of a loop. Thanks.

---

<div class="post-metadata">

**Author:** ![Jameson](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jameson](https://forum.ansible.com/u/Jameson)\
**Post date:** [August 23, 2018, 7:22pm UTC](https://forum.ansible.com/t/comparing-part-of-a-string-as-an-integer/28405/8 "2018-08-23T19:22:19Z")

</div>

Thanks for helping me sort my debug procedure. It turns out, this is actually what I was going for in the end:

---

<div class="post-metadata">

**Author:** ![flowerysong1](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@flowerysong1](https://forum.ansible.com/u/flowerysong1)\
**Post date:** [August 23, 2018, 8:08pm UTC](https://forum.ansible.com/t/comparing-part-of-a-string-as-an-integer/28405/9 "2018-08-23T20:08:36Z")

</div>

Yes, it can. You can do “msg={{ item }}” or “var=item”. You just can’t do what you did. Because it’s wrong.
