# Buggy code
def _wait(timeout, cloud, server, action, module, sdk):
    """Wait for the server to reach the desired state for the given action."""

    for count in sdk.utils._iterate_timeout(
        timeout, "Timeout waiting for server to complete %s" % action
    ):
        try:
            server = cloud.get_server(server.id)
        except Exception:
            continue

        if server.status == _action_map[action]:
            return

        if server.status == "ERROR":
            module.fail_json(
                msg="Server reached ERROR state while attempting to %s" % action
            )


# Traceback (AttributeError)
ansible-playbook 2.6.1
config file = /home/korthalsp/test/ansible/os_server_action/ansible.cfg
configured module search path = [u'/home/korthalsp/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible-playbook
python version = 2.7.15 (default, May 16 2018, 17:50:09) [GCC 8.1.1 20180502 (Red Hat 8.1.1-1)]
Using /home/korthalsp/test/ansible/os_server_action/ansible.cfg as config file
setting up inventory plugins
Loading callback plugin default of type stdout, v2.0 from /usr/lib/python2.7/site-packages/ansible/plugins/callback/default.pyc

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

PLAY [fail os_server_action] ********************************************************************************************************************************************
META: ran handlers

TASK [os_server_action] *************************************************************************************************************************************************
task path: /home/korthalsp/test/ansible/os_server_action/playbook.yml:11
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: korthalsp
<127.0.0.1> EXEC /bin/sh -c 'echo ~korthalsp &amp;&amp; sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 &amp;&amp; mkdir -p "` echo /home/korthalsp/.ansible/tmp/ansible-tmp-1530865901.17-78642021245336 `" &amp;&amp; echo ansible-tmp-1530865901.17-78642021245336="` echo /home/korthalsp/.ansible/tmp/ansible-tmp-1530865901.17-78642021245336 `" ) &amp;&amp; sleep 0'
Using module file /usr/lib/python2.7/site-packages/ansible/modules/cloud/openstack/os_server_action.py
<127.0.0.1> PUT /home/korthalsp/.ansible/tmp/ansible-local-14330UL3H87/tmpmdhByf TO /home/korthalsp/.ansible/tmp/ansible-tmp-1530865901.17-78642021245336/os_server_action.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /home/korthalsp/.ansible/tmp/ansible-tmp-1530865901.17-78642021245336/ /home/korthalsp/.ansible/tmp/ansible-tmp-1530865901.17-78642021245336/os_server_action.py &amp;&amp; sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python2 /home/korthalsp/.ansible/tmp/ansible-tmp-1530865901.17-78642021245336/os_server_action.py &amp;&amp; sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/korthalsp/.ansible/tmp/ansible-tmp-1530865901.17-78642021245336/ > /dev/null 2>&amp;1 &amp;&amp; sleep 0'
The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_I4wQZ3/ansible_module_os_server_action.py", line 250, in <module>
main()
File "/tmp/ansible_I4wQZ3/ansible_module_os_server_action.py", line 242, in main
_wait(timeout, cloud, server, action, module, sdk)
File "/tmp/ansible_I4wQZ3/ansible_module_os_server_action.py", line 97, in _wait
for count in sdk.utils._iterate_timeout(
AttributeError: 'module' object has no attribute '_iterate_timeout'
fatal: [localhost]: FAILED! => {
"changed": false,
"module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_I4wQZ3/ansible_module_os_server_action.py\", line 250, in <module>\n    main()\n  File \"/tmp/ansible_I4wQZ3/ansible_module_os_server_action.py\", line 242, in main\n    _wait(timeout, cloud, server, action, module, sdk)\n  File \"/tmp/ansible_I4wQZ3/ansible_module_os_server_action.py\", line 97, in _wait\n    for count in sdk.utils._iterate_timeout(\nAttributeError: 'module' object has no attribute '_iterate_timeout'\n",
"module_stdout": "",
"msg": "MODULE FAILURE",
"rc": 1
}
to retry, use: --limit @/home/korthalsp/test/ansible/os_server_action/playbook.retry

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

# Fixed code
def _wait(timeout, cloud, server, action, module, sdk):
    """Wait for the server to reach the desired state for the given action."""

    for count in sdk.utils.iterate_timeout(
        timeout, "Timeout waiting for server to complete %s" % action
    ):
        try:
            server = cloud.get_server(server.id)
        except Exception:
            continue

        if server.status == _action_map[action]:
            return

        if server.status == "ERROR":
            module.fail_json(
                msg="Server reached ERROR state while attempting to %s" % action
            )