From 0cbc41534a6979f0c01e93fd03a969c0ba88c5c4 Mon Sep 17 00:00:00 2001 From: AAYUSH ANAND Date: Wed, 16 Oct 2024 12:17:59 +0530 Subject: [PATCH] Fix nxos_user purge deleting locally configured users --- plugins/modules/nxos_user.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/plugins/modules/nxos_user.py b/plugins/modules/nxos_user.py index d3e847359..060a6b7c0 100644 --- a/plugins/modules/nxos_user.py +++ b/plugins/modules/nxos_user.py @@ -411,6 +411,14 @@ def update_objects(want, have): updates.append((entry, item)) return updates +def get_configured_usernames(module): + config_output = run_commands(module, [{"command": "show running-config | section ^username", "output": "text"}]) + usernames = set() + for line in config_output[0].splitlines(): + if line.startswith("username "): + username = line.split()[1] + usernames.add(username) + return usernames def main(): """main entry point for module execution""" @@ -457,9 +465,11 @@ def main(): commands = map_obj_to_commands(update_objects(want, have), module) if module.params["purge"]: - want_users = [x["name"] for x in want] - have_users = [x["name"] for x in have] - for item in set(have_users).difference(want_users): + want_users = set([x["name"] for x in want]) + have_users = set([x["name"] for x in have]) + configured_users = get_configured_usernames(module) + + for item in have_users.difference(want_users).difference(configured_users): if item != "admin": item = item.replace("\\", "\\\\") commands.append("no username %s" % item)