mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Add version_sort filter to properly sort list of versions (#1916)
* Add version_sort filter to properly sort list of versions * Fix all comments from Felix - add changelog fragment - fix test by removing runme.sh/yml and renaming to filter_version_sort - use fully qualified name of filter in test case * Remove wrong plugin.test changelog fragment Ups... * Properly name the file version_sort.py * Update changelogs/fragments/1916-add-version-sort-filter.yml Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Eric L <ewl+git@lavar.de> Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
parent
20bd065e77
commit
4a8d6cf7cc
4 changed files with 36 additions and 0 deletions
3
changelogs/fragments/1916-add-version-sort-filter.yml
Normal file
3
changelogs/fragments/1916-add-version-sort-filter.yml
Normal file
|
@ -0,0 +1,3 @@
|
|||
add plugin.filter:
|
||||
- name: version_sort
|
||||
description: Sort a list according to version order instead of pure alphabetical one
|
21
plugins/filter/version_sort.py
Normal file
21
plugins/filter/version_sort.py
Normal file
|
@ -0,0 +1,21 @@
|
|||
# Copyright (C) 2021 Eric Lavarde <elavarde@redhat.com>
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
from distutils.version import LooseVersion
|
||||
|
||||
|
||||
def version_sort(value, reverse=False):
|
||||
'''Sort a list according to loose versions so that e.g. 2.9 is smaller than 2.10'''
|
||||
return sorted(value, key=LooseVersion, reverse=reverse)
|
||||
|
||||
|
||||
class FilterModule(object):
|
||||
''' Version sort filter '''
|
||||
|
||||
def filters(self):
|
||||
return {
|
||||
'version_sort': version_sort
|
||||
}
|
2
tests/integration/targets/filter_version_sort/aliases
Normal file
2
tests/integration/targets/filter_version_sort/aliases
Normal file
|
@ -0,0 +1,2 @@
|
|||
shippable/posix/group2
|
||||
skip/python2.6 # filters are controller only, and we no longer support Python 2.6 on the controller
|
10
tests/integration/targets/filter_version_sort/tasks/main.yml
Normal file
10
tests/integration/targets/filter_version_sort/tasks/main.yml
Normal file
|
@ -0,0 +1,10 @@
|
|||
---
|
||||
####################################################################
|
||||
# WARNING: These are designed specifically for Ansible tests #
|
||||
# and should not be used as examples of how to write Ansible roles #
|
||||
####################################################################
|
||||
|
||||
- name: validate that versions are properly sorted in a stable way
|
||||
assert:
|
||||
that:
|
||||
- "['a-1.9.rpm', 'a-1.10-1.rpm', 'a-1.09.rpm', 'b-1.01.rpm', 'a-2.1-0.rpm', 'a-1.10-0.rpm'] | community.general.version_sort == ['a-1.9.rpm', 'a-1.09.rpm', 'a-1.10-0.rpm', 'a-1.10-1.rpm', 'a-2.1-0.rpm', 'b-1.01.rpm']"
|
Loading…
Reference in a new issue