summaryrefslogtreecommitdiff
path: root/filter_plugins/os-version.py
blob: 6158353fe3716fcb3d51f71177333c55a3d3f3cd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from ansible import errors
import operator

_operator_map_ = {
    "==": operator.eq,
    "<": operator.lt,
    "<=": operator.le,
    ">": operator.gt,
    ">=": operator.ge,
}


def _get_operator(op):
    try:
        return _operator_map_[op]
    except KeyError as e:
        raise errors.AnsibleFilterError("unknown operator: %s, must be one of: %s" % (str(e), ', '.join(list(_operator_map_))))


_ubuntu_release_names_ = {
    "focal": 2004,
    "groovy": 2010,
    "hisrute": 2104,
    "impish": 2110,
    "jammy": 2204,
    "kinetic": 2210,
    "lunar": 2304,
    "mantic": 2310,
    "noble": 2404,
    "oracular": 2410,
}


def _get_ubuntu_version(v):
    try:
        return _ubuntu_release_names_[v]
    except KeyError as e:
        raise errors.AnsibleFilterError("unknown ubuntu release: %s" % str(e))


_debian_release_names_ = {
    "buster": 10,
    "bullseye": 11,
    "bookworm": 12,
    "trixie": 13,
    "forky": 14,
}


def _get_debian_version(v):
    try:
        return _debian_release_names_[v]
    except KeyError as e:
        raise errors.AnsibleFilterError("unknown debian release: %s" % str(e))


def ubuntu_release_compare(a, op, b):
    try:
        av = _get_ubuntu_version(a)
        bv = _get_ubuntu_version(b)
        return _get_operator(op)(av, bv)
    except Exception as e:
        raise errors.AnsibleFilterError("ubuntu_release_compare(): %s" % str(e))


def debian_release_compare(a, op, b):
    try:
        av = _get_debian_version(a)
        bv = _get_debian_version(b)
        return _get_operator(op)(av, bv)
    except Exception as e:
        raise errors.AnsibleFilterError("debian_release_compare(): %s" % str(e))


class FilterModule(object):

    ''' os version compare '''
    filter_map = {
        'ubuntu_release_compare': ubuntu_release_compare,
        'debian_release_compare': debian_release_compare,
    }

    def filters(self):
        return self.filter_map