# Buggy code
def reload_sysctl(self):
    # do it
    if self.platform == "freebsd":
        # freebsd doesn't support -p, so reload the sysctl service
        rc, out, err = self.module.run_command(
            "/etc/rc.d/sysctl reload", environ_update=self.LANG_ENV
        )
    elif self.platform == "openbsd":
        # openbsd doesn't support -p and doesn't have a sysctl service,
        # so we have to set every value with its own sysctl call
        for k, v in self.file_values.items():
            rc = 0
            if k != self.args["name"]:
                rc = self.set_token_value(k, v)
                if rc != 0:
                    break
        if rc == 0 and self.args["state"] == "present":
            rc = self.set_token_value(self.args["name"], self.args["value"])
    else:
        # system supports reloading via the -p flag to sysctl, so we'll use that
        sysctl_args = [self.sysctl_cmd, "-p", self.sysctl_file]
        if self.args["ignoreerrors"]:
            sysctl_args.insert(1, "-e")

        rc, out, err = self.module.run_command(
            sysctl_args, environ_update=self.LANG_ENV
        )

    if rc != 0 or self._stderr_failed(err):
        self.module.fail_json(
            msg="Failed to reload sysctl: %s" % to_native(out) + to_native(err)
        )


# Traceback (UnboundLocalError)
PLAY [unknown sysctl] *******************************************************************************************************************************************************************************************************************************************************************************************************************************************************

TASK [Apply unknown sysctl] *************************************************************************************************************************************************************************************************************************************************************************************************************************************************
fatal: [puff]: FAILED! => changed=false
module_stderr: |-
Shared connection to puff closed.
module_stdout: |-
/dev/shm/root/ansible/tmp/ansible-tmp-1572094579.6784432-28211222395658/AnsiballZ_sysctl.py:18: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
import imp
Traceback (most recent call last):
File "/dev/shm/root/ansible/tmp/ansible-tmp-1572094579.6784432-28211222395658/AnsiballZ_sysctl.py", line 114, in <module>
_ansiballz_main()
File "/dev/shm/root/ansible/tmp/ansible-tmp-1572094579.6784432-28211222395658/AnsiballZ_sysctl.py", line 106, in _ansiballz_main
invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
File "/dev/shm/root/ansible/tmp/ansible-tmp-1572094579.6784432-28211222395658/AnsiballZ_sysctl.py", line 49, in invoke_module
imp.load_module('__main__', mod, module, MOD_DESC)
File "/usr/local/lib/python3.7/imp.py", line 234, in load_module
return load_source(name, filename, file)
File "/usr/local/lib/python3.7/imp.py", line 169, in load_source
module = _exec(spec, sys.modules[name])
File "<frozen importlib._bootstrap>", line 630, in _exec
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/tmp/ansible_sysctl_payload_nswrubmc/__main__.py", line 415, in <module>
File "/tmp/ansible_sysctl_payload_nswrubmc/__main__.py", line 409, in main
File "/tmp/ansible_sysctl_payload_nswrubmc/__main__.py", line 133, in __init__
File "/tmp/ansible_sysctl_payload_nswrubmc/__main__.py", line 193, in process
File "/tmp/ansible_sysctl_payload_nswrubmc/__main__.py", line 304, in reload_sysctl
UnboundLocalError: local variable 'err' referenced before assignment
msg: |-
MODULE FAILURE
See stdout/stderr for the exact error
rc: 1

PLAY RECAP ******************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
puff                       : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

# Fixed code
def reload_sysctl(self):
    if self.platform == "freebsd":
        # freebsd doesn't support -p, so reload the sysctl service
        rc, out, err = self.module.run_command(
            "/etc/rc.d/sysctl reload", environ_update=self.LANG_ENV
        )
    elif self.platform == "openbsd":
        # openbsd doesn't support -p and doesn't have a sysctl service,
        # so we have to set every value with its own sysctl call
        for k, v in self.file_values.items():
            rc = 0
            if k != self.args["name"]:
                rc = self.set_token_value(k, v)
                # FIXME this check is probably not needed as set_token_value would fail_json if rc != 0
                if rc != 0:
                    break
        if rc == 0 and self.args["state"] == "present":
            rc = self.set_token_value(self.args["name"], self.args["value"])

        # set_token_value would have called fail_json in case of failure
        # so return here and do not continue to the error processing below
        # https://github.com/ansible/ansible/issues/58158
        return
    else:
        # system supports reloading via the -p flag to sysctl, so we'll use that
        sysctl_args = [self.sysctl_cmd, "-p", self.sysctl_file]
        if self.args["ignoreerrors"]:
            sysctl_args.insert(1, "-e")

        rc, out, err = self.module.run_command(
            sysctl_args, environ_update=self.LANG_ENV
        )

    if rc != 0 or self._stderr_failed(err):
        self.module.fail_json(
            msg="Failed to reload sysctl: %s" % to_native(out) + to_native(err)
        )