From 97dce1f621165b81843ee3401c10c16c7b05d344 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Mon, 3 Oct 2022 20:36:32 +0200 Subject: [PATCH] Fix #5313: redhat_subscription module is not idempotent when pool_ids (#5319) (#5329) This fix ensures the idempotency of the redhat_subscription module when pool_ids are used. The main problem was, that a 'None' quantity was not properly handled and that the quantity check compared a string with an integer. Signed-off-by: Christoph Fiehe Signed-off-by: Christoph Fiehe Co-authored-by: Christoph Fiehe (cherry picked from commit 6fe2a84e8733c4f27d6aca7b77dbdb9bb6e173be) Co-authored-by: cfiehe --- ...-redhat_subscription-idempotency-pool_ids.yml | 2 ++ .../modules/packaging/os/redhat_subscription.py | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/5313-fix-redhat_subscription-idempotency-pool_ids.yml 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)