# Buggy code
def exec_command(self, cmd, in_data=None, sudoable=True):
    # this try..except block is just to handle the transition to supporting
    # network_cli as a toplevel connection.  Once connection=local is gone,
    # this block can be removed as well and all calls passed directly to
    # the local connection
    if self._ssh_shell:
        try:
            cmd = json.loads(to_text(cmd, errors="surrogate_or_strict"))
            kwargs = {"command": to_bytes(cmd["command"], errors="surrogate_or_strict")}
            for key in ("prompt", "answer", "sendonly", "newline"):
                if cmd.get(key) is True or cmd.get(key) is False:
                    kwargs[key] = cmd[key]
                elif cmd.get(key) is not None:
                    kwargs[key] = to_bytes(cmd[key], errors="surrogate_or_strict")
            return self.send(**kwargs)
        except ValueError:
            cmd = to_bytes(cmd, errors="surrogate_or_strict")
            return self.send(command=cmd)

    else:
        return self._local.exec_command(cmd, in_data, sudoable)


# Traceback (ConnectionError)
TASK [Config Using Core Module] ************************************************************************************************************************************************************
task path: /Users/gnalawad/targets/ios_interface.yml:21
<ios> attempting to start connection
<ios> using connection plugin network_cli
<ios> found existing local domain socket, using it!
<ios>
The full traceback is:
Traceback (most recent call last):
File "/Users/home/ansible/bin/ansible-connection", line 253, in main
messages.extend(conn.update_play_context(pc_data))
File "/Users/home/ansible/lib/ansible/module_utils/connection.py", line 146, in __rpc__
raise ConnectionError(to_text(msg, errors='surrogate_then_replace'), code=code)
ConnectionError: 'NoneType' object has no attribute 'endswith'

fatal: [ios]: FAILED! => {
"failed": true,
"msg": "'NoneType' object has no attribute 'endswith'"


The full traceback is:
Traceback (most recent call last):
File "/Users/home/ansible/bin/ansible-connection", line 253, in main
messages.extend(conn.update_play_context(pc_data))
File "/Users/home/ansible/lib/ansible/module_utils/connection.py", line 146, in __rpc__
raise ConnectionError(to_text(msg, errors='surrogate_then_replace'), code=code)
ConnectionError: unable to elevate privilege to enable mode, at prompt [None]

fatal: [ios]: FAILED! => {
"failed": true,
"msg": "unable to elevate privilege to enable mode, at prompt [None]"
}

# Fixed code
def exec_command(self, cmd, in_data=None, sudoable=True):
    # this try..except block is just to handle the transition to supporting
    # network_cli as a toplevel connection.  Once connection=local is gone,
    # this block can be removed as well and all calls passed directly to
    # the local connection
    if self._ssh_shell:
        try:
            cmd = json.loads(to_text(cmd, errors="surrogate_or_strict"))
            kwargs = {"command": to_bytes(cmd["command"], errors="surrogate_or_strict")}
            for key in (
                "prompt",
                "answer",
                "sendonly",
                "newline",
                "prompt_retry_check",
            ):
                if cmd.get(key) is True or cmd.get(key) is False:
                    kwargs[key] = cmd[key]
                elif cmd.get(key) is not None:
                    kwargs[key] = to_bytes(cmd[key], errors="surrogate_or_strict")
            return self.send(**kwargs)
        except ValueError:
            cmd = to_bytes(cmd, errors="surrogate_or_strict")
            return self.send(command=cmd)

    else:
        return self._local.exec_command(cmd, in_data, sudoable)