# Buggy code
def send_request(module, obj, check_rc=True):
    request = tostring(obj)
    rc, out, err = exec_command(module, request)
    if rc != 0 and check_rc:
        error_root = fromstring(err)
        fake_parent = Element("root")
        fake_parent.append(error_root)

        error_list = fake_parent.findall(".//nc:rpc-error", NS_MAP)
        if not error_list:
            module.fail_json(msg=str(err))

        warnings = []
        for rpc_error in error_list:
            message = rpc_error.find("./nc:error-message", NS_MAP).text
            severity = rpc_error.find("./nc:error-severity", NS_MAP).text

            if severity == "warning":
                warnings.append(message)
            else:
                module.fail_json(msg=str(err))
        return warnings
    return fromstring(out)


# Traceback (AttributeError)
$ ansible-playbook dev-conf.yml --extra-vars "dev=athens os=juniper" -vvvv
Using /home/app/saint/ansible/ansible.cfg as config file
Loading callback plugin default of type stdout, v2.0 from /usr/lib/python2.7/site-packages/ansible/plugins/callback/__init__.pyc

PLAYBOOK: dev-conf.yml *******************************************************************************************************************************
1 plays in dev-conf.yml

PLAY [dev-conf] **************************************************************************************************************************************

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

TASK [Retrieve router's configuration: junos] ********************************************************************************************************
task path: /home/app/saint/ansible/dev-conf.yml:12
<athens> using connection plugin netconf
<athens> socket_path: /home/app/.ansible/pc/d090e15c31
open_session() returned 0 ok
Using module file /usr/lib/python2.7/site-packages/ansible/modules/network/junos/junos_command.py
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: app
<localhost> EXEC /bin/sh -c 'echo ~ &amp;&amp; sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /home/app/.ansible/tmp/ansible-tmp-1499090340.43-6328802200596 `" &amp;&amp; echo ansible-tmp-1499090340.43-6328802200596="` echo /home/app/.ansible/tmp/ansible-tmp-1499090340.43-6328802200596 `" ) &amp;&amp; sleep 0'
<localhost> PUT /tmp/tmpGOR9Z2 TO /home/app/.ansible/tmp/ansible-tmp-1499090340.43-6328802200596/junos_command.py
<localhost> EXEC /bin/sh -c 'chmod u+x /home/app/.ansible/tmp/ansible-tmp-1499090340.43-6328802200596/ /home/app/.ansible/tmp/ansible-tmp-1499090340.43-6328802200596/junos_command.py &amp;&amp; sleep 0'
<localhost> EXEC /bin/sh -c '/usr/bin/python /home/app/.ansible/tmp/ansible-tmp-1499090340.43-6328802200596/junos_command.py; rm -rf "/home/app/.ansible/tmp/ansible-tmp-1499090340.43-6328802200596/" > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_t7Cbbz/ansible_module_junos_command.py", line 388, in <module>
main()
File "/tmp/ansible_t7Cbbz/ansible_module_junos_command.py", line 338, in main
responses = rpc(module, items)
File "/tmp/ansible_t7Cbbz/ansible_module_junos_command.py", line 229, in rpc
responses.append(data.text.strip())
AttributeError: 'NoneType' object has no attribute 'text'

fatal: [localhost]: FAILED! => {
"changed": false,
"failed": true,
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_t7Cbbz/ansible_module_junos_command.py\", line 388, in <module>\n    main()\n  File \"/tmp/ansible_t7Cbbz/ansible_module_junos_command.py\", line 338, in main\n    responses = rpc(module, items)\n  File \"/tmp/ansible_t7Cbbz/ansible_module_junos_command.py\", line 229, in rpc\n    responses.append(data.text.strip())\nAttributeError: 'NoneType' object has no attribute 'text'\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 0
}
to retry, use: --limit @/home/app/saint/ansible/dev-conf.retry

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

# Fixed code
def send_request(module, obj, check_rc=True, ignore_warning=True):
    request = tostring(obj)
    rc, out, err = exec_command(module, request)
    if rc != 0 and check_rc:
        error_root = fromstring(err)
        fake_parent = Element("root")
        fake_parent.append(error_root)

        error_list = fake_parent.findall(".//nc:rpc-error", NS_MAP)
        if not error_list:
            module.fail_json(msg=str(err))

        warnings = []
        for rpc_error in error_list:
            message = rpc_error.find("./nc:error-message", NS_MAP).text
            severity = rpc_error.find("./nc:error-severity", NS_MAP).text

            if severity == "warning" and ignore_warning:
                warnings.append(message)
            else:
                module.fail_json(msg=str(err))
        return warnings
    return fromstring(out)