# Buggy code
def chgroups(name, groups, append=False, root=None):
    """
    Change the groups to which this user belongs

    name
        User to modify

    groups
        Groups to set for the user

    append : False
        If ``True``, append the specified group(s). Otherwise, this function
        will replace the user's groups with the specified group(s).

    CLI Examples:

    .. code-block:: bash

        salt '*' user.chgroups foo wheel,root
        salt '*' user.chgroups foo wheel,root append=True
    """
    if isinstance(groups, six.string_types):
        groups = groups.split(",")
    ugrps = set(list_groups(name))
    if ugrps == set(groups):
        return True
    cmd = ["usermod"]

    if __grains__["kernel"] != "OpenBSD":
        if append:
            cmd.append("-a")
    else:
        if append:
            cmd.append("-G")
        else:
            cmd.append("-S")

    if __grains__["kernel"] != "OpenBSD":
        cmd.append("-G")
    cmd.extend([",".join(groups), name])

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

    result = __salt__["cmd.run_all"](cmd, python_shell=False)
    # try to fallback on gpasswd to add user to localgroups
    # for old lib-pamldap support
    if __grains__["kernel"] != "OpenBSD":
        if result["retcode"] != 0 and "not found in" in result["stderr"]:
            ret = True
            for group in groups:
                cmd = ["gpasswd", "-a", "{0}".format(name), "{1}".format(group)]
                if __salt__["cmd.retcode"](cmd, python_shell=False) != 0:
                    ret = False
            return ret
    return result["retcode"] == 0


# 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 chgroups(name, groups, append=False, root=None):
    """
    Change the groups to which this user belongs

    name
        User to modify

    groups
        Groups to set for the user

    append : False
        If ``True``, append the specified group(s). Otherwise, this function
        will replace the user's groups with the specified group(s).

    CLI Examples:

    .. code-block:: bash

        salt '*' user.chgroups foo wheel,root
        salt '*' user.chgroups foo wheel,root append=True
    """
    if isinstance(groups, six.string_types):
        groups = groups.split(",")
    ugrps = set(list_groups(name))
    if ugrps == set(groups):
        return True
    cmd = ["usermod"]

    if __grains__["kernel"] != "OpenBSD":
        if append and __grains__["kernel"] != "AIX":
            cmd.append("-a")
        cmd.append("-G")
    else:
        if append:
            cmd.append("-G")
        else:
            cmd.append("-S")

    if append and __grains__["kernel"] == "AIX":
        cmd.extend([",".join(ugrps) + "," + ",".join(groups), name])
    else:
        cmd.extend([",".join(groups), name])

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

    result = __salt__["cmd.run_all"](cmd, python_shell=False)
    # try to fallback on gpasswd to add user to localgroups
    # for old lib-pamldap support
    if __grains__["kernel"] != "OpenBSD" and __grains__["kernel"] != "AIX":
        if result["retcode"] != 0 and "not found in" in result["stderr"]:
            ret = True
            for group in groups:
                cmd = ["gpasswd", "-a", "{0}".format(name), "{1}".format(group)]
                if __salt__["cmd.retcode"](cmd, python_shell=False) != 0:
                    ret = False
            return ret
    return result["retcode"] == 0