From f0b4f71a886988fe4f7ffbb35ee83db2edf92fab Mon Sep 17 00:00:00 2001 From: Julien Riou Date: Mon, 27 Nov 2023 08:51:28 +0100 Subject: [PATCH] refactor(mongodb_replicaset): Count voting members Count voting members based on the sum of "votes" and using a comprehension list (improves #614). Signed-off-by: Julien Riou --- plugins/modules/mongodb_replicaset.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/plugins/modules/mongodb_replicaset.py b/plugins/modules/mongodb_replicaset.py index 1ce52fc31..d6cdaae26 100644 --- a/plugins/modules/mongodb_replicaset.py +++ b/plugins/modules/mongodb_replicaset.py @@ -554,13 +554,11 @@ def main(): debug = module.params['debug'] cluster_cmd = module.params['cluster_cmd'] - voting_members = [] - for member in members: - if ("votes" in member and member["votes"]) or "votes" not in member: - voting_members.append(member) + # Count voting members + voting_members = sum([1 if not isinstance(m, dict) or m.get("votes", 1) == 1 else 0 for m in members]) if validate and reconfigure is False: - if len(members) <= 2 or len(voting_members) % 2 == 0: + if len(members) <= 2 or voting_members % 2 == 0: module.fail_json(msg="MongoDB Replicaset validation failed. Invalid number of replicaset members.") if arbiter_at_index is not None and len(members) - 1 < arbiter_at_index: module.fail_json(msg="MongoDB Replicaset validation failed. Invalid arbiter index.")