# Buggy code
def run(module, result):
    match = module.params["match"]
    replace = module.params["replace"]
    path = module.params["parents"]

    candidate = get_candidate(module)
    if match != "none":
        contents = module.params["config"]
        if not contents:
            contents = get_config(module)
        config = NetworkConfig(indent=1, contents=contents)
        configobjs = candidate.difference(
            config, path=path, match=match, replace=replace
        )

    else:
        configobjs = candidate.items

    if configobjs:
        commands = dumps(configobjs, "commands").split("\n")

        if module.params["lines"]:
            if module.params["before"]:
                commands[:0] = module.params["before"]

            if module.params["after"]:
                commands.extend(module.params["after"])

        result["updates"] = commands

        # send the configuration commands to the device and merge
        # them with the current running config
        if not module.check_mode:
            load_config(module, commands)
        result["changed"] = True

    if module.params["save"]:
        if not module.check_mode:
            run_commands(module, "configuration write")
        result["changed"] = True


# Traceback (ansible.module_utils.connection.ConnectionError)
user@somewhere:~/git/noc-docs/ansible/playbooks$ ansible-playbook onyx_config.yml  -vvvv
ansible-playbook 2.5.2
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/home/user/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/dist-packages/ansible
executable location = /usr/bin/ansible-playbook
python version = 2.7.14+ (default, Feb  6 2018, 19:12:18) [GCC 7.3.0]
Using /etc/ansible/ansible.cfg as config file
SSH password:
setting up inventory plugins
Parsed /home/user/git/noc-docs/ansible/hosts inventory source with ini plugin
Loading callback plugin default of type stdout, v2.0 from /usr/lib/python2.7/dist-packages/ansible/plugins/callback/default.pyc

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

PLAY [mlnx_os] **************************************************************************************************************************************************************************
META: ran handlers

TASK [change config on MLNX-OS device] **************************************************************************************************************************************************
task path: /home/user/git/noc-docs/ansible/playbooks/onyx_config.yml:7
<1.1.1.1> attempting to start connection
<1.1.1.1> using connection plugin network_cli
<1.1.1.1> local domain socket does not exist, starting it
<1.1.1.1> control socket path is /home/user/.ansible/pc/e3c129338f
<1.1.1.1> <1.1.1.1> ESTABLISH CONNECTION FOR USER: admin on PORT 22 TO 1.1.1.1
<1.1.1.1> <1.1.1.1> ssh connection done, setting terminal
<1.1.1.1> <1.1.1.1> loaded terminal plugin for network_os onyx
<1.1.1.1> <1.1.1.1> loaded cliconf plugin for network_os onyx
<1.1.1.1> <1.1.1.1> firing event: on_open_shell()
<1.1.1.1> <1.1.1.1> firing event: on_become
<1.1.1.1> <1.1.1.1> ssh connection has completed successfully
<1.1.1.1> connection to remote device started successfully
<1.1.1.1> local domain socket listeners started successfully
<1.1.1.1>
<1.1.1.1> local domain socket path is /home/user/.ansible/pc/e3c129338f
Using module file /usr/lib/python2.7/dist-packages/ansible/modules/network/onyx/onyx_config.py
<1.1.1.1> ESTABLISH LOCAL CONNECTION FOR USER: user
<1.1.1.1> EXEC /bin/sh -c 'echo ~ &amp;&amp; sleep 0'
<1.1.1.1> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /home/user/.ansible/tmp/ansible-tmp-1525264175.37-178605857687780 `" &amp;&amp; echo ansible-tmp-1525264175.37-178605857687780="` echo /home/user/.ansible/tmp/ansible-tmp-1525264175.37-178605857687780 `" ) &amp;&amp; sleep 0'
<1.1.1.1> PUT /home/user/.ansible/tmp/ansible-local-241752v_IKG/tmpv7tEf1 TO /home/user/.ansible/tmp/ansible-tmp-1525264175.37-178605857687780/onyx_config.py
<1.1.1.1> EXEC /bin/sh -c 'chmod u+x /home/user/.ansible/tmp/ansible-tmp-1525264175.37-178605857687780/ /home/user/.ansible/tmp/ansible-tmp-1525264175.37-178605857687780/onyx_config.py &amp;&amp; sleep 0'
<1.1.1.1> EXEC /bin/sh -c '/usr/bin/python /home/user/.ansible/tmp/ansible-tmp-1525264175.37-178605857687780/onyx_config.py &amp;&amp; sleep 0'
<1.1.1.1> EXEC /bin/sh -c 'rm -f -r /home/user/.ansible/tmp/ansible-tmp-1525264175.37-178605857687780/ > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_OvwwDT/ansible_module_onyx_config.py", line 229, in <module>
main()
File "/tmp/ansible_OvwwDT/ansible_module_onyx_config.py", line 223, in main
run(module, result)
File "/tmp/ansible_OvwwDT/ansible_module_onyx_config.py", line 182, in run
run_commands(module, 'configuration write')
File "/tmp/ansible_OvwwDT/ansible_modlib.zip/ansible/module_utils/network/onyx/onyx.py", line 61, in run_commands
File "/tmp/ansible_OvwwDT/ansible_modlib.zip/ansible/module_utils/connection.py", line 149, in __rpc__
ansible.module_utils.connection.ConnectionError: configuration write
% Unrecognized command "configuration".
Type "?" for help.
switch-ced562 [gr0: master] #

fatal: [1.1.1.1]: FAILED! => {
"changed": false,
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_OvwwDT/ansible_module_onyx_config.py\", line 229, in <module>\n    main()\n  File \"/tmp/ansible_OvwwDT/ansible_module_onyx_config.py\", line 223, in main\n    run(module, result)\n  File \"/tmp/ansible_OvwwDT/ansible_module_onyx_config.py\", line 182, in run\n    run_commands(module, 'configuration write')\n  File \"/tmp/ansible_OvwwDT/ansible_modlib.zip/ansible/module_utils/network/onyx/onyx.py\", line 61, in run_commands\n  File \"/tmp/ansible_OvwwDT/ansible_modlib.zip/ansible/module_utils/connection.py\", line 149, in __rpc__\nansible.module_utils.connection.ConnectionError: configuration write\r\r\n% Unrecognized command \"configuration\".\r\nType \"?\" for help.\r\n\rswitch-ced562 [gr0: master] # \n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
}
to retry, use: --limit @/home/user/git/noc-docs/ansible/playbooks/onyx_config.retry

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

# Fixed code
def run(module, result):
    match = module.params["match"]
    replace = module.params["replace"]
    path = module.params["parents"]

    candidate = get_candidate(module)
    if match != "none":
        contents = module.params["config"]
        if not contents:
            contents = get_config(module)
        config = NetworkConfig(indent=1, contents=contents)
        configobjs = candidate.difference(
            config, path=path, match=match, replace=replace
        )

    else:
        configobjs = candidate.items

    total_commands = []
    if configobjs:
        commands = dumps(configobjs, "commands").split("\n")

        if module.params["lines"]:
            if module.params["before"]:
                commands[:0] = module.params["before"]

            if module.params["after"]:
                commands.extend(module.params["after"])

        total_commands.extend(commands)
        result["updates"] = total_commands

    if module.params["save"]:
        total_commands.append("configuration write")
    if total_commands:
        result["changed"] = True
        if not module.check_mode:
            load_config(module, total_commands)