Skip to content

Commit

Permalink
Fix nxos_user purge deleting locally configured users
Browse files Browse the repository at this point in the history
  • Loading branch information
AAYUSH2091 committed Oct 16, 2024
1 parent bc69035 commit 0cbc415
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions plugins/modules/nxos_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 0cbc415

Please sign in to comment.