# Buggy code
def netconf_edit_config(m, xml, commit, retkwargs):
    if ":candidate" in m.server_capabilities:
        datastore = "candidate"
    else:
        datastore = "running"
    m.lock(target=datastore)
    try:
        m.discard_changes()
        config_before = m.get_config(source=datastore)
        m.edit_config(target=datastore, config=xml)
        config_after = m.get_config(source=datastore)
        changed = config_before.data_xml != config_after.data_xml
        if changed and commit:
            if ":confirmed-commit" in m.server_capabilities:
                m.commit(confirmed=True)
                m.commit()
            else:
                m.commit()
        return changed
    finally:
        m.unlock(target=datastore)


# Traceback (ncclient.operations.errors.MissingCapabilityError)
steve@gitsum:~/ansible-automation$ ansible-playbook site.yml --v
Usage: ansible-playbook playbook.yml

ansible-playbook: error: ambiguous option: --v (--vault-password-file, --verbose, --version?)
steve@gitsum:~/ansible-automation$ ansible-playbook site.yml -vvvv
Using /home/steve/ansible-automation/ansible.cfg as config file
Loading callback plugin default of type stdout, v2.0 from /usr/local/lib/python2.7/dist-packages/ansible/plugins/callback/__init__.pyc

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

PLAY [hp5900] ******************************************************************

TASK [motd : Configure MOTD banner] ********************************************
task path: /home/steve/ansible-automation/roles/motd/tasks/main.yml:2
Using module file /usr/local/lib/python2.7/dist-packages/ansible/modules/extras/network/netconf/netconf_config.py
<192.168.11.15> ESTABLISH LOCAL CONNECTION FOR USER: steve
<192.168.11.15> EXEC /bin/sh -c '/usr/bin/python &amp;&amp; sleep 0'
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_9TGtyr/ansible_module_netconf_config.py", line 221, in <module>
main()
File "/tmp/ansible_9TGtyr/ansible_module_netconf_config.py", line 210, in main
retkwargs=retkwargs,
File "/tmp/ansible_9TGtyr/ansible_module_netconf_config.py", line 135, in netconf_edit_config
m.discard_changes()
File "/usr/local/lib/python2.7/dist-packages/ncclient/manager.py", line 161, in wrapper
return self.execute(op_cls, *args, **kwds)
File "/usr/local/lib/python2.7/dist-packages/ncclient/manager.py", line 231, in execute
raise_mode=self._raise_mode).request(*args, **kwds)
File "/usr/local/lib/python2.7/dist-packages/ncclient/operations/rpc.py", line 282, in __init__
self._assert(cap)
File "/usr/local/lib/python2.7/dist-packages/ncclient/operations/rpc.py", line 356, in _assert
raise MissingCapabilityError('Server does not support [%s]' % capability)
ncclient.operations.errors.MissingCapabilityError: Server does not support [:candidate]
fatal: [192.168.11.15]: FAILED! => {
"changed": false,
"failed": true,
"invocation": {
"module_name": "netconf_config"
},
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_9TGtyr/ansible_module_netconf_config.py\", line 221, in <module>\n    main()\n  File \"/tmp/ansible_9TGtyr/ansible_module_netconf_config.py\", line 210, in main\n    retkwargs=retkwargs,\n  File \"/tmp/ansible_9TGtyr/ansible_module_netconf_config.py\", line 135, in netconf_edit_config\n    m.discard_changes()\n  File \"/usr/local/lib/python2.7/dist-packages/ncclient/manager.py\", line 161, in wrapper\n    return self.execute(op_cls, *args, **kwds)\n  File \"/usr/local/lib/python2.7/dist-packages/ncclient/manager.py\", line 231, in execute\n    raise_mode=self._raise_mode).request(*args, **kwds)\n  File \"/usr/local/lib/python2.7/dist-packages/ncclient/operations/rpc.py\", line 282, in __init__\n    self._assert(cap)\n  File \"/usr/local/lib/python2.7/dist-packages/ncclient/operations/rpc.py\", line 356, in _assert\n    raise MissingCapabilityError('Server does not support [%s]' % capability)\nncclient.operations.errors.MissingCapabilityError: Server does not support [:candidate]\n",
"module_stdout": "",
"msg": "MODULE FAILURE"
}
to retry, use: --limit @/home/steve/ansible-automation/site.retry

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

# Fixed code
def netconf_edit_config(m, xml, commit, retkwargs):
    if ":candidate" in m.server_capabilities:
        datastore = "candidate"
    else:
        datastore = "running"
    m.lock(target=datastore)
    try:
        if ":candidate" in m.server_capabilities:
            m.discard_changes()
        config_before = m.get_config(source=datastore)
        m.edit_config(target=datastore, config=xml)
        config_after = m.get_config(source=datastore)
        changed = config_before.data_xml != config_after.data_xml
        if changed and commit:
            if ":confirmed-commit" in m.server_capabilities:
                m.commit(confirmed=True)
                m.commit()
            else:
                m.commit()
        return changed
    finally:
        m.unlock(target=datastore)