I'm trying to sort a list of files based on a desired extension preference. Let's say I have a list that looks like:
- name: Declare all assets
ansible.builtin.set_fact:
asset_list:
- artifact_amd64.tar.gz
- artifact_amd64.tar.gz.sbom
- artifact_arm64.tar.gz
- artifact.deb
- artifact.appimage
- artifact_amd64
- artifact_amd64.sha256sum
Now, I'd like to get sort them in some order which could be changed by prompt, say: ['deb', 'appimage', 'tar.gz', '']. In this case '' means no extension, which in this case means it's a linux executable. Regarding the files that don't match the extension, I'm not intereseted in them, I'd prefer them to be removed.
Is this possible to do without writing a custom filter? I had the idea to prepend the extensions to the items with something like: map('regex_replace', '^(.*\\.)(deb|appimage|tar\\.gz)$', '\\2 \\1\\2') but I would still need to prepend a number or something to pass it through the regular ansible sort before trying to remove it to get the final filename. The entire thing looks very complicated and hard to read in that case and I don't have a good solution for no extension.
Given the lists of the files and the extensions
Reverse the items
Use the fact that the test match:
and create the ordered list of files with extensions
gives
Create a list of executables
gives
Add the lists
gives
Notes
remove it from the list of the reversed extensions
and test it when assembling the result
gives