1

I'm trying to get the highest version number of a software product from its website with Ansible. I can get a list with the version numbers without problem like this:

set_fact:
  app_version: "{{ lookup('url', 'https://example.com/product/versions.json') 
    | regex_findall('"version":"(.+?)"', '\\1') 
    | unique }}"

The problem is to get the highest version number, because the sort() filter compares plain strings, and not like a human would.

Example:

---
- hosts: localhost
  gather_facts: no
  vars:
    list: [ '6.13.15', '6.5.8', '6.14.5' ]
  tasks:
  - name: "sort version numbers"
    set_fact:
      app_version: "{{ list | sort }}"
  - name: debug
    debug:
      var: app_version

This results in the output:

ok: [localhost] => {
    "app_version": [
        "6.13.15",
        "6.14.5",
        "6.5.8"
    ]
}

Which is obviously wrong, 6.5 should be before 6.13, not after. Is there any way to sort a list with Jinja2 filters like a human would, or at least to sort version numbers properly?

Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78
  • 1
    You can try creating a custom filter using the filter_plugin as suggested here -> https://stackoverflow.com/a/54026844/4420422 – khari-sing Sep 14 '20 at 07:00
  • Oh great, I spent at least half an hour searching if anybody asked this before. Looks like my google-fu is diminishing :( – Gerald Schneider Sep 14 '20 at 07:02

0 Answers0