# Buggy code
def on_become(self, passwd=None):
    if self._get_prompt().endswith(b"#"):
        return

    cmd = {"command": "enable"}
    if passwd:
        # Note: python-3.5 cannot combine u"" and r"" together.  Thus make
        # an r string and use to_text to ensure it's text on both py2 and py3.
        cmd["prompt"] = to_text(r"[\r\n]password: $", errors="surrogate_or_strict")
        cmd["answer"] = passwd

    try:
        self._exec_cli_command(to_bytes(json.dumps(cmd), errors="surrogate_or_strict"))
        prompt = self._get_prompt()
        if not prompt.endswith(b"#"):
            raise AnsibleConnectionFailure(
                "failed to elevate privilege to enable mode still at prompt [%s]"
                % prompt
            )
    except AnsibleConnectionFailure:
        prompt = self._get_prompt()
        raise AnsibleConnectionFailure(
            "unable to elevate privilege to enable mode, at prompt [%s]" % prompt
        )


# 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 on_become(self, passwd=None):
    if self._get_prompt().endswith(b"#"):
        return

    cmd = {"command": "enable"}
    if passwd:
        # Note: python-3.5 cannot combine u"" and r"" together.  Thus make
        # an r string and use to_text to ensure it's text on both py2 and py3.
        cmd["prompt"] = to_text(r"[\r\n]password: $", errors="surrogate_or_strict")
        cmd["answer"] = passwd
        cmd["prompt_retry_check"] = True
    try:
        self._exec_cli_command(to_bytes(json.dumps(cmd), errors="surrogate_or_strict"))
        prompt = self._get_prompt()
        if prompt is None or not prompt.endswith(b"#"):
            raise AnsibleConnectionFailure(
                "failed to elevate privilege to enable mode still at prompt [%s]"
                % prompt
            )
    except AnsibleConnectionFailure as e:
        prompt = self._get_prompt()
        raise AnsibleConnectionFailure(
            "unable to elevate privilege to enable mode, at prompt [%s] with error: %s"
            % (prompt, e.message)
        )