# Buggy code
def present(name, value, config=None):
    """
    Ensure that the named sysctl value is set in memory and persisted to the
    named configuration file. The default sysctl configuration file is
    /etc/sysctl.conf

    name
        The name of the sysctl value to edit

    value
        The sysctl value to apply

    config
        The location of the sysctl configuration file. If not specified, the
        proper location will be detected based on platform.
    """
    ret = {"name": name, "result": True, "changes": {}, "comment": ""}

    if config is None:
        # Certain linux systems will ignore /etc/sysctl.conf, get the right
        # default configuration file.
        if "sysctl.default_config" in __salt__:
            config = __salt__["sysctl.default_config"]()
        else:
            config = "/etc/sysctl.conf"

    if __opts__["test"]:
        current = __salt__["sysctl.show"]()
        configured = __salt__["sysctl.show"](config_file=config)
        if not configured:
            ret["result"] = None
            ret["comment"] = (
                "Sysctl option {0} might be changed, we failed to check config file at {1}.\n"
                "The file is either unreadable, or missing."
            ).format(name, config)
            return ret
        if name in current and name not in configured:
            if re.sub(" +|\t+", " ", current[name]) != re.sub(
                " +|\t+", " ", str(value)
            ):
                ret["result"] = None
                ret["comment"] = ("Sysctl option {0} set to be changed to {1}").format(
                    name, value
                )
                return ret
            else:
                ret["result"] = None
                ret["comment"] = (
                    "Sysctl value is currently set on the running system but not in a config file.\n"
                    "Sysctl option {0} set to be changed to {1} in config file.".format(
                        name, value
                    )
                )
                return ret
        elif name in configured and name not in current:
            ret["result"] = None
            ret["comment"] = (
                "Sysctl value {0} is present in configuration file but is not present in the running config.\n"
                "The value {0} is set to be changed to {1} "
            )
            return ret
        elif name in configured and name in current:
            if str(value).split() == __salt__["sysctl.get"](name).split():
                ret["result"] = True
                ret["comment"] = "Sysctl value {0} = {1} is already set".format(
                    name, value
                )
                return ret
        # otherwise, we don't have it set anywhere and need to set it
        ret["result"] = None
        ret["comment"] = "Sysctl option {0}  set to be changed to {1}".format(
            name, value
        )
        return ret

    update = __salt__["sysctl.persist"](name, value, config)

    if update == "Updated":
        ret["changes"] = {name: value}
        ret["comment"] = "Updated sysctl value {0} = {1}".format(name, value)
    elif update == "Already set":
        ret["comment"] = "Sysctl value {0} = {1} is already set".format(name, value)

    return ret


# Traceback (CommandExecutionError)
----------
ID: setting_CCE-27164-3-net.ipv6.conf.default.accept_ra
Function: sysctl.present
Name: net.ipv6.conf.default.accept_ra
Result: False
Comment: An exception occurred in this state: Traceback (most recent call last):
File "/usr/lib/python2.6/site-packages/salt/state.py", line 1533, in call
**cdata['kwargs'])
File "/usr/lib/python2.6/site-packages/salt/states/sysctl.py", line 88, in present
update = __salt__['sysctl.persist'](name, value, config)
File "/usr/lib/python2.6/site-packages/salt/modules/linux_sysctl.py", line 247, in persist
assign(name, value)
File "/usr/lib/python2.6/site-packages/salt/modules/linux_sysctl.py", line 138, in assign
raise CommandExecutionError('sysctl {0} does not exist'.format(name))
CommandExecutionError: sysctl net.ipv6.conf.default.accept_ra does not exist
Started: 07:42:27.369578
Duration: 33.687 ms
Changes:

# Fixed code
def present(name, value, config=None):
    """
    Ensure that the named sysctl value is set in memory and persisted to the
    named configuration file. The default sysctl configuration file is
    /etc/sysctl.conf

    name
        The name of the sysctl value to edit

    value
        The sysctl value to apply

    config
        The location of the sysctl configuration file. If not specified, the
        proper location will be detected based on platform.
    """
    ret = {"name": name, "result": True, "changes": {}, "comment": ""}

    if config is None:
        # Certain linux systems will ignore /etc/sysctl.conf, get the right
        # default configuration file.
        if "sysctl.default_config" in __salt__:
            config = __salt__["sysctl.default_config"]()
        else:
            config = "/etc/sysctl.conf"

    if __opts__["test"]:
        current = __salt__["sysctl.show"]()
        configured = __salt__["sysctl.show"](config_file=config)
        if not configured:
            ret["result"] = None
            ret["comment"] = (
                "Sysctl option {0} might be changed, we failed to check "
                "config file at {1}. The file is either unreadable, or "
                "missing.".format(name, config)
            )
            return ret
        if name in current and name not in configured:
            if re.sub(" +|\t+", " ", current[name]) != re.sub(
                " +|\t+", " ", str(value)
            ):
                ret["result"] = None
                ret["comment"] = "Sysctl option {0} set to be changed to {1}".format(
                    name, value
                )
                return ret
            else:
                ret["result"] = None
                ret["comment"] = (
                    "Sysctl value is currently set on the running system but "
                    "not in a config file. Sysctl option {0} set to be "
                    "changed to {1} in config file.".format(name, value)
                )
                return ret
        elif name in configured and name not in current:
            ret["result"] = None
            ret["comment"] = (
                "Sysctl value {0} is present in configuration file but is not "
                "present in the running config. The value {0} is set to be "
                "changed to {1}".format(name, value)
            )
            return ret
        elif name in configured and name in current:
            if str(value).split() == __salt__["sysctl.get"](name).split():
                ret["result"] = True
                ret["comment"] = "Sysctl value {0} = {1} is already set".format(
                    name, value
                )
                return ret
        # otherwise, we don't have it set anywhere and need to set it
        ret["result"] = None
        ret["comment"] = "Sysctl option {0} would be changed to {1}".format(name, value)
        return ret

    try:
        update = __salt__["sysctl.persist"](name, value, config)
    except CommandExecutionError as exc:
        ret["result"] = False
        ret["comment"] = "Failed to set {0} to {1}: {2}".format(name, value, exc)
        return ret

    if update == "Updated":
        ret["changes"] = {name: value}
        ret["comment"] = "Updated sysctl value {0} = {1}".format(name, value)
    elif update == "Already set":
        ret["comment"] = "Sysctl value {0} = {1} is already set".format(name, value)

    return ret