# Buggy code
def edit_config(self, command):
    for cmd in chain(["configure terminal"], to_list(command), ["end"]):
        if isinstance(cmd, dict):
            command = cmd["command"]
            prompt = cmd["prompt"]
            answer = cmd["answer"]
            newline = cmd.get("newline", True)
        else:
            command = cmd
            prompt = None
            answer = None
            newline = True

        self.send_command(
            to_bytes(command), to_bytes(prompt), to_bytes(answer), False, newline
        )


# Traceback (ansible.module_utils.connection.ConnectionError)
___________________________________________________________
/ TASK [ios_tls_cert : Installing the TLS certifcate on the \
\ remote device]                                            /
-----------------------------------------------------------
\   ^__^
\  (oo)\_______
(__)\       )\/\
||----w |
||     ||

task path: /media/actionmystique/SAMSUNG5-Shared/home/actionmystique/Repositories/SDX-Live/Roles/roles/ios_tls_cert/tasks/push.yml:63
<172.21.85.132> using connection plugin network_cli (was local)
<172.21.85.132> starting connection from persistent connection plugin
<172.21.85.132> local domain socket does not exist, starting it
<172.21.85.132> control socket path is /root/.ansible/pc/a528aca52e
<172.21.85.132> connection to remote device started successfully
<172.21.85.132> local domain socket listeners started successfully
<172.21.85.132>
<172.21.85.132> local domain socket path is /root/.ansible/pc/a528aca52e
<172.21.85.132> socket_path: /root/.ansible/pc/a528aca52e
Using module file /media/actionmystique/SAMSUNG5-Shared/home/actionmystique/src/Ansible/git-ansible/lib/ansible/modules/network/ios/ios_config.py
<172.21.85.132> ESTABLISH LOCAL CONNECTION FOR USER: root
<172.21.85.132> EXEC /bin/sh -c '/usr/bin/python &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_wptXPP/ansible_module_ios_config.py", line 551, in <module>
main()
File "/tmp/ansible_wptXPP/ansible_module_ios_config.py", line 480, in main
load_config(module, commands)
File "/tmp/ansible_wptXPP/ansible_modlib.zip/ansible/module_utils/network/ios/ios.py", line 162, in load_config
File "/tmp/ansible_wptXPP/ansible_modlib.zip/ansible/module_utils/connection.py", line 146, in __rpc__
ansible.module_utils.connection.ConnectionError:              ^
% Invalid input detected at '^' marker.

XEv_Spine_32(ca-trustpoint)#rsakeypair actionmystique.net
XEv_Spine_32(ca-trustpoint)#None
^
% Invalid input detected at '^' marker.

XEv_Spine_32(ca-trustpoint)#

fatal: [XEv_Spine_32]: FAILED! => {
"changed": false,
"failed": true,
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_wptXPP/ansible_module_ios_config.py\", line 551, in <module>\n    main()\n  File \"/tmp/ansible_wptXPP/ansible_module_ios_config.py\", line 480, in main\n    load_config(module, commands)\n  File \"/tmp/ansible_wptXPP/ansible_modlib.zip/ansible/module_utils/network/ios/ios.py\", line 162, in load_config\n  File \"/tmp/ansible_wptXPP/ansible_modlib.zip/ansible/module_utils/connection.py\", line 146, in __rpc__\nansible.module_utils.connection.ConnectionError:              ^\r\n% Invalid input detected at '^' marker.\r\n\r\nXEv_Spine_32(ca-trustpoint)#rsakeypair actionmystique.net\r\nXEv_Spine_32(ca-trustpoint)#None\r\n                              ^\r\n% Invalid input detected at '^' marker.\r\n\r\nXEv_Spine_32(ca-trustpoint)#\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
}

# Fixed code
def edit_config(self, command):
    for cmd in chain(["configure terminal"], to_list(command), ["end"]):
        if isinstance(cmd, dict):
            command = cmd["command"]
            prompt = cmd["prompt"]
            answer = cmd["answer"]
            newline = cmd.get("newline", True)
        else:
            command = cmd
            prompt = None
            answer = None
            newline = True

        self.send_command(command, prompt, answer, False, newline)