# Buggy code
def delete(name, remove=False, force=False, root=None):
    """
    Remove a user from the minion

    CLI Example:

    .. code-block:: bash

        salt '*' user.delete name remove=True force=True
    """
    cmd = ["userdel"]

    if remove:
        cmd.append("-r")

    if force and __grains__["kernel"] != "OpenBSD":
        cmd.append("-f")

    cmd.append(name)

    if root is not None:
        cmd.extend(("-R", root))

    ret = __salt__["cmd.run_all"](cmd, python_shell=False)

    if ret["retcode"] == 0:
        # Command executed with no errors
        return True

    if ret["retcode"] == 12:
        # There's a known bug in Debian based distributions, at least, that
        # makes the command exit with 12, see:
        #  https://bugs.launchpad.net/ubuntu/+source/shadow/+bug/1023509
        if __grains__["os_family"] not in ("Debian",):
            return False

        if "var/mail" in ret["stderr"] or "var/spool/mail" in ret["stderr"]:
            # We've hit the bug, let's log it and not fail
            log.debug(
                "While the userdel exited with code 12, this is a known bug on "
                "debian based distributions. See http://goo.gl/HH3FzT"
            )
            return True

    return False


# Traceback (KeyError)
gvlac201.dolgen.net:
----------
ID: admin
Function: group.present
Result: False
Comment: An exception occurred in this state: Traceback (most recent call last):
File "/opt/salt/lib/python2.7/site-packages/salt/state.py", line 1626, in call
**cdata['kwargs'])
File "/opt/salt/lib/python2.7/site-packages/salt/loader.py", line 1492, in wrapper
return f(*args, **kwargs)
File "/opt/salt/lib/python2.7/site-packages/salt/states/group.py", line 133, in present
members)
File "/opt/salt/lib/python2.7/site-packages/salt/states/group.py", line 49, in _changes
lgrp = __salt__['group.info'](name)
File "/opt/salt/lib/python2.7/site-packages/salt/loader.py", line 900, in __getitem__
func = super(LazyLoader, self).__getitem__(item)
File "/opt/salt/lib/python2.7/site-packages/salt/utils/lazy.py", line 93, in __getitem__
raise KeyError(key)
KeyError: 'group.info'
Started: 08:33:03.885764
Duration: 529.316 ms
Changes:
----------
ID: ahatch
Function: user.present
Result: False
Comment: An exception occurred in this state: Traceback (most recent call last):
File "/opt/salt/lib/python2.7/site-packages/salt/state.py", line 1626, in call
**cdata['kwargs'])
File "/opt/salt/lib/python2.7/site-packages/salt/loader.py", line 1492, in wrapper
return f(*args, **kwargs)
File "/opt/salt/lib/python2.7/site-packages/salt/states/user.py", line 385, in present
missing_groups = [x for x in groups if not __salt__['group.info'](x)]
File "/opt/salt/lib/python2.7/site-packages/salt/loader.py", line 900, in __getitem__
func = super(LazyLoader, self).__getitem__(item)
File "/opt/salt/lib/python2.7/site-packages/salt/utils/lazy.py", line 93, in __getitem__
raise KeyError(key)
KeyError: 'group.info'
Started: 08:33:04.415783
Duration: 6.421 ms
Changes:
----------
ID: ahatch_key
Function: ssh_auth.present
Name: ssh-rsa     AAAAB3NzaC1yc2EAAAABIwAAAQEA2rCAFL1YpFpfssaMJYg5PFr3FVoFtQ9LhNR2DTdLiA5KSful    E7fS7WTCDQgHDVdIAlEDdybqTktf0OExRfhmL5Qg6B6b9qYnIL5tChDC79xfby1Aqv/psIOciAYuy5    GaLLmSzUrjJsu+Z2482dCT7svcLhg/1oZ8ZZfVkh6H8wE1JL3eZ0mFQsTsV5SOFJHb11TKulBciXi    beKKNuk1hlT7nDN1wdHa5W6xU775CzQJhG37Yzgl1gAcSSDY0GXDynbz7TGIc/66FeAoc3N14sH    JFLvImvsIL+GmqpDKlaoMkBdTSRNeHMXGXKNraG2Xjirlbi2o8RZI00gvs/lm1WQ==     ahatch@gvllx000.dolgen.net
Result: False
Comment: An exception occurred in this state: Traceback (most recent call last):
File "/opt/salt/lib/python2.7/site-packages/salt/state.py", line 1626, in call
**cdata['kwargs'])
File "/opt/salt/lib/python2.7/site-packages/salt/loader.py", line 1492, in wrapper
return f(*args, **kwargs)
File "/opt/salt/lib/python2.7/site-packages/salt/states/ssh_auth.py", line 330, in present
config)
File "/opt/salt/lib/python2.7/site-packages/salt/modules/ssh.py", line 668, in set_auth_key
uinfo = __salt__['user.info'](user)
File "/opt/salt/lib/python2.7/site-packages/salt/loader.py", line 900, in __getitem__
func = super(LazyLoader, self).__getitem__(item)
File "/opt/salt/lib/python2.7/site-packages/salt/utils/lazy.py", line 93, in __getitem__
raise KeyError(key)
KeyError: 'user.info'
Started: 08:33:04.422682
Duration: 7.286 ms
Changes:

Summary for gvlac201.dolgen.net
------------
Succeeded: 0
Failed:    3
------------
Total states run:     3

# Fixed code
def delete(name, remove=False, force=False, root=None):
    """
    Remove a user from the minion

    CLI Example:

    .. code-block:: bash

        salt '*' user.delete name remove=True force=True
    """
    cmd = ["userdel"]

    if remove:
        cmd.append("-r")

    if force and __grains__["kernel"] != "OpenBSD" and __grains__["kernel"] != "AIX":
        cmd.append("-f")

    cmd.append(name)

    if root is not None and __grains__["kernel"] != "AIX":
        cmd.extend(("-R", root))

    ret = __salt__["cmd.run_all"](cmd, python_shell=False)

    if ret["retcode"] == 0:
        # Command executed with no errors
        return True

    if ret["retcode"] == 12:
        # There's a known bug in Debian based distributions, at least, that
        # makes the command exit with 12, see:
        #  https://bugs.launchpad.net/ubuntu/+source/shadow/+bug/1023509
        if __grains__["os_family"] not in ("Debian",):
            return False

        if "var/mail" in ret["stderr"] or "var/spool/mail" in ret["stderr"]:
            # We've hit the bug, let's log it and not fail
            log.debug(
                "While the userdel exited with code 12, this is a known bug on "
                "debian based distributions. See http://goo.gl/HH3FzT"
            )
            return True

    return False