# Buggy code
def apply(self):
    changed = False
    ifgroup_exists = False
    add_ports_exists = True
    remove_ports_exists = False
    results = netapp_utils.get_cserver(self.server)
    cserver = netapp_utils.setup_na_ontap_zapi(module=self.module, vserver=results)
    netapp_utils.ems_log_event("na_ontap_net_ifgrp", cserver)
    if_group_detail = self.get_if_grp()
    if if_group_detail:
        ifgroup_exists = True
        ifgrp_ports_detail = self.get_if_grp_ports()
        if self.state == "absent":
            changed = True
            if self.port:
                if self.port in ifgrp_ports_detail["ports"]:
                    remove_ports_exists = True
        elif self.state == "present":
            if self.port:
                if not ifgrp_ports_detail["ports"]:
                    add_ports_exists = False
                    changed = True
                else:
                    if self.port not in ifgrp_ports_detail["ports"]:
                        add_ports_exists = False
                        changed = True
    else:
        if self.state == "present":
            changed = True

    if changed:
        if self.module.check_mode:
            pass
        else:
            if self.state == "present":
                if not ifgroup_exists:
                    self.create_if_grp()
                    if self.port:
                        self.add_port_to_if_grp()
                else:
                    if not add_ports_exists:
                        self.add_port_to_if_grp()
            elif self.state == "absent":
                if remove_ports_exists:
                    self.remove_port_to_if_grp()
                self.delete_if_grp()

    self.module.exit_json(changed=changed)


