# Buggy code
def run(self, tmp=None, task_vars=None):
    del tmp  # tmp no longer has any effect

    socket_path = None

    if self._play_context.connection == "local":
        provider = load_provider(iosxr_provider_spec, self._task.args)
        pc = copy.deepcopy(self._play_context)
        if self._task.action in ["iosxr_netconf", "iosxr_config", "iosxr_command"] or (
            provider["transport"] == "cli"
        ):
            pc.connection = "network_cli"
            pc.port = int(provider["port"] or self._play_context.port or 22)
        elif provider["transport"] == "netconf":
            pc.connection = "netconf"
            pc.port = int(provider["port"] or self._play_context.port or 830)
        else:
            return {
                "failed": True,
                "msg": "Transport type %s is not valid for this module"
                % provider["transport"],
            }

        pc.network_os = "iosxr"
        pc.remote_addr = provider["host"] or self._play_context.remote_addr
        pc.port = int(provider["port"] or self._play_context.port or 22)
        pc.remote_user = provider["username"] or self._play_context.connection_user
        pc.password = provider["password"] or self._play_context.password
        pc.timeout = int(provider["timeout"] or C.PERSISTENT_COMMAND_TIMEOUT)

        display.vvv(
            "using connection plugin %s (was local)" % pc.connection, pc.remote_addr
        )
        connection = self._shared_loader_obj.connection_loader.get(
            "persistent", pc, sys.stdin
        )

        socket_path = connection.run()
        display.vvvv("socket_path: %s" % socket_path, pc.remote_addr)
        if not socket_path:
            return {
                "failed": True,
                "msg": "unable to open shell. Please see: "
                + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell",
            }

        task_vars["ansible_socket"] = socket_path
    elif self._play_context.connection in ("netconf", "network_cli"):
        provider = self._task.args.get("provider", {})
        if any(provider.values()):
            display.warning(
                "provider is unnecessary when using {0} and will be ignored".format(
                    self._play_context.connection
                )
            )
    else:
        return {
            "failed": True,
            "msg": "Connection type %s is not valid for this module"
            % self._play_context.connection,
        }

    # make sure we are in the right cli context which should be
    # enable mode and not config module
    if (
        self._play_context.connection == "local" and pc.connection == "network_cli"
    ) or self._play_context.connection == "network_cli":
        if socket_path is None:
            socket_path = self._connection.socket_path

        conn = Connection(socket_path)
        out = conn.get_prompt()
        while to_text(out, errors="surrogate_then_replace").strip().endswith(")#"):
            display.vvvv(
                "wrong context, sending exit to device", self._play_context.remote_addr
            )
            conn.send_command("abort")
            out = conn.get_prompt()

    result = super(ActionModule, self).run(task_vars=task_vars)
    return result


# Traceback (ansible.module_utils.connection.ConnectionError)
TASK [Enable Netconf service] *************************************************************************************************
task path: /Users/david/code/test/iosxr.yaml:7
<iosxr01> attempting to start connection
<iosxr01> using connection plugin netconf
<iosxr01> found existing local domain socket, using it!
<iosxr01>
<iosxr01> local domain socket path is /Users/david/.ansible/pc/3971a0dcfb
Using module file /Users/david/code/ansible/lib/ansible/modules/network/iosxr/iosxr_netconf.py
<iosxr01> ESTABLISH LOCAL CONNECTION FOR USER: david
<iosxr01> EXEC /bin/sh -c 'echo ~ &amp;&amp; sleep 0'
<iosxr01> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /Users/david/.ansible/tmp/ansible-tmp-1520963067.58-275705164356214 `" &amp;&amp; echo ansible-tmp-1520963067.58-275705164356214="` echo /Users/david/.ansible/tmp/ansible-tmp-1520963067.58-275705164356214 `" ) &amp;&amp; sleep 0'
<iosxr01> PUT /Users/david/.ansible/tmp/ansible-local-10916PvIHw4/tmpx6yOe7 TO /Users/david/.ansible/tmp/ansible-tmp-1520963067.58-275705164356214/iosxr_netconf.py
<iosxr01> EXEC /bin/sh -c 'chmod u+x /Users/david/.ansible/tmp/ansible-tmp-1520963067.58-275705164356214/ /Users/david/.ansible/tmp/ansible-tmp-1520963067.58-275705164356214/iosxr_netconf.py &amp;&amp; sleep 0'
<iosxr01> EXEC /bin/sh -c '/Users/david/code/venv/bin/python /Users/david/.ansible/tmp/ansible-tmp-1520963067.58-275705164356214/iosxr_netconf.py &amp;&amp; sleep 0'
<iosxr01> EXEC /bin/sh -c 'rm -f -r /Users/david/.ansible/tmp/ansible-tmp-1520963067.58-275705164356214/ > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/var/folders/x6/l9cfl1bs7x9_h9_v5sn94z640000gn/T/ansible_AEDyFA/ansible_module_iosxr_netconf.py", line 203, in <module>
main()
File "/var/folders/x6/l9cfl1bs7x9_h9_v5sn94z640000gn/T/ansible_AEDyFA/ansible_module_iosxr_netconf.py", line 188, in main
have = map_config_to_obj(module)
File "/var/folders/x6/l9cfl1bs7x9_h9_v5sn94z640000gn/T/ansible_AEDyFA/ansible_module_iosxr_netconf.py", line 130, in map_config_to_obj
netconf_config = get_config(module, config_filter='netconf-yang agent')
File "/var/folders/x6/l9cfl1bs7x9_h9_v5sn94z640000gn/T/ansible_AEDyFA/ansible_modlib.zip/ansible/module_utils/network/iosxr/iosxr.py", line 373, in get_config
File "/var/folders/x6/l9cfl1bs7x9_h9_v5sn94z640000gn/T/ansible_AEDyFA/ansible_modlib.zip/ansible/module_utils/network/common/netconf.py", line 70, in __rpc__
File "/var/folders/x6/l9cfl1bs7x9_h9_v5sn94z640000gn/T/ansible_AEDyFA/ansible_modlib.zip/ansible/module_utils/network/common/netconf.py", line 102, in parse_rpc_error
ansible.module_utils.connection.ConnectionError: 'NoneType' object has no attribute 'tag'

fatal: [iosxr01]: FAILED! => {
"changed": false,
"module_stderr": "Traceback (most recent call last):\n  File \"/var/folders/x6/l9cfl1bs7x9_h9_v5sn94z640000gn/T/ansible_AEDyFA/ansible_module_iosxr_netconf.py\", line 203, in <module>\n    main()\n  File \"/var/folders/x6/l9cfl1bs7x9_h9_v5sn94z640000gn/T/ansible_AEDyFA/ansible_module_iosxr_netconf.py\", line 188, in main\n    have = map_config_to_obj(module)\n  File \"/var/folders/x6/l9cfl1bs7x9_h9_v5sn94z640000gn/T/ansible_AEDyFA/ansible_module_iosxr_netconf.py\", line 130, in map_config_to_obj\n    netconf_config = get_config(module, config_filter='netconf-yang agent')\n  File \"/var/folders/x6/l9cfl1bs7x9_h9_v5sn94z640000gn/T/ansible_AEDyFA/ansible_modlib.zip/ansible/module_utils/network/iosxr/iosxr.py\", line 373, in get_config\n  File \"/var/folders/x6/l9cfl1bs7x9_h9_v5sn94z640000gn/T/ansible_AEDyFA/ansible_modlib.zip/ansible/module_utils/network/common/netconf.py\", line 70, in __rpc__\n  File \"/var/folders/x6/l9cfl1bs7x9_h9_v5sn94z640000gn/T/ansible_AEDyFA/ansible_modlib.zip/ansible/module_utils/network/common/netconf.py\", line 102, in parse_rpc_error\nansible.module_utils.connection.ConnectionError: 'NoneType' object has no attribute 'tag'\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
}

# Fixed code
def run(self, tmp=None, task_vars=None):
    del tmp  # tmp no longer has any effect

    socket_path = None
    force_cli = self._task.action in (
        "iosxr_netconf",
        "iosxr_config",
        "iosxr_command",
        "iosxr_facts",
    )

    if self._play_context.connection == "local":
        provider = load_provider(iosxr_provider_spec, self._task.args)
        pc = copy.deepcopy(self._play_context)
        if force_cli or provider["transport"] == "cli":
            pc.connection = "network_cli"
            pc.port = int(provider["port"] or self._play_context.port or 22)
        elif provider["transport"] == "netconf":
            pc.connection = "netconf"
            pc.port = int(provider["port"] or self._play_context.port or 830)
        else:
            return {
                "failed": True,
                "msg": "Transport type %s is not valid for this module"
                % provider["transport"],
            }

        pc.network_os = "iosxr"
        pc.remote_addr = provider["host"] or self._play_context.remote_addr
        pc.port = int(provider["port"] or self._play_context.port or 22)
        pc.remote_user = provider["username"] or self._play_context.connection_user
        pc.password = provider["password"] or self._play_context.password
        pc.timeout = int(provider["timeout"] or C.PERSISTENT_COMMAND_TIMEOUT)

        display.vvv(
            "using connection plugin %s (was local)" % pc.connection, pc.remote_addr
        )
        connection = self._shared_loader_obj.connection_loader.get(
            "persistent", pc, sys.stdin
        )

        socket_path = connection.run()
        display.vvvv("socket_path: %s" % socket_path, pc.remote_addr)
        if not socket_path:
            return {
                "failed": True,
                "msg": "unable to open shell. Please see: "
                + "https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell",
            }

        task_vars["ansible_socket"] = socket_path
    elif self._play_context.connection in ("netconf", "network_cli"):
        if force_cli and self._play_context.connection != "network_cli":
            return {
                "failed": True,
                "msg": "Connection type %s is not valid for module %s"
                % (self._play_context.connection, self._task.action),
            }
        provider = self._task.args.get("provider", {})
        if any(provider.values()):
            display.warning(
                "provider is unnecessary when using {0} and will be ignored".format(
                    self._play_context.connection
                )
            )
    else:
        return {
            "failed": True,
            "msg": "Connection type %s is not valid for this module"
            % self._play_context.connection,
        }

    # make sure we are in the right cli context which should be
    # enable mode and not config module
    if (
        self._play_context.connection == "local" and pc.connection == "network_cli"
    ) or self._play_context.connection == "network_cli":
        if socket_path is None:
            socket_path = self._connection.socket_path

        conn = Connection(socket_path)
        out = conn.get_prompt()
        while to_text(out, errors="surrogate_then_replace").strip().endswith(")#"):
            display.vvvv(
                "wrong context, sending exit to device", self._play_context.remote_addr
            )
            conn.send_command("abort")
            out = conn.get_prompt()

    result = super(ActionModule, self).run(task_vars=task_vars)
    return result