Skip to content

Commit

Permalink
[PR #6275/c280b793 backport][stable-6] redhat_subscription: fix D-Bus…
Browse files Browse the repository at this point in the history
… option for environments on CentOS (#6282)

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).

(cherry picked from commit c280b79)

Co-authored-by: Pino Toscano <ptoscano@redhat.com>
  • Loading branch information
patchback[bot] and ptoscano authored Apr 3, 2023
1 parent 79578e5 commit f0320b5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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).
31 changes: 26 additions & 5 deletions plugins/modules/redhat_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,8 @@ def str2int(s, default=0):
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
Expand Down Expand Up @@ -585,11 +586,31 @@ def str2int(s, default=0):
# 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:
Expand Down

0 comments on commit f0320b5

Please sign in to comment.