# Buggy code
def decode_hex(hexstring):
    if len(hexstring) < 3:
        return hexstring
    if hexstring[:2] == "0x":
        return hexstring[2:].decode("hex")
    else:
        return hexstring


# Traceback (AttributeError)
Using /etc/ansible/ansible.cfg as config file

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

PLAY [Identify network operating system using SNMP] ***********************************************************

TASK [Gathering Facts] ****************************************************************************************
Using module file /usr/lib/python2.7/site-packages/ansible/modules/system/setup.py
<xxx.xxx.xxx.xxx> ESTABLISH LOCAL CONNECTION FOR USER: tjuberg
<xxx.xxx.xxx.xxx> EXEC /bin/sh -c 'echo ~ &amp;&amp; sleep 0'
<xxx.xxx.xxx.xxx> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /home/tjuberg/.ansible/tmp/ansible-tmp-1487590039.5-43328809108677 `" &amp;&amp; echo ansible-tmp-1487590039.5-43328809108677="` echo /home/tjuberg/.ansible/tmp/ansible-tmp-1487590039.5-43328809108677 `" ) &amp;&amp; sleep 0'
<xxx.xxx.xxx.xxx> PUT /tmp/tjuberg/tmpsBN1eW TO /home/tjuberg/.ansible/tmp/ansible-tmp-1487590039.5-43328809108677/setup.py
<xxx.xxx.xxx.xxx> EXEC /bin/sh -c 'chmod u+x /home/tjuberg/.ansible/tmp/ansible-tmp-1487590039.5-43328809108677/ /home/tjuberg/.ansible/tmp/ansible-tmp-1487590039.5-43328809108677/setup.py &amp;&amp; sleep 0'
<xxx.xxx.xxx.xxx> EXEC /bin/sh -c '/usr/bin/python /home/tjuberg/.ansible/tmp/ansible-tmp-1487590039.5-43328809108677/setup.py; rm -rf "/home/tjuberg/.ansible/tmp/ansible-tmp-1487590039.5-43328809108677/" > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
ok: [xxx.xxx.xxx.xxx]
META: ran handlers

TASK [SNMP Call] **********************************************************************************************
task path: /home/tjuberg/work/gitlab/bachelor_ansible/snmp.yml:8
Using module file /usr/lib/python2.7/site-packages/ansible/modules/network/snmp_facts.py
<xxx.xxx.xxx.xxx> ESTABLISH LOCAL CONNECTION FOR USER: tjuberg
<xxx.xxx.xxx.xxx> EXEC /bin/sh -c 'echo ~ &amp;&amp; sleep 0'
<xxx.xxx.xxx.xxx> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /home/tjuberg/.ansible/tmp/ansible-tmp-1487590040.13-259195315274094 `" &amp;&amp; echo ansible-tmp-1487590040.13-259195315274094="` echo /home/tjuberg/.ansible/tmp/ansible-tmp-1487590040.13-259195315274094 `" ) &amp;&amp; sleep 0'
<xxx.xxx.xxx.xxx> PUT /tmp/tjuberg/tmpcQ5K2o TO /home/tjuberg/.ansible/tmp/ansible-tmp-1487590040.13-259195315274094/snmp_facts.py
<xxx.xxx.xxx.xxx> EXEC /bin/sh -c 'chmod u+x /home/tjuberg/.ansible/tmp/ansible-tmp-1487590040.13-259195315274094/ /home/tjuberg/.ansible/tmp/ansible-tmp-1487590040.13-259195315274094/snmp_facts.py &amp;&amp; sleep 0'
<xxx.xxx.xxx.xxx> EXEC /bin/sh -c '/usr/bin/python /home/tjuberg/.ansible/tmp/ansible-tmp-1487590040.13-259195315274094/snmp_facts.py; rm -rf "/home/tjuberg/.ansible/tmp/ansible-tmp-1487590040.13-259195315274094/" > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
[WARNING]: Removed unexpected internal key in module return: _ansible_parsed = False

An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
File "/tmp/tjuberg/ansible_i83smb6y/ansible_module_snmp_facts.py", line 375, in <module>
main()
File "/tmp/tjuberg/ansible_i83smb6y/ansible_module_snmp_facts.py", line 270, in main
results['ansible_sysdescr'] = decode_hex(current_val)
File "/tmp/tjuberg/ansible_i83smb6y/ansible_module_snmp_facts.py", line 144, in decode_hex
return hexstring[2:].decode("hex")
AttributeError: 'str' object has no attribute 'decode'

fatal: [xxx.xxx.xxx.xxx]: FAILED! => {
"changed": false,
"failed": true,
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/tjuberg/ansible_i83smb6y/ansible_module_snmp_facts.py\", line 375, in <module>\n    main()\n  File \"/tmp/tjuberg/ansible_i83smb6y/ansible_module_snmp_facts.py\", line 270, in main\n    results['ansible_sysdescr'] = decode_hex(current_val)\n  File \"/tmp/tjuberg/ansible_i83smb6y/ansible_module_snmp_facts.py\", line 144, in decode_hex\n    return hexstring[2:].decode(\"hex\")\nAttributeError: 'str' object has no attribute 'decode'\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 0
}

msg: MODULE FAILURE
to retry, use: --limit @/home/tjuberg/work/gitlab/bachelor_ansible/snmp.retry

PLAY RECAP ****************************************************************************************************
xxx.xxx.xxx.xxx             : ok=1    changed=0    unreachable=0    failed=1

# Fixed code
def decode_hex(hexstring):
    if len(hexstring) < 3:
        return hexstring
    if hexstring[:2] == "0x":
        return to_text(binascii.unhexlify(hexstring[2:]))
    else:
        return hexstring