Comparing part of a string as an integer

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

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

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

Your set_fact is missing a dash in front

debug: var={{ interfaces }}
  loop: "{{ facts }}"
  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:
    interfaces:
      - TenGigabitEthernet 0/32
      - TenGigabitEthernet 0/33

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

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:

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 /.

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.

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

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

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