Theppasin Kongsakda
Theppasin Kongsakda

Reputation: 207

How can I convert a list of string to a list of integers?

- debug: 
    var: my_list | select("greaterthan", 2) | list
  vars: 
    my_list: 
      - "1"
      - "2"
      - "3"

Errors: '>' not supported between instances of 'str' and 'int'

I've tried my_list | int | select("greaterthan", 2) | list but it does not give the expected result.

Upvotes: 3

Views: 5445

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68034

Convert the items of the list to integers. For example

    - debug:
        msg: "{{ my_list|map('int')|select('greaterthan', 2)|list }}"
      vars:
        my_list:
          - "1"
          - "2"
          - "3"

gives

    "msg": [
        3
    ]

Upvotes: 10

Related Questions