# Buggy code
def load_config(
    module,
    command_filter,
    commit=False,
    replace=False,
    comment=None,
    admin=False,
    running=None,
    nc_get_filter=None,
):
    conn = get_connection(module)

    diff = None
    if is_netconf(module):
        # FIXME: check for platform behaviour and restore this
        # conn.lock(target = 'candidate')
        # conn.discard_changes()

        try:
            for filter in to_list(command_filter):
                conn.edit_config(filter)

            candidate = get_config(
                module, source="candidate", config_filter=nc_get_filter
            )
            diff = get_config_diff(module, running, candidate)

            if commit and diff:
                commit_config(module)
            else:
                discard_config(module)
        finally:
            # conn.unlock(target = 'candidate')
            pass

    elif is_cliconf(module):
        # to keep the pre-cliconf behaviour, make a copy, avoid adding commands to input list
        cmd_filter = deepcopy(command_filter)
        cmd_filter.insert(0, "configure terminal")
        if admin:
            cmd_filter.insert(0, "admin")
        conn.edit_config(cmd_filter)

        if module._diff:
            diff = get_config_diff(module)

        if replace:
            cmd = list()
            cmd.append(
                {
                    "command": "commit replace",
                    "prompt": "This commit will replace or remove the entire running configuration",
                    "answer": "yes",
                }
            )
            cmd.append("end")
            conn.edit_config(cmd)
        elif commit:
            commit_config(module, comment=comment)
            conn.edit_config("end")
        else:
            conn.discard_changes()

    return diff


# Traceback (ConnectionError)
16.04.18 13:57:35  TASK [common : configure root] *************************************************
16.04.18 13:57:35  task path: /home/jenkins-ci/config_iosxr/roles/common/tasks/main.yaml:326
16.04.18 13:57:35  <rt-1> using connection plugin network_cli (was local)
16.04.18 13:57:35  <rt-1> starting connection from persistent connection plugin
16.04.18 13:57:36  <rt-1> found existing local domain socket, using it!
16.04.18 13:57:36  <rt-1> updating play_context for connection
16.04.18 13:57:36  <rt-1>
16.04.18 13:57:36  <rt-1> local domain socket path is /home/jenkins-ci/.ansible/pc/bcf1355494
16.04.18 13:57:36  <rt-1> socket_path: /home/jenkins-ci/.ansible/pc/bcf1355494
16.04.18 13:57:36  Using module file /usr/lib/python2.7/site-packages/ansible/modules/network/iosxr/iosxr_config.py
16.04.18 13:57:36  <rt-1> ESTABLISH LOCAL CONNECTION FOR USER: jenkins-ci
16.04.18 13:57:36  <rt-1> EXEC /bin/sh -c 'echo ~ &amp;&amp; sleep 0'
16.04.18 13:57:36  <rt-1> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /home/jenkins-ci/.ansible/tmp/ansible-tmp-1523879856.88-230380841114231 `" &amp;&amp; echo ansible-tmp-1523879856.88-230380841114231="` echo /home/jenkins-ci/.ansible/tmp/ansible-tmp-1523879856.88-230380841114231 `" ) &amp;&amp; sleep 0'
16.04.18 13:57:36  <rt-1> PUT /home/jenkins-ci/.ansible/tmp/ansible-local-32627i6OfvX/tmpbrAMDt TO /home/jenkins-ci/.ansible/tmp/ansible-tmp-1523879856.88-230380841114231/iosxr_config.py
16.04.18 13:57:36  <rt-1> EXEC /bin/sh -c 'chmod u+x /home/jenkins-ci/.ansible/tmp/ansible-tmp-1523879856.88-230380841114231/ /home/jenkins-ci/.ansible/tmp/ansible-tmp-1523879856.88-230380841114231/iosxr_config.py &amp;&amp; sleep 0'
16.04.18 13:57:36  <rt-1> EXEC /bin/sh -c '/usr/bin/python /home/jenkins-ci/.ansible/tmp/ansible-tmp-1523879856.88-230380841114231/iosxr_config.py &amp;&amp; sleep 0'
16.04.18 13:57:42  <rt-1> EXEC /bin/sh -c 'rm -f -r /home/jenkins-ci/.ansible/tmp/ansible-tmp-1523879856.88-230380841114231/ > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
16.04.18 13:57:42  changed: [rt-decix-1] => (item={u'group': u'root-system', u'name': u'root'}) => {
16.04.18 13:57:42      "changed": true,
16.04.18 13:57:42      "commands": [
16.04.18 13:57:42          "username root",
16.04.18 13:57:42          "username root group root-system",
16.04.18 13:57:42          "username root secret 0 ********"
16.04.18 13:57:42      ],
16.04.18 13:57:42      "invocation": {
16.04.18 13:57:42          "module_args": {
16.04.18 13:57:42              "admin": true,
16.04.18 13:57:42              "after": null,
16.04.18 13:57:42              "backup": false,
16.04.18 13:57:42              "before": null,
16.04.18 13:57:42              "comment": "configured by iosxr_config",
16.04.18 13:57:42              "config": null,
16.04.18 13:57:42              "force": false,
16.04.18 13:57:42              "host": null,
16.04.18 13:57:42              "lines": [
16.04.18 13:57:42                  "username root",
16.04.18 13:57:42                  "username root group root-system",
16.04.18 13:57:42                  "username root secret 0 ********"
16.04.18 13:57:42              ],
16.04.18 13:57:42              "match": "line",
16.04.18 13:57:42              "parents": null,
16.04.18 13:57:42              "password": null,
16.04.18 13:57:42              "port": null,
16.04.18 13:57:42              "provider": {
16.04.18 13:57:42                  "host": null,
16.04.18 13:57:42                  "password": null,
16.04.18 13:57:42                  "port": null,
16.04.18 13:57:42                  "ssh_keyfile": null,
16.04.18 13:57:42                  "timeout": null,
16.04.18 13:57:42                  "transport": "cli",
16.04.18 13:57:42                  "username": null
16.04.18 13:57:42              },
16.04.18 13:57:42              "replace": "line",
16.04.18 13:57:42              "src": null,
16.04.18 13:57:42              "ssh_keyfile": null,
16.04.18 13:57:42              "timeout": null,
16.04.18 13:57:42              "username": null
16.04.18 13:57:42          }
16.04.18 13:57:42      },
16.04.18 13:57:42      "item": {
16.04.18 13:57:42          "group": "root-system",
16.04.18 13:57:42          "name": "root"
16.04.18 13:57:42      }
16.04.18 13:57:42  }
16.04.18 13:57:42
16.04.18 13:57:42  TASK [common : get user] *******************************************************
16.04.18 13:57:42  task path: /home/jenkins-ci/config_iosxr/roles/common/tasks/main.yaml:338
16.04.18 13:57:42  <rt-1> using connection plugin network_cli (was local)
16.04.18 13:57:42  <rt-1> starting connection from persistent connection plugin
16.04.18 13:57:43  <rt-1> found existing local domain socket, using it!
16.04.18 13:57:43  <rt-1> updating play_context for connection
16.04.18 13:57:43  <rt-1>
16.04.18 13:57:43  <rt-1> local domain socket path is /home/jenkins-ci/.ansible/pc/bcf1355494
16.04.18 13:57:43  <rt-1> socket_path: /home/jenkins-ci/.ansible/pc/bcf1355494
16.04.18 13:57:43  <rt-1> wrong context, sending exit to device
16.04.18 13:57:43  The full traceback is:
16.04.18 13:57:43  Traceback (most recent call last):
16.04.18 13:57:43    File "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 138, in run
16.04.18 13:57:43      res = self._execute()
16.04.18 13:57:43    File "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 554, in _execute
16.04.18 13:57:43      result = self._handler.run(task_vars=variables)
16.04.18 13:57:43    File "/usr/lib/python2.7/site-packages/ansible/plugins/action/iosxr.py", line 97, in run
16.04.18 13:57:43      conn.send_command('abort')
16.04.18 13:57:43    File "/usr/lib/python2.7/site-packages/ansible/module_utils/connection.py", line 146, in __rpc__
16.04.18 13:57:43      raise ConnectionError(to_text(msg, errors='surrogate_then_replace'), code=code)
16.04.18 13:57:43  ConnectionError: abort
16.04.18 13:57:43
^
16.04.18 13:57:43  % Invalid input detected at '^' marker.
16.04.18 13:57:43  RP/0/RSP0/CPU0:rt-decix-1(admin)#
16.04.18 13:57:43  fatal: [rt-decix-1]: FAILED! => {
16.04.18 13:57:43      "msg": "Unexpected failure during module execution.",
16.04.18 13:57:43      "stdout": ""
16.04.18 13:57:43  }
16.04.18 13:57:43  	to retry, use: --limit @/home/jenkins-ci/config_iosxr/roles/conf_device.retry

# Fixed code
def load_config(
    module,
    command_filter,
    commit=False,
    replace=False,
    comment=None,
    admin=False,
    running=None,
    nc_get_filter=None,
):
    conn = get_connection(module)

    diff = None
    if is_netconf(module):
        # FIXME: check for platform behaviour and restore this
        # conn.lock(target = 'candidate')
        # conn.discard_changes()

        try:
            for filter in to_list(command_filter):
                conn.edit_config(filter)

            candidate = get_config(
                module, source="candidate", config_filter=nc_get_filter
            )
            diff = get_config_diff(module, running, candidate)

            if commit and diff:
                commit_config(module)
            else:
                discard_config(module)
        finally:
            # conn.unlock(target = 'candidate')
            pass

    elif is_cliconf(module):
        # to keep the pre-cliconf behaviour, make a copy, avoid adding commands to input list
        cmd_filter = deepcopy(command_filter)
        cmd_filter.insert(0, "configure terminal")
        if admin:
            cmd_filter.insert(0, "admin")
        conn.edit_config(cmd_filter)

        if module._diff:
            diff = get_config_diff(module)

        if replace:
            cmd = list()
            cmd.append(
                {
                    "command": "commit replace",
                    "prompt": "This commit will replace or remove the entire running configuration",
                    "answer": "yes",
                }
            )
            cmd.append("end")
            conn.edit_config(cmd)
        elif commit:
            commit_config(module, comment=comment)
            conn.edit_config("end")
            if admin:
                conn.edit_config("exit")
        else:
            conn.discard_changes()

    return diff