Specific sort

18 views
Skip to first unread message

Jean-Yves LENHOF

unread,
Aug 22, 2018, 5:24:38 AM8/22/18
to Ansible Project
Hi,

I have some patch to apply in order, so I have to sort my list which
came from an RPM...

By default my list is sorted like this :
/update/v902002.5_p05.0_00_0
/update/v902002.5_p05.1_08_0
/update/v902002.5_p05.1_08_1
/update/v902002.5_p05.1_08_10
/update/v902002.5_p05.1_08_2
/update/v902002.5_p05.1_08_3
/update/v902002.5_p05.1_08_4
/update/v902002.5_p05.1_08_5
/update/v902002.5_p05.1_08_6
/update/v902002.5_p05.1_08_7
/update/v902002.5_p05.1_08_8
/update/v902002.5_p05.1_08_9

I would love an ansible way to do sort with perhaps a specific option to
do like the linux sort with option -V which give me this :

/update/v902002.5_p05.0_00_0
/update/v902002.5_p05.1_08_0
/update/v902002.5_p05.1_08_1
/update/v902002.5_p05.1_08_2
/update/v902002.5_p05.1_08_3
/update/v902002.5_p05.1_08_4
/update/v902002.5_p05.1_08_5
/update/v902002.5_p05.1_08_6
/update/v902002.5_p05.1_08_7
/update/v902002.5_p05.1_08_8
/update/v902002.5_p05.1_08_9
/update/v902002.5_p05.1_08_10

Is there's such an option of sort available ?
If not, is there a way to achieve it ?

Thanks

Regards,

JYL

Brian Coca

unread,
Aug 22, 2018, 11:00:53 AM8/22/18
to Ansible Project
Currently we have http://jinja.pocoo.org/docs/2.10/templates/#sort,
which does not seem to support a 'numeric' option. A custom filter
would be required.

--
----------
Brian Coca

Dick Visser

unread,
Aug 22, 2018, 12:40:17 PM8/22/18
to ansible...@googlegroups.com
Elaborating a bit, see https://natsort.readthedocs.io/en/master/index.html.

First "pip install natsort".
Then create filters.py:

from jinja2._compat import string_types
# Custom filter for use with Jinja2
def natsort(arg):
from natsort import natsorted
return natsorted(arg)
class FilterModule(object):
def filters(self):
return {
'natsort': natsort
}

and store it in your filters path (see 'ansible-config dump | grep
DEFAULT_FILTER_PLUGIN_PATH').


Now you can just use "{{ my_version|natsort }}" which gives the order
you're looking for.

Dick
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CACVha7eccJVwKxuuKNmaKpg_hdWb05TPhmu0tQBQvr_7wg8O4A%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT
T: +31205304488
PGP key fingerprint: F575 58C6 62C6 FD5B A9CD 217C 2667 13D4 E6EF 488D
GÉANT Vereniging (Association) is registered with the Chamber of
Commerce in Amsterdam with registration number 40535155 and operates
in the UK as a branch of GÉANT Vereniging. Registered office:
Hoekenrode 3, 1102 BR Amsterdam, The Netherlands. UK branch address:
City House, 126-130 Hills Road, Cambridge CB2 1PQ, UK.
Reply all
Reply to author
Forward
0 new messages