diff --git a/changelogs/fragments/5313-fix-redhat_subscription-idempotency-pool_ids.yml b/changelogs/fragments/5313-fix-redhat_subscription-idempotency-pool_ids.yml new file mode 100644 index 0000000000..1432dd95bd --- /dev/null +++ b/changelogs/fragments/5313-fix-redhat_subscription-idempotency-pool_ids.yml @@ -0,0 +1,2 @@ +bugfixes: + - redhat_subscription - make module idempotent when ``pool_ids`` are used (https://github.com/ansible-collections/community.general/issues/5313). diff --git a/plugins/modules/packaging/os/redhat_subscription.py b/plugins/modules/packaging/os/redhat_subscription.py index 7309ba7d66..a52cca1bb2 100644 --- a/plugins/modules/packaging/os/redhat_subscription.py +++ b/plugins/modules/packaging/os/redhat_subscription.py @@ -592,15 +592,22 @@ class Rhsm(RegistrationBase): consumed_pools = RhsmPools(self.module, consumed=True) existing_pools = {} + serials_to_remove = [] for p in consumed_pools: - existing_pools[p.get_pool_id()] = p.QuantityUsed + pool_id = p.get_pool_id() + quantity_used = p.get_quantity_used() + existing_pools[pool_id] = quantity_used + + quantity = pool_ids.get(pool_id, 0) + if quantity is not None and quantity != quantity_used: + serials_to_remove.append(p.Serial) - serials_to_remove = [p.Serial for p in consumed_pools if pool_ids.get(p.get_pool_id(), 0) != p.QuantityUsed] serials = self.unsubscribe(serials=serials_to_remove) missing_pools = {} for pool_id, quantity in sorted(pool_ids.items()): - if existing_pools.get(pool_id, 0) != quantity: + quantity_used = existing_pools.get(pool_id, 0) + if quantity is None and quantity_used == 0 or quantity not in (None, 0, quantity_used): missing_pools[pool_id] = quantity self.subscribe_by_pool_ids(missing_pools) @@ -634,6 +641,9 @@ class RhsmPool(object): def get_pool_id(self): return getattr(self, 'PoolId', getattr(self, 'PoolID')) + def get_quantity_used(self): + return int(getattr(self, 'QuantityUsed')) + def subscribe(self): args = "subscription-manager attach --pool %s" % self.get_pool_id() rc, stdout, stderr = self.module.run_command(args, check_rc=True)