From c280b793de8be6613a3e5061e554521afdc436cb Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Mon, 3 Apr 2023 21:26:56 +0200 Subject: [PATCH] redhat_subscription: fix D-Bus option for environments on CentOS (#6275) Factorize the current logic to determine whether use 'environments' as D-Bus registration option (rather than 'environment') in an own function, so it is easier to read it and maintain it. With the small helper function in place, extend the logic to support CentOS: it is in practice the same as the RHEL one, with an additional check to support CentOS Stream 8 (which is a rolling release, and not versioned). --- ..._subscription-fix-environments-centos.yaml | 4 +++ plugins/modules/redhat_subscription.py | 31 ++++++++++++++++--- 2 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/6275-redhat_subscription-fix-environments-centos.yaml diff --git a/changelogs/fragments/6275-redhat_subscription-fix-environments-centos.yaml b/changelogs/fragments/6275-redhat_subscription-fix-environments-centos.yaml new file mode 100644 index 0000000000..e6780dd70a --- /dev/null +++ b/changelogs/fragments/6275-redhat_subscription-fix-environments-centos.yaml @@ -0,0 +1,4 @@ +bugfixes: + - redhat_subscription - use the right D-Bus options for environments when registering + a CentOS Stream 8 system and using ``environment`` + (https://github.com/ansible-collections/community.general/pull/6275). diff --git a/plugins/modules/redhat_subscription.py b/plugins/modules/redhat_subscription.py index 593fb5b62a..2896e73181 100644 --- a/plugins/modules/redhat_subscription.py +++ b/plugins/modules/redhat_subscription.py @@ -544,7 +544,8 @@ class Rhsm(RegistrationBase): return default distro_id = distro.id() - distro_version = tuple(str2int(p) for p in distro.version_parts()) + distro_version_parts = distro.version_parts() + distro_version = tuple(str2int(p) for p in distro_version_parts) # Stop the rhsm service when using systemd (which means Fedora or # RHEL 7+): this is because the service may not use new configuration bits @@ -585,11 +586,31 @@ class Rhsm(RegistrationBase): # of RHEL before 8.6, and then it changed to 'environments'; since # the Register*() D-Bus functions reject unknown options, we have # to pass the right option depending on the version -- funky. + def supports_option_environments(): + # subscription-manager in any supported Fedora version + # has the new option. + if distro_id == 'fedora': + return True + # Check for RHEL 8 >= 8.6, or RHEL >= 9. + if distro_id == 'rhel' and \ + ((distro_version[0] == 8 and distro_version[1] >= 6) or + distro_version[0] >= 9): + return True + # CentOS: similar checks as for RHEL, with one extra bit: + # if the 2nd part of the version is empty, it means it is + # CentOS Stream, and thus we can assume it has the latest + # version of subscription-manager. + if distro_id == 'centos' and \ + ((distro_version[0] == 8 and + (distro_version[1] >= 6 or distro_version_parts[1] == '')) or + distro_version[0] >= 9): + return True + # Unknown or old distro: assume it does not support + # the new option. + return False + environment_key = 'environment' - if distro_id == 'fedora' or \ - (distro_id == 'rhel' and - ((distro_version[0] == 8 and distro_version[1] >= 6) or - distro_version[0] >= 9)): + if supports_option_environments(): environment_key = 'environments' register_opts[environment_key] = environment if force_register and dbus_force_option_works and was_registered: