# Buggy code
def rpc(module, items):
    responses = list()
    for item in items:
        name = item["name"]
        xattrs = item["xattrs"]
        fetch_config = False

        args = item.get("args")
        text = item.get("text")

        name = str(name).replace("_", "-")

        if all((module.check_mode, not name.startswith("get"))):
            module.fail_json(msg="invalid rpc for running in check_mode")

        if (
            name == "command"
            and text.startswith("show configuration")
            or name == "get-configuration"
        ):
            fetch_config = True

        element = Element(name, xattrs)

        if text:
            element.text = text

        elif args:
            for key, value in iteritems(args):
                key = str(key).replace("_", "-")
                if isinstance(value, list):
                    for item in value:
                        child = SubElement(element, key)
                        if item is not True:
                            child.text = item
                else:
                    child = SubElement(element, key)
                    if value is not True:
                        child.text = value

        if fetch_config:
            reply = get_configuration(module, format=xattrs["format"])
        else:
            reply = exec_rpc(module, tostring(element), ignore_warning=False)

        if xattrs["format"] == "text":
            if fetch_config:
                data = reply.find(".//configuration-text")
            else:
                data = reply.find(".//output")

            if data is None:
                module.fail_json(msg=tostring(reply))

            responses.append(data.text.strip())

        elif xattrs["format"] == "json":
            responses.append(module.from_json(reply.text.strip()))

        elif xattrs["format"] == "set":
            data = reply.find(".//configuration-set")
            if data is None:
                module.fail_json(
                    msg="Display format 'set' is not supported by remote device."
                )
            responses.append(data.text.strip())

        else:
            responses.append(tostring(reply))

    return responses


# Traceback (TypeError)
ansible-playbook 2.4.3.0
config file = /home/am/test_cswt/ansible.cfg
configured module search path = ['/home/am/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /home/am/env2/lib/python3.6/site-packages/ansible
executable location = /home/am/env2/bin/ansible-playbook
python version = 3.6.3 (default, Feb  8 2018, 12:39:01) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
Using /home/am/test_cswt/ansible.cfg as config file
[DEPRECATION WARNING]: [defaults]hostfile option, The key is misleading as it can also be a list of hosts, a directory or a list of paths . This feature will be removed in version 2.8. Deprecation warnings can be disabled by setting
deprecation_warnings=False in ansible.cfg.
setting up inventory plugins
Parsed /home/am/test_cswt/hosts inventory source with ini plugin
Loading callback plugin default of type stdout, v2.0 from /home/am/env2/lib/python3.6/site-packages/ansible/plugins/callback/default.py

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

PLAY [show version] ************************************************************************************************************************************************************************************************************************
META: ran handlers

TASK [show ver_juniper] ********************************************************************************************************************************************************************************************************************
task path: /home/am/test_cswt/dcag_juniper.yml:16
<x.x.x.x> using connection plugin netconf
<x.x.x.x> socket_path: /home/am/.ansible/pc/656d6bb5b3
Using module file /home/am/env2/lib/python3.6/site-packages/ansible/modules/network/junos/junos_command.py
<x.x.x.x> ESTABLISH LOCAL CONNECTION FOR USER: am
<x.x.x.x> EXEC /bin/sh -c 'echo ~ &amp;&amp; sleep 0'
<x.x.x.x> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /home/am/.ansible/tmp/ansible-tmp-1518639080.5723124-44749168683326 `" &amp;&amp; echo ansible-tmp-1518639080.5723124-44749168683326="` echo /home/am/.ansible/tmp/ansible-tmp-1518639080.5723124-44749168683326 `" ) &amp;&amp; sleep 0'
<x.x.x.x> PUT /tmp/tmpk42qsj7u TO /home/am/.ansible/tmp/ansible-tmp-1518639080.5723124-44749168683326/junos_command.py
<x.x.x.x> EXEC /bin/sh -c 'chmod u+x /home/am/.ansible/tmp/ansible-tmp-1518639080.5723124-44749168683326/ /home/am/.ansible/tmp/ansible-tmp-1518639080.5723124-44749168683326/junos_command.py &amp;&amp; sleep 0'
<x.x.x.x> EXEC /bin/sh -c '/usr/bin/python /home/am/.ansible/tmp/ansible-tmp-1518639080.5723124-44749168683326/junos_command.py; rm -rf "/home/am/.ansible/tmp/ansible-tmp-1518639080.5723124-44749168683326/" > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_acLhAt/ansible_module_junos_command.py", line 446, in <module>
main()
File "/tmp/ansible_acLhAt/ansible_module_junos_command.py", line 394, in main
responses = rpc(module, items)
File "/tmp/ansible_acLhAt/ansible_module_junos_command.py", line 244, in rpc
reply = send_request(module, element, ignore_warning=False)
File "/tmp/ansible_acLhAt/ansible_modlib.zip/ansible/module_utils/netconf.py", line 42, in send_request
File "/tmp/ansible_acLhAt/ansible_modlib.zip/ansible/module_utils/connection.py", line 71, in exec_command
TypeError: int() can't convert non-string with explicit base
fatal: [x.x.x.x]: FAILED! => {
"changed": false,
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_acLhAt/ansible_module_junos_command.py\", line 446, in <module>\n    main()\n  File \"/tmp/ansible_acLhAt/ansible_module_junos_command.py\", line 394, in main\n    responses = rpc(module, items)\n  File \"/tmp/ansible_acLhAt/ansible_module_junos_command.py\", line 244, in rpc\n    reply = send_request(module, element, ignore_warning=False)\n  File \"/tmp/ansible_acLhAt/ansible_modlib.zip/ansible/module_utils/netconf.py\", line 42, in send_request\n  File \"/tmp/ansible_acLhAt/ansible_modlib.zip/ansible/module_utils/connection.py\", line 71, in exec_command\nTypeError: int() can't convert non-string with explicit base\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 0
}
to retry, use: --limit @/home/am/test_cswt/dcag_juniper.retry

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

# Fixed code
def rpc(module, items):
    responses = list()
    for item in items:
        name = item["name"]
        xattrs = item["xattrs"]
        fetch_config = False

        args = item.get("args")
        text = item.get("text")

        name = str(name).replace("_", "-")

        if all((module.check_mode, not name.startswith("get"))):
            module.fail_json(msg="invalid rpc for running in check_mode")

        if (
            name == "command"
            and text.startswith("show configuration")
            or name == "get-configuration"
        ):
            fetch_config = True

        element = Element(name, xattrs)

        if text:
            element.text = text

        elif args:
            for key, value in iteritems(args):
                key = str(key).replace("_", "-")
                if isinstance(value, list):
                    for item in value:
                        child = SubElement(element, key)
                        if item is not True:
                            child.text = item
                else:
                    child = SubElement(element, key)
                    if value is not True:
                        child.text = value

        if fetch_config:
            reply = get_configuration(module, format=xattrs["format"])
        else:
            reply = exec_rpc(
                module,
                to_text(tostring(element), errors="surrogate_then_replace"),
                ignore_warning=False,
            )

        if xattrs["format"] == "text":
            if fetch_config:
                data = reply.find(".//configuration-text")
            else:
                data = reply.find(".//output")

            if data is None:
                module.fail_json(msg=tostring(reply))

            responses.append(data.text.strip())

        elif xattrs["format"] == "json":
            responses.append(module.from_json(reply.text.strip()))

        elif xattrs["format"] == "set":
            data = reply.find(".//configuration-set")
            if data is None:
                module.fail_json(
                    msg="Display format 'set' is not supported by remote device."
                )
            responses.append(data.text.strip())

        else:
            responses.append(tostring(reply))

    return responses