# Traceback (netapp_lib.api.zapi.zapi.NaApiError)
ansible-playbook -i netapp-inventory config.yml -vvv
ansible-playbook 2.6.1
config file = None
configured module search path = [u'/home/fgzx6022/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /home/fgzx6022/Work/python-env/ansible2.6/local/lib/python2.7/site-packages/ansible
executable location = /home/fgzx6022/Work/python-env/ansible2.6/bin/ansible-playbook
python version = 2.7.15rc1 (default, Apr 15 2018, 21:51:34) [GCC 7.3.0]
No config file found; using defaults
Parsed /home/fgzx6022/Work/ansible-netapp/netapp-inventory inventory source with ini plugin

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

PLAY [cdot_9] ******************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************
task path: /home/fgzx6022/Work/ansible-netapp/config.yml:1
<cloud-nas-y00n-prd.cloud.620nm.net> ESTABLISH LOCAL CONNECTION FOR USER: fgzx6022
<cloud-nas-y00n-prd.cloud.620nm.net> EXEC /bin/sh -c 'echo ~fgzx6022 &amp;&amp; sleep 0'
<cloud-nas-y00n-prd.cloud.620nm.net> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908092.55-277150875000599 `" &amp;&amp; echo ansible-tmp-1531908092.55-277150875000599="` echo /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908092.55-277150875000599 `" ) &amp;&amp; sleep 0'
Using module file /home/fgzx6022/Work/python-env/ansible2.6/local/lib/python2.7/site-packages/ansible/modules/system/setup.py
<cloud-nas-y00n-prd.cloud.620nm.net> PUT /home/fgzx6022/.ansible/tmp/ansible-local-28921IRjFWh/tmp9gDqI_ TO /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908092.55-277150875000599/setup.py
<cloud-nas-y00n-prd.cloud.620nm.net> EXEC /bin/sh -c 'chmod u+x /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908092.55-277150875000599/ /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908092.55-277150875000599/setup.py &amp;&amp; sleep 0'
<cloud-nas-y00n-prd.cloud.620nm.net> EXEC /bin/sh -c '/usr/bin/python /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908092.55-277150875000599/setup.py &amp;&amp; sleep 0'
<cloud-nas-y00n-prd.cloud.620nm.net> EXEC /bin/sh -c 'rm -f -r /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908092.55-277150875000599/ > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
ok: [cloud-nas-y00n-prd.cloud.620nm.net]
META: ran handlers

TASK [network : create LACP Data interface group] ******************************************************************************************
task path: /home/fgzx6022/Work/ansible-netapp/roles/network/tasks/main.yml:1
<cloud-nas-y00n-prd.cloud.620nm.net> ESTABLISH LOCAL CONNECTION FOR USER: fgzx6022
<cloud-nas-y00n-prd.cloud.620nm.net> EXEC /bin/sh -c 'echo ~fgzx6022 &amp;&amp; sleep 0'
<cloud-nas-y00n-prd.cloud.620nm.net> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908094.16-128445494609484 `" &amp;&amp; echo ansible-tmp-1531908094.16-128445494609484="` echo /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908094.16-128445494609484 `" ) &amp;&amp; sleep 0'
Using module file /home/fgzx6022/Work/python-env/ansible2.6/local/lib/python2.7/site-packages/ansible/modules/storage/netapp/na_ontap_net_ifgrp.py
<cloud-nas-y00n-prd.cloud.620nm.net> PUT /home/fgzx6022/.ansible/tmp/ansible-local-28921IRjFWh/tmpqfg6da TO /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908094.16-128445494609484/na_ontap_net_ifgrp.py
<cloud-nas-y00n-prd.cloud.620nm.net> EXEC /bin/sh -c 'chmod u+x /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908094.16-128445494609484/ /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908094.16-128445494609484/na_ontap_net_ifgrp.py &amp;&amp; sleep 0'
<cloud-nas-y00n-prd.cloud.620nm.net> EXEC /bin/sh -c '/usr/bin/python /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908094.16-128445494609484/na_ontap_net_ifgrp.py &amp;&amp; sleep 0'
<cloud-nas-y00n-prd.cloud.620nm.net> EXEC /bin/sh -c 'rm -f -r /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908094.16-128445494609484/ > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
ok: [cloud-nas-y00n-prd.cloud.620nm.net] => (item=[u'cloud-nas-y01n-prd', u'e0c']) => {
"changed": false,
"invocation": {
"module_args": {
"distribution_function": "ip",
"hostname": "cloud-nas-y00n-prd.cloud.620nm.net",
"https": false,
"mode": "multimode_lacp",
"name": "a0a",
"node": "cloud-nas-y01n-prd",
"password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"port": "e0c",
"state": "present",
"username": "admin"
}
},
"item": [
"cloud-nas-y01n-prd",
"e0c"
]
}
<cloud-nas-y00n-prd.cloud.620nm.net> EXEC /bin/sh -c 'echo ~fgzx6022 &amp;&amp; sleep 0'
<cloud-nas-y00n-prd.cloud.620nm.net> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908095.44-221329292655805 `" &amp;&amp; echo ansible-tmp-1531908095.44-221329292655805="` echo /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908095.44-221329292655805 `" ) &amp;&amp; sleep 0'
Using module file /home/fgzx6022/Work/python-env/ansible2.6/local/lib/python2.7/site-packages/ansible/modules/storage/netapp/na_ontap_net_ifgrp.py
<cloud-nas-y00n-prd.cloud.620nm.net> PUT /home/fgzx6022/.ansible/tmp/ansible-local-28921IRjFWh/tmpgOlad4 TO /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908095.44-221329292655805/na_ontap_net_ifgrp.py
<cloud-nas-y00n-prd.cloud.620nm.net> EXEC /bin/sh -c 'chmod u+x /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908095.44-221329292655805/ /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908095.44-221329292655805/na_ontap_net_ifgrp.py &amp;&amp; sleep 0'
<cloud-nas-y00n-prd.cloud.620nm.net> EXEC /bin/sh -c '/usr/bin/python /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908095.44-221329292655805/na_ontap_net_ifgrp.py &amp;&amp; sleep 0'
<cloud-nas-y00n-prd.cloud.620nm.net> EXEC /bin/sh -c 'rm -f -r /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908095.44-221329292655805/ > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
ok: [cloud-nas-y00n-prd.cloud.620nm.net] => (item=[u'cloud-nas-y01n-prd', u'e0d']) => {
"changed": false,
"invocation": {
"module_args": {
"distribution_function": "ip",
"hostname": "cloud-nas-y00n-prd.cloud.620nm.net",
"https": false,
"mode": "multimode_lacp",
"name": "a0a",
"node": "cloud-nas-y01n-prd",
"password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"port": "e0d",
"state": "present",
"username": "admin"
}
},
"item": [
"cloud-nas-y01n-prd",
"e0d"
]
}
<cloud-nas-y00n-prd.cloud.620nm.net> EXEC /bin/sh -c 'echo ~fgzx6022 &amp;&amp; sleep 0'
<cloud-nas-y00n-prd.cloud.620nm.net> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908096.45-128927256535041 `" &amp;&amp; echo ansible-tmp-1531908096.45-128927256535041="` echo /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908096.45-128927256535041 `" ) &amp;&amp; sleep 0'
Using module file /home/fgzx6022/Work/python-env/ansible2.6/local/lib/python2.7/site-packages/ansible/modules/storage/netapp/na_ontap_net_ifgrp.py
<cloud-nas-y00n-prd.cloud.620nm.net> PUT /home/fgzx6022/.ansible/tmp/ansible-local-28921IRjFWh/tmpA1qXmY TO /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908096.45-128927256535041/na_ontap_net_ifgrp.py
<cloud-nas-y00n-prd.cloud.620nm.net> EXEC /bin/sh -c 'chmod u+x /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908096.45-128927256535041/ /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908096.45-128927256535041/na_ontap_net_ifgrp.py &amp;&amp; sleep 0'
<cloud-nas-y00n-prd.cloud.620nm.net> EXEC /bin/sh -c '/usr/bin/python /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908096.45-128927256535041/na_ontap_net_ifgrp.py &amp;&amp; sleep 0'
<cloud-nas-y00n-prd.cloud.620nm.net> EXEC /bin/sh -c 'rm -f -r /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908096.45-128927256535041/ > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_VD77M_/ansible_module_na_ontap_net_ifgrp.py", line 324, in <module>
main()
File "/tmp/ansible_VD77M_/ansible_module_na_ontap_net_ifgrp.py", line 320, in main
obj.apply()
File "/tmp/ansible_VD77M_/ansible_module_na_ontap_net_ifgrp.py", line 276, in apply
ifgrp_ports_detail = self.get_if_grp_ports()
File "/tmp/ansible_VD77M_/ansible_module_na_ontap_net_ifgrp.py", line 185, in get_if_grp_ports
result = self.server.invoke_successfully(if_group_iter, True)
File "/usr/local/lib/python2.7/dist-packages/netapp_lib/api/zapi/zapi.py", line 294, in invoke_successfully
raise NaApiError(code, msg)
netapp_lib.api.zapi.zapi.NaApiError: NetApp API failed. Reason - 15661:entry doesn't exist

failed: [cloud-nas-y00n-prd.cloud.620nm.net] (item=[u'cloud-nas-y02n-prd', u'e0c']) => {
"changed": false,
"item": [
"cloud-nas-y02n-prd",
"e0c"
],
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_VD77M_/ansible_module_na_ontap_net_ifgrp.py\", line 324, in <module>\n    main()\n  File \"/tmp/ansible_VD77M_/ansible_module_na_ontap_net_ifgrp.py\", line 320, in main\n    obj.apply()\n  File \"/tmp/ansible_VD77M_/ansible_module_na_ontap_net_ifgrp.py\", line 276, in apply\n    ifgrp_ports_detail = self.get_if_grp_ports()\n  File \"/tmp/ansible_VD77M_/ansible_module_na_ontap_net_ifgrp.py\", line 185, in get_if_grp_ports\n    result = self.server.invoke_successfully(if_group_iter, True)\n  File \"/usr/local/lib/python2.7/dist-packages/netapp_lib/api/zapi/zapi.py\", line 294, in invoke_successfully\n    raise NaApiError(code, msg)\nnetapp_lib.api.zapi.zapi.NaApiError: NetApp API failed. Reason - 15661:entry doesn't exist\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
}
<cloud-nas-y00n-prd.cloud.620nm.net> EXEC /bin/sh -c 'echo ~fgzx6022 &amp;&amp; sleep 0'
<cloud-nas-y00n-prd.cloud.620nm.net> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908097.39-119102551277484 `" &amp;&amp; echo ansible-tmp-1531908097.39-119102551277484="` echo /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908097.39-119102551277484 `" ) &amp;&amp; sleep 0'
Using module file /home/fgzx6022/Work/python-env/ansible2.6/local/lib/python2.7/site-packages/ansible/modules/storage/netapp/na_ontap_net_ifgrp.py
<cloud-nas-y00n-prd.cloud.620nm.net> PUT /home/fgzx6022/.ansible/tmp/ansible-local-28921IRjFWh/tmpvmkBzP TO /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908097.39-119102551277484/na_ontap_net_ifgrp.py
<cloud-nas-y00n-prd.cloud.620nm.net> EXEC /bin/sh -c 'chmod u+x /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908097.39-119102551277484/ /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908097.39-119102551277484/na_ontap_net_ifgrp.py &amp;&amp; sleep 0'
<cloud-nas-y00n-prd.cloud.620nm.net> EXEC /bin/sh -c '/usr/bin/python /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908097.39-119102551277484/na_ontap_net_ifgrp.py &amp;&amp; sleep 0'
<cloud-nas-y00n-prd.cloud.620nm.net> EXEC /bin/sh -c 'rm -f -r /home/fgzx6022/.ansible/tmp/ansible-tmp-1531908097.39-119102551277484/ > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_5Y2eJR/ansible_module_na_ontap_net_ifgrp.py", line 324, in <module>
main()
File "/tmp/ansible_5Y2eJR/ansible_module_na_ontap_net_ifgrp.py", line 320, in main
obj.apply()
File "/tmp/ansible_5Y2eJR/ansible_module_na_ontap_net_ifgrp.py", line 276, in apply
ifgrp_ports_detail = self.get_if_grp_ports()
File "/tmp/ansible_5Y2eJR/ansible_module_na_ontap_net_ifgrp.py", line 185, in get_if_grp_ports
result = self.server.invoke_successfully(if_group_iter, True)
File "/usr/local/lib/python2.7/dist-packages/netapp_lib/api/zapi/zapi.py", line 294, in invoke_successfully
raise NaApiError(code, msg)
netapp_lib.api.zapi.zapi.NaApiError: NetApp API failed. Reason - 15661:entry doesn't exist

failed: [cloud-nas-y00n-prd.cloud.620nm.net] (item=[u'cloud-nas-y02n-prd', u'e0d']) => {
"changed": false,
"item": [
"cloud-nas-y02n-prd",
"e0d"
],
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_5Y2eJR/ansible_module_na_ontap_net_ifgrp.py\", line 324, in <module>\n    main()\n  File \"/tmp/ansible_5Y2eJR/ansible_module_na_ontap_net_ifgrp.py\", line 320, in main\n    obj.apply()\n  File \"/tmp/ansible_5Y2eJR/ansible_module_na_ontap_net_ifgrp.py\", line 276, in apply\n    ifgrp_ports_detail = self.get_if_grp_ports()\n  File \"/tmp/ansible_5Y2eJR/ansible_module_na_ontap_net_ifgrp.py\", line 185, in get_if_grp_ports\n    result = self.server.invoke_successfully(if_group_iter, True)\n  File \"/usr/local/lib/python2.7/dist-packages/netapp_lib/api/zapi/zapi.py\", line 294, in invoke_successfully\n    raise NaApiError(code, msg)\nnetapp_lib.api.zapi.zapi.NaApiError: NetApp API failed. Reason - 15661:entry doesn't exist\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
}
to retry, use: --limit @/home/fgzx6022/Work/ansible-netapp/config.retry

PLAY RECAP *********************************************************************************************************************************
cloud-nas-y00n-prd.cloud.620nm.net : ok=1    changed=0    unreachable=0    failed=1

# Fixed code
def apply(self):
    self.autosupport_log()
    current = self.get_if_grp()
    cd_action = self.na_helper.get_cd_action(current, self.parameters)
    if current and self.parameters["state"] == "present":
        current_ports = self.get_if_grp_ports()
        modify = self.na_helper.get_modified_attributes(current_ports, self.parameters)
    if self.na_helper.changed:
        if self.module.check_mode:
            pass
        else:
            if cd_action == "create":
                self.create_if_grp()
            elif cd_action == "delete":
                self.delete_if_grp()
            elif modify:
                self.modify_ports(current_ports["ports"])
    self.module.exit_json(changed=self.na_helper.changed)