Sort version number

I have a issue with sorting

I have a list from version numbers:
[‘9.0.0’, ‘11.1.1’, ‘11.1.0-h2’, ‘11.1.0’]

I want to sort these:
I have this code:
- set_fact:
sorted_versions: “{{ result | sort(reverse=true) }}”

but the sorting is then:
[‘9.0.0’, ‘11.1.1’, ‘11.1.0-h2’, ‘11.1.0’]

instead of:
[ ‘11.1.1’, ‘11.1.0-h2’, ‘11.1.0’,‘9.0.0’,]

How can I arrange that?

The sort filter is provided by Jinja2 which uses Python’s sorted that does not know how to sort versions (which versioning scheme?). You would have to provide a sorting function to sorted and in order to do that you would have to implement your own filter since provided sort does not allow that functionality.

Maybe the version_sort from the community.general collection would work for your use case? See community.general.version_sort filter – Sort a list according to version order instead of pure alphabetical one — Ansible Documentation

3 Likes

I solved the issue with adding a filter plugin.