File tree Expand file tree Collapse file tree 3 files changed +75
-0
lines changed
Expand file tree Collapse file tree 3 files changed +75
-0
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ version: "1.0.2"
55readme : README.md
66authors :
77 - Andrew Block <ablock@redhat.com>
8+ - Guido Grazioli <ggraziol@redhat.com>
89description : Common utilities to support Ansible Middleware automation.
910license_file : " LICENSE"
1011tags :
Original file line number Diff line number Diff line change 1+ # -*- coding: utf-8 -*-
2+ # Copyright (C) 2021 Eric Lavarde <elavarde@redhat.com>
3+ # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4+ # SPDX-License-Identifier: GPL-3.0-or-later
5+
6+ from __future__ import (absolute_import , division , print_function )
7+ __metaclass__ = type
8+
9+ DOCUMENTATION = '''
10+ name: version_sort
11+ short_description: Sort a list according to version order instead of pure alphabetical one
12+ version_added: 2.2.0
13+ author: Eric L. (@ericzolf)
14+ description:
15+ - Sort a list according to version order instead of pure alphabetical one.
16+ options:
17+ _input:
18+ description: A list of strings to sort.
19+ type: list
20+ elements: string
21+ required: true
22+ '''
23+
24+ EXAMPLES = '''
25+ - name: Convert list of tuples into dictionary
26+ ansible.builtin.set_fact:
27+ dictionary: "{{ ['2.1', '2.10', '2.9'] | middleware_automation.common.version_sort }}"
28+ # Result is ['2.1', '2.9', '2.10']
29+ '''
30+
31+ RETURN = '''
32+ _value:
33+ description: The list of strings sorted by version.
34+ type: list
35+ elements: string
36+ '''
37+
38+ from ansible_collections .middleware_automation .common .plugins .module_utils .version import LooseVersion
39+
40+
41+ def version_sort (value , reverse = False ):
42+ '''Sort a list according to loose versions so that e.g. 2.9 is smaller than 2.10'''
43+ return sorted (value , key = LooseVersion , reverse = reverse )
44+
45+
46+ class FilterModule (object ):
47+ ''' Version sort filter '''
48+
49+ def filters (self ):
50+ return {
51+ 'version_sort' : version_sort
52+ }
Original file line number Diff line number Diff line change 1+ # -*- coding: utf-8 -*-
2+
3+ # Copyright (c) 2021, Felix Fontein <felix@fontein.de>
4+ # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5+ # SPDX-License-Identifier: GPL-3.0-or-later
6+
7+ """Provide version object to compare version numbers."""
8+
9+ from __future__ import absolute_import , division , print_function
10+ __metaclass__ = type
11+
12+
13+ from ansible .module_utils .six import raise_from
14+
15+ try :
16+ from ansible .module_utils .compat .version import LooseVersion # noqa: F401, pylint: disable=unused-import
17+ except ImportError :
18+ try :
19+ from distutils .version import LooseVersion # noqa: F401, pylint: disable=unused-import
20+ except ImportError as exc :
21+ msg = 'To use this plugin or module with ansible-core 2.11, you need to use Python < 3.12 with distutils.version present'
22+ raise_from (ImportError (msg ), exc )
You can’t perform that action at this time.
0 commit comments