From 477cdee79a54f98eb2ac973ef38138cf417d7bfb Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Thu, 9 May 2024 22:00:19 +0200 Subject: make debian/ubuntu os release compare script much more generic --- filter_plugins/os-version.py | 76 ++++++++++++---------- roles/apt-repo/base/defaults/main.yml | 2 +- roles/apt-repo/base/templates/Debian.list.j2 | 2 +- roles/core/base/tasks/Debian.yml | 2 +- .../autoinstall/templates/autoinstall.yml.j2 | 4 +- 5 files changed, 45 insertions(+), 41 deletions(-) diff --git a/filter_plugins/os-version.py b/filter_plugins/os-version.py index 4c5c3a29..6158353f 100644 --- a/filter_plugins/os-version.py +++ b/filter_plugins/os-version.py @@ -2,6 +2,23 @@ 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, @@ -16,6 +33,14 @@ _ubuntu_release_names_ = { "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, @@ -25,58 +50,37 @@ _debian_release_names_ = { } -def ubuntu_release_after(a, b): +def _get_debian_version(v): try: - av = _ubuntu_release_names_[a] - bv = _ubuntu_release_names_[b] - return (av > bv) + return _debian_release_names_[v] except KeyError as e: - raise errors.AnsibleFilterError("unknown ubuntu release: %s" % str(e)) - except Exception as e: - raise errors.AnsibleFilterError("ubuntu_release_after(): %s" % str(e)) - - -def ubuntu_release_before(a, b): - try: - av = _ubuntu_release_names_[a] - bv = _ubuntu_release_names_[b] - return (av < bv) - except KeyError as e: - raise errors.AnsibleFilterError("unknown ubuntu release: %s" % str(e)) - except Exception as e: - raise errors.AnsibleFilterError("ubuntu_release_before(): %s" % str(e)) + raise errors.AnsibleFilterError("unknown debian release: %s" % str(e)) -def debian_release_after(a, b): +def ubuntu_release_compare(a, op, b): try: - av = _debian_release_names_[a] - bv = _debian_release_names_[b] - return (av > bv) - except KeyError as e: - raise errors.AnsibleFilterError("unknown debian release: %s" % str(e)) + av = _get_ubuntu_version(a) + bv = _get_ubuntu_version(b) + return _get_operator(op)(av, bv) except Exception as e: - raise errors.AnsibleFilterError("debian_release_after(): %s" % str(e)) + raise errors.AnsibleFilterError("ubuntu_release_compare(): %s" % str(e)) -def debian_release_before(a, b): +def debian_release_compare(a, op, b): try: - av = _debian_release_names_[a] - bv = _debian_release_names_[b] - return (av < bv) - except KeyError as e: - raise errors.AnsibleFilterError("unknown debian release: %s" % str(e)) + 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_before(): %s" % str(e)) + raise errors.AnsibleFilterError("debian_release_compare(): %s" % str(e)) class FilterModule(object): ''' os version compare ''' filter_map = { - 'ubuntu_release_after': ubuntu_release_after, - 'ubuntu_release_before': ubuntu_release_before, - 'debian_release_after': debian_release_after, - 'debian_release_before': debian_release_before, + 'ubuntu_release_compare': ubuntu_release_compare, + 'debian_release_compare': debian_release_compare, } def filters(self): diff --git a/roles/apt-repo/base/defaults/main.yml b/roles/apt-repo/base/defaults/main.yml index f5d9193f..e7e48029 100644 --- a/roles/apt-repo/base/defaults/main.yml +++ b/roles/apt-repo/base/defaults/main.yml @@ -10,7 +10,7 @@ apt_repo_base_components: ubuntu: - main - universe - debian: "{{ ((ansible_distribution_major_version | int) <= 11) | ternary(_apt_repo_base_components_debian_.until_bullseye, _apt_repo_base_components_debian_.after_bullseye) }}" + debian: "{{ (ansible_distribution_release | debian_release_compare('<=', 'bullseye')) | ternary(_apt_repo_base_components_debian_.until_bullseye, _apt_repo_base_components_debian_.after_bullseye) }}" raspbian: - main - rpi diff --git a/roles/apt-repo/base/templates/Debian.list.j2 b/roles/apt-repo/base/templates/Debian.list.j2 index 61b4b164..91531f7e 100644 --- a/roles/apt-repo/base/templates/Debian.list.j2 +++ b/roles/apt-repo/base/templates/Debian.list.j2 @@ -1,6 +1,6 @@ deb http://{{ apt_repo_providers[apt_repo_provider].debian.host }}{{ apt_repo_providers[apt_repo_provider].debian.path }} {{ ansible_distribution_release }} {{ apt_repo_components | default(apt_repo_base_components.debian) | join(' ') }} deb http://{{ apt_repo_providers[apt_repo_provider].debian.host }}{{ apt_repo_providers[apt_repo_provider].debian.path }} {{ ansible_distribution_release }}-updates {{ apt_repo_components | default(apt_repo_base_components.debian) | join(' ') }} -{% if (ansible_distribution_major_version | int) <= 10 %} +{% if (ansible_distribution_release | debian_release_compare('<=', 'buster')) %} deb http://{{ apt_repo_providers[apt_repo_provider].debian_security.host }}{{ apt_repo_providers[apt_repo_provider].debian_security.path }} {{ ansible_distribution_release }}/updates {{ apt_repo_components | default(apt_repo_base_components.debian) | join(' ') }} {% else %} deb http://{{ apt_repo_providers[apt_repo_provider].debian_security.host }}{{ apt_repo_providers[apt_repo_provider].debian_security.path }} {{ ansible_distribution_release }}-security {{ apt_repo_components | default(apt_repo_base_components.debian) | join(' ') }} diff --git a/roles/core/base/tasks/Debian.yml b/roles/core/base/tasks/Debian.yml index 7056a9bc..a735bcc7 100644 --- a/roles/core/base/tasks/Debian.yml +++ b/roles/core/base/tasks/Debian.yml @@ -48,7 +48,7 @@ ## TODO: install dool on newer systems... or all of them. From which package source?? - name: install dstat for older systems only - when: (ansible_distribution == 'Debian' and (ansible_distribution_major_version | int) <= 12) or (ansible_distribution == 'Ubuntu' and (ansible_distribution_major_version | int) < 24) + when: (ansible_distribution == 'Debian' and (ansible_distribution_release | debian_release_compare('<=', 'bookworm'))) or (ansible_distribution == 'Ubuntu' and (ansible_distribution_release | debian_release_compare('<', 'noble'))) apt: name: dstat state: present diff --git a/roles/installer/ubuntu/autoinstall/templates/autoinstall.yml.j2 b/roles/installer/ubuntu/autoinstall/templates/autoinstall.yml.j2 index 3d12091e..841ebf63 100644 --- a/roles/installer/ubuntu/autoinstall/templates/autoinstall.yml.j2 +++ b/roles/installer/ubuntu/autoinstall/templates/autoinstall.yml.j2 @@ -259,13 +259,13 @@ autoinstall: - curtin in-target --target=/target -- bash -c "mkdir -p /etc/systemd/system/ssh.socket.d; echo -e '[Socket]\nListenStream=\nListenStream={{ ansible_port }}' > /etc/systemd/system/ssh.socket.d/port.conf" {% endif %} - curtin in-target --target=/target -- apt-get -y -q purge multipath-tools open-vm-tools -{% if not (install_codename | ubuntu_release_before('jammy')) %} +{% if (install_codename | ubuntu_release_compare('>=', 'jammy')) %} - curtin in-target --target=/target -- apt-get -y -q purge systemd-oomd {% endif %} {% if ubuntu_autoinstall_desktop is undefined %} - curtin in-target --target=/target -- apt-mark manual iputils-ping isc-dhcp-client netcat-openbsd netplan.io sudo - curtin in-target --target=/target -- apt-get -y -q purge policykit-1 ubuntu-minimal unattended-upgrades ubuntu-advantage-tools sound-theme-freedesktop thin-provisioning-tools cryptsetup byobu open-iscsi btrfs-progs pollinate lxd-agent-loader ufw -{% if not (install_codename | ubuntu_release_before('noble')) %} +{% if (install_codename | ubuntu_release_compare('>=', 'noble')) %} - curtin in-target --target=/target -- apt-get -y -q purge ubuntu-kernel-accessories {% endif %} {% if install.disks.primary != "software-raid" %} -- cgit v1.2.3