# Buggy code
def interface_is_portchannel(name, module):
    if get_interface_type(name) == "ethernet":
        config = get_config(module, flags=[" | section interface"])
        if "channel group" in config:
            return True

    return False


# Traceback (ansible.module_utils.connection.ConnectionError)
ansible-playbook 2.5.0
config file = None
configured module search path = [u'/Users/larry/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python2.7/site-packages/ansible
executable location = /usr/local/bin/ansible-playbook
python version = 2.7.14 (default, Mar 10 2018, 00:01:04) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)]
No config file found; using defaults
setting up inventory plugins
Parsed /Users/larry/Git_Projects/Personal/GitHub/mrlesmithjr/ansible-cisco-ios-demo/hosts.inv inventory source with ini plugin
Loading callback plugin default of type stdout, v2.0 from /usr/local/lib/python2.7/site-packages/ansible/plugins/callback/default.pyc

PLAYBOOK: playbook.yml ***********************************************************************************************************************************************************
1 plays in playbook.yml

PLAY [Managing Cisco IOS Devices] ************************************************************************************************************************************************
META: ran handlers

TASK [Managing L2 Interfaces] ****************************************************************************************************************************************************
task path: /Users/larry/Git_Projects/Personal/GitHub/mrlesmithjr/ansible-cisco-ios-demo/playbook.yml:36
<10.10.10.3> attempting to start connection
<10.10.10.3> using connection plugin network_cli
<10.10.10.3> local domain socket does not exist, starting it
<10.10.10.3> control socket path is /Users/larry/.ansible/pc/386c9c18f0
<10.10.10.3> <10.10.10.3> ESTABLISH CONNECTION FOR USER: admin on PORT 22 TO 10.10.10.3
<10.10.10.3> <10.10.10.3> ssh connection done, setting terminal
<10.10.10.3> <10.10.10.3> loaded terminal plugin for network_os ios
<10.10.10.3> <10.10.10.3> loaded cliconf plugin for network_os ios
<10.10.10.3> <10.10.10.3> firing event: on_open_shell()
<10.10.10.3> <10.10.10.3> firing event: on_become
<10.10.10.3> <10.10.10.3> ssh connection has completed successfully
<10.10.10.3> connection to remote device started successfully
<10.10.10.3> local domain socket listeners started successfully
<10.10.10.3>
<10.10.10.3> local domain socket path is /Users/larry/.ansible/pc/386c9c18f0
Using module file /usr/local/lib/python2.7/site-packages/ansible/modules/network/ios/ios_l2_interface.py
<10.10.10.3> ESTABLISH LOCAL CONNECTION FOR USER: larry
<10.10.10.3> EXEC /bin/sh -c 'echo ~ &amp;&amp; sleep 0'
<10.10.10.3> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /Users/larry/.ansible/tmp/ansible-tmp-1524140293.77-146360030393540 `" &amp;&amp; echo ansible-tmp-1524140293.77-146360030393540="` echo /Users/larry/.ansible/tmp/ansible-tmp-1524140293.77-146360030393540 `" ) &amp;&amp; sleep 0'
<10.10.10.3> PUT /Users/larry/.ansible/tmp/ansible-local-7208okODAK/tmpTJHLZC TO /Users/larry/.ansible/tmp/ansible-tmp-1524140293.77-146360030393540/ios_l2_interface.py
<10.10.10.3> EXEC /bin/sh -c 'chmod u+x /Users/larry/.ansible/tmp/ansible-tmp-1524140293.77-146360030393540/ /Users/larry/.ansible/tmp/ansible-tmp-1524140293.77-146360030393540/ios_l2_interface.py &amp;&amp; sleep 0'
<10.10.10.3> EXEC /bin/sh -c '/usr/bin/python /Users/larry/.ansible/tmp/ansible-tmp-1524140293.77-146360030393540/ios_l2_interface.py &amp;&amp; sleep 0'
<10.10.10.3> EXEC /bin/sh -c 'rm -f -r /Users/larry/.ansible/tmp/ansible-tmp-1524140293.77-146360030393540/ > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/var/folders/sn/4yyvc32d5gzf35hdwqmfx73m0000gn/T/ansible_jDfz77/ansible_module_ios_l2_interface.py", line 488, in <module>
main()
File "/var/folders/sn/4yyvc32d5gzf35hdwqmfx73m0000gn/T/ansible_jDfz77/ansible_module_ios_l2_interface.py", line 420, in main
if interface_is_portchannel(name, module):
File "/var/folders/sn/4yyvc32d5gzf35hdwqmfx73m0000gn/T/ansible_jDfz77/ansible_module_ios_l2_interface.py", line 149, in interface_is_portchannel
config = get_config(module, flags=[' | section interface'])
File "/var/folders/sn/4yyvc32d5gzf35hdwqmfx73m0000gn/T/ansible_jDfz77/ansible_modlib.zip/ansible/module_utils/network/ios/ios.py", line 117, in get_config
File "/var/folders/sn/4yyvc32d5gzf35hdwqmfx73m0000gn/T/ansible_jDfz77/ansible_modlib.zip/ansible/module_utils/connection.py", line 146, in __rpc__
ansible.module_utils.connection.ConnectionError: show running-config  | section interface
^
% Invalid input detected at '^' marker.

switch01#

fatal: [switch01]: FAILED! => {
"changed": false,
"module_stderr": "Traceback (most recent call last):\n  File \"/var/folders/sn/4yyvc32d5gzf35hdwqmfx73m0000gn/T/ansible_jDfz77/ansible_module_ios_l2_interface.py\", line 488, in <module>\n    main()\n  File \"/var/folders/sn/4yyvc32d5gzf35hdwqmfx73m0000gn/T/ansible_jDfz77/ansible_module_ios_l2_interface.py\", line 420, in main\n    if interface_is_portchannel(name, module):\n  File \"/var/folders/sn/4yyvc32d5gzf35hdwqmfx73m0000gn/T/ansible_jDfz77/ansible_module_ios_l2_interface.py\", line 149, in interface_is_portchannel\n    config = get_config(module, flags=[' | section interface'])\n  File \"/var/folders/sn/4yyvc32d5gzf35hdwqmfx73m0000gn/T/ansible_jDfz77/ansible_modlib.zip/ansible/module_utils/network/ios/ios.py\", line 117, in get_config\n  File \"/var/folders/sn/4yyvc32d5gzf35hdwqmfx73m0000gn/T/ansible_jDfz77/ansible_modlib.zip/ansible/module_utils/connection.py\", line 146, in __rpc__\nansible.module_utils.connection.ConnectionError: show running-config  | section interface\r\n                                ^\r\n% Invalid input detected at '^' marker.\r\n\r\nswitch01#\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
}
to retry, use: --limit @/Users/larry/Git_Projects/Personal/GitHub/mrlesmithjr/ansible-cisco-ios-demo/playbook.retry

PLAY RECAP ***********************************************************************************************************************************************************************
switch01                   : ok=0    changed=0    unreachable=0    failed=1

# Fixed code
def interface_is_portchannel(name, module):
    if get_interface_type(name) == "ethernet":
        config = run_commands(module, ["show run interface {0}".format(name)])[0]
        if any(c in config for c in ["channel group", "channel-group"]):
            return True
    return False