Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

redhat_subscription: manually unregister only when registered #6259

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelogs/fragments/6259-redhat_subscription-fix-force.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bugfixes:
- redhat_subscription - try to unregister only when already registered when ``force_register`` is specified
(https://github.com/ansible-collections/community.general/issues/6258,
https://github.com/ansible-collections/community.general/pull/6259).
17 changes: 10 additions & 7 deletions plugins/modules/redhat_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ def _can_connect_to_dbus(self):
self.module.debug('Verified system D-Bus bus as usable')
return True

def register(self, username, password, token, auto_attach, activationkey, org_id,
def register(self, was_registered, username, password, token, auto_attach, activationkey, org_id,
consumer_type, consumer_name, consumer_id, force_register, environment,
release):
'''
Expand All @@ -463,7 +463,7 @@ def register(self, username, password, token, auto_attach, activationkey, org_id
# There is no support for token-based registration in the D-Bus API
# of rhsm, so always use the CLI in that case.
if not token and self._can_connect_to_dbus():
self._register_using_dbus(username, password, auto_attach,
self._register_using_dbus(was_registered, username, password, auto_attach,
activationkey, org_id, consumer_type,
consumer_name, consumer_id,
force_register, environment, release)
Expand Down Expand Up @@ -521,7 +521,7 @@ def _register_using_cli(self, username, password, token, auto_attach,

rc, stderr, stdout = self.module.run_command(args, check_rc=True, expand_user_and_vars=False)

def _register_using_dbus(self, username, password, auto_attach,
def _register_using_dbus(self, was_registered, username, password, auto_attach,
activationkey, org_id, consumer_type, consumer_name,
consumer_id, force_register, environment, release):
'''
Expand Down Expand Up @@ -570,7 +570,7 @@ def str2int(s, default=0):
distro_version[0] > 9)):
dbus_force_option_works = True

if force_register and not dbus_force_option_works:
if force_register and not dbus_force_option_works and was_registered:
self.unregister()

register_opts = {}
Expand All @@ -592,7 +592,7 @@ def str2int(s, default=0):
distro_version[0] >= 9)):
environment_key = 'environments'
register_opts[environment_key] = environment
if force_register and dbus_force_option_works:
if force_register and dbus_force_option_works and was_registered:
register_opts['force'] = True
# Wrap it as proper D-Bus dict
register_opts = dbus.Dictionary(register_opts, signature='sv', variant_level=1)
Expand Down Expand Up @@ -1137,8 +1137,11 @@ def main():
# Ensure system is registered
if state == 'present':

# Cache the status of the system before the changes
was_registered = rhsm.is_registered

# Register system
if rhsm.is_registered and not force_register:
if was_registered and not force_register:
if syspurpose and 'sync' in syspurpose and syspurpose['sync'] is True:
try:
rhsm.sync_syspurpose()
Expand All @@ -1165,7 +1168,7 @@ def main():
try:
rhsm.enable()
rhsm.configure(**module.params)
rhsm.register(username, password, token, auto_attach, activationkey, org_id,
rhsm.register(was_registered, username, password, token, auto_attach, activationkey, org_id,
consumer_type, consumer_name, consumer_id, force_register,
environment, release)
if syspurpose and 'sync' in syspurpose and syspurpose['sync'] is True:
Expand Down