# Buggy code
def rename(name, new_name, root=None):
    """
    Change the username for a named user

    CLI Example:

    .. code-block:: bash

        salt '*' user.rename name new_name
    """
    current_info = info(name)
    if not current_info:
        raise CommandExecutionError("User '{0}' does not exist".format(name))

    new_info = info(new_name)
    if new_info:
        raise CommandExecutionError("User '{0}' already exists".format(new_name))

    cmd = ["usermod", "-l", "{0}".format(new_name), "{0}".format(name)]

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

    __salt__["cmd.run"](cmd, python_shell=False)
    return info(name).get("name") == new_name


# 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 rename(name, new_name, root=None):
    """
    Change the username for a named user

    CLI Example:

    .. code-block:: bash

        salt '*' user.rename name new_name
    """
    current_info = info(name)
    if not current_info:
        raise CommandExecutionError("User '{0}' does not exist".format(name))

    new_info = info(new_name)
    if new_info:
        raise CommandExecutionError("User '{0}' already exists".format(new_name))

    cmd = ["usermod", "-l", "{0}".format(new_name), "{0}".format(name)]

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

    __salt__["cmd.run"](cmd, python_shell=False)
    return info(name).get("name") == new_name