Search code examples
ansiblejinja2

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.


Solution

  • 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
        ]