2

I have multiple versions of scripts files within the same directory. I want ansible via the find: module, through some sort of sorting comparison to always choose the highest version file it can. The issue is that the integer sorting I desire won't work because the strings in question aren't a pure integer comparison, and the lexicographic sorting won't give me the proper expected version. These filenames have a naming "convention" but there is no exact hard-coded filename versioning to work with, the versions are random within each project, but if integer ordering could be used, I/we could determine which file to use for each task.

Example, lets say I have the following 3 strings:

script_v3.sh
script_v9.sh
script_v10.sh

The normal sorting methods/filters within ansible (such as the sort filter) will try to do a lexicographic comparison of these strings in order. This means the one with the "highest" value will be script_v9.sh which is bad as I/we expect it should be script_v10.sh this is no good, as it will try to use script_v9.sh and then cause the rest of that process/task to fail. I would love to turn them into integers to do an integer comparison/sort, but as there are other non-numerical characters in the string every attempt so far to do so has been a failure. Note that I/we also need to occasionally use the lowest version in some tasks as well, which also screws up utilizing our example if lexicographic sorting is used.

I would like to know if this is possible to accomplish through some convenient comparison method, or filter which I have overlooked, or if anyone has any better ideas? The only thing I could possibly come up with is to use a regex to strip out the integers, compare them by themselves as integers, and then finally match up the result to the filename which contains the highest 10 value and then have the task use that. However, I'm horrible at regular expressions, and I'm not even certain that's the most elegant way to approach this. Anyone who could help me out would be highly appreciated.

Viscosity
  • 311
  • 1
  • 2
  • 14
  • What I do is `file0001.txt`, `file0020.txt` –  Nov 29 '22 at 06:13
  • I actually discussed this with the team who creates those files for their application, and there are reasons why they can't change the integers in the naming structure that much, thus one of the reasons why I asked this question. It's a nice simple solution, but unfortunately one I'm not able to implement in this case. – Viscosity Nov 29 '22 at 06:28
  • Does this answer your question? [Sorting list in ansible in natural alphanumeric order](https://stackoverflow.com/questions/67601218/sorting-list-in-ansible-in-natural-alphanumeric-order) – β.εηοιτ.βε Nov 29 '22 at 07:39
  • There is already a good solution available under [How to sort version numbers in Ansible](https://stackoverflow.com/questions/54025894/how-to-sort-version-numbers-in-ansible). In the past I've tested the there provided approach approach and found it working properly. – U880D Nov 29 '22 at 07:44
  • Yes a combination of the 2nd and 3rd solutions got me to where I needed, thank you! 19955356's answer while it makes sense isn't something that's desirable to implement here. – Viscosity Dec 16 '22 at 04:38

0 Answers0