# Buggy code
def list_users(verbose=True, hashes=False):
    """
    List user accounts

    verbose : boolean
        return all information
    hashes : boolean
        include NT HASH and LM HASH in verbose output

    CLI Example:

    .. code-block:: bash

        salt '*' pdbedit.list
    """
    users = {} if verbose else []

    if verbose:
        # parse detailed user data
        res = __salt__["cmd.run_all"](
            "pdbedit --list --verbose {hashes}".format(
                hashes="--smbpasswd-style" if hashes else ""
            ),
        )

        if res["retcode"] > 0:
            log.error(res["stderr"] if "stderr" in res else res["stdout"])
        else:
            user_data = {}
            for user in res["stdout"].splitlines():
                if user.startswith("-"):
                    if len(user_data) > 0:
                        users[user_data["unix username"]] = user_data
                    user_data = {}
                else:
                    label = user[: user.index(":")].strip().lower()
                    data = user[(user.index(":") + 1) :].strip()
                    user_data[label] = data

            if len(user_data) > 0:
                users[user_data["unix username"]] = user_data
    else:
        # list users
        res = __salt__["cmd.run_all"]("pdbedit --list")

        if res["retcode"] > 0:
            return {"Error": res["stderr"] if "stderr" in res else res["stdout"]}
        else:
            for user in res["stdout"].splitlines():
                users.append(user.split(":")[0])

    return users


# Traceback (KeyError)
[atlas :: sjorge][~]
[.]$ salt-call pdbedit.list
[ERROR   ] An un-handled exception was caught by salt's global exception handler:
KeyError: u'unix username'
Traceback (most recent call last):
File "/opt/local/bin/salt-call", line 11, in <module>
salt_call()
File "/opt/local/lib/python2.7/site-packages/salt/scripts.py", line 410, in salt_call
client.run()
File "/opt/local/lib/python2.7/site-packages/salt/cli/call.py", line 57, in run
caller.run()
File "/opt/local/lib/python2.7/site-packages/salt/cli/caller.py", line 134, in run
ret = self.call()
File "/opt/local/lib/python2.7/site-packages/salt/cli/caller.py", line 212, in call
ret['return'] = func(*args, **kwargs)
File "/var/cache/salt/minion/extmods/modules/pdbedit.py", line 100, in list_users
users[user_data['unix username']] = user_data
KeyError: u'unix username'
Traceback (most recent call last):
File "/opt/local/bin/salt-call", line 11, in <module>
salt_call()
File "/opt/local/lib/python2.7/site-packages/salt/scripts.py", line 410, in salt_call
client.run()
File "/opt/local/lib/python2.7/site-packages/salt/cli/call.py", line 57, in run
caller.run()
File "/opt/local/lib/python2.7/site-packages/salt/cli/caller.py", line 134, in run
ret = self.call()
File "/opt/local/lib/python2.7/site-packages/salt/cli/caller.py", line 212, in call
ret['return'] = func(*args, **kwargs)
File "/var/cache/salt/minion/extmods/modules/pdbedit.py", line 100, in list_users
users[user_data['unix username']] = user_data
KeyError: u'unix username'

# Fixed code
def list_users(verbose=True, hashes=False):
    """
    List user accounts

    verbose : boolean
        return all information
    hashes : boolean
        include NT HASH and LM HASH in verbose output

    CLI Example:

    .. code-block:: bash

        salt '*' pdbedit.list
    """
    users = {} if verbose else []

    if verbose:
        # parse detailed user data
        res = __salt__["cmd.run_all"](
            "pdbedit --list --verbose {hashes}".format(
                hashes="--smbpasswd-style" if hashes else ""
            ),
        )

        if res["retcode"] > 0:
            log.error(res["stderr"] if "stderr" in res else res["stdout"])
            return users

        user_data = {}
        for user in res["stdout"].splitlines():
            if user.startswith("-"):
                if "unix username" in user_data:
                    users[user_data["unix username"]] = user_data
                user_data = {}
            elif ":" in user:
                label = user[: user.index(":")].strip().lower()
                data = user[(user.index(":") + 1) :].strip()
                user_data[label] = data

        if user_data:
            users[user_data["unix username"]] = user_data
    else:
        # list users
        res = __salt__["cmd.run_all"]("pdbedit --list")

        if res["retcode"] > 0:
            return {"Error": res["stderr"] if "stderr" in res else res["stdout"]}

        for user in res["stdout"].splitlines():
            if ":" not in user:
                continue
            user_data = user.split(":")
            if len(user_data) >= 3:
                users.append(user_data[0])

    return users