Repository: ansible/ansible

Issue:
os_server_action crush when wait=True
<!---
Verify first that your issue/request is not already reported on GitHub.
THIS FORM WILL BE READ BY A MACHINE, COMPLETE ALL SECTIONS AS DESCRIBED.
Also test if the latest release, and devel branch are affected too.
ALWAYS add information AFTER (OUTSIDE) these html comments.
Otherwise it may end up being automatically closed by our bot. -->

##### SUMMARY
When trying to use os_server_action module error appear.
Issue was reproduced in:
- ansible 2.6.0
- ansible 2.6.1
Issue was not present in:
- ansible 2.5.5 and below

##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
<!--- Insert, BELOW THIS COMMENT, the name of the module, plugin, task or feature.
Do not include extra details here, e.g. "vyos_command" not "the network module vyos_command" or the full path-->
os_server_action

##### ANSIBLE VERSION
<!--- Paste, BELOW THIS COMMENT, verbatim output from "ansible --version" between quotes below -->
```
ansible 2.6.1
  config file = /etc/ansible/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
  python version = 2.7.15 (default, May 16 2018, 17:50:09) [GCC 8.1.1 20180502 (Red Hat 8.1.1-1)]
```

##### CONFIGURATION
<!--- If using Ansible 2.4 or above, paste, BELOW THIS COMMENT, the results of "ansible-config dump --only-changed"
Otherwise, mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).-->

##### OS / ENVIRONMENT
<!--- Mention, BELOW THIS COMMENT, the OS you are running Ansible from, and the OS you are
managing, or say "N/A" for anything that is not platform-specific.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.-->
```
Name: ansible
Version: 2.6.1
Summary: Radically simple IT automation
Home-page: https://ansible.com/
Author: Ansible, Inc.
Author-email: info@ansible.com
License: GPLv3+
Location: /usr/lib/python2.7/site-packages
Requires: PyYAML, setuptools, jinja2, paramiko, cryptography
---
Name: python-openstacksdk
Version: 0.5.2
Summary: Deprecated SDK for OpenStack.  Use openstacksdk.
Home-page: http://www.openstack.org/
Author: OpenStack
Author-email: openstack-dev@lists.openstack.org
License: UNKNOWN
Location: /usr/lib/python2.7/site-packages
Requires: openstacksdk
---
Name: shade
Version: 1.28.0
Summary: Simple client library for interacting with OpenStack clouds
Home-page: http://docs.openstack.org/shade/latest
Author: OpenStack
Author-email: openstack-dev@lists.openstack.org
License: UNKNOWN
Location: /usr/lib/python2.7/site-packages
Requires: pbr, os-client-config, openstacksdk
```
##### STEPS TO REPRODUCE
<!--- For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used. -->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
---
- name: "fail os_server_action"
  hosts: localhost
  gather_facts: no
  vars:
    cloud_name: "my_cloud_profil_name"
    openstack_server_id: "00000000-0000-0000-0000-000000000000"
  tasks:
    - os_server_action:
        cloud: "{{ cloud_name }}"
        action: "rebuild"
        server: "{{ openstack_server_id }}"
        image: "image_name"
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
Openstack server should be rebuilded.

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
Module crushed.

<!--- Paste verbatim command output between quotes below -->
```
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 && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/korthalsp/.ansible/tmp/ansible-tmp-1530865901.17-78642021245336 `" && echo ansible-tmp-1530865901.17-78642021245336="` echo /home/korthalsp/.ansible/tmp/ansible-tmp-1530865901.17-78642021245336 `" ) && 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 && 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 && 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>&1 && 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   

```



Discussion:
Files identified in the description:
* [lib/ansible/modules/cloud/openstack/os_server_action.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/cloud/openstack/os_server_action.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @dagnello @emonty @juliakreger @omgjlk @rcarrillocruz @shrews @thingee
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
Same issue here when we try to shutoff a instance.
Error still persists in Ansible 2.6.2:
```
The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_SsgEzs/ansible_module_os_server_action.py", line 250, in <module>
    main()
  File "/tmp/ansible_SsgEzs/ansible_module_os_server_action.py", line 170, in main
    _wait(timeout, cloud, server, action, module, sdk)
  File "/tmp/ansible_SsgEzs/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: [somehost]: FAILED! => {
    "changed": false, 
    "failed_when_result": true, 
    "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_SsgEzs/ansible_module_os_server_action.py\", line 250, in <module>\n    main()\n  File \"/tmp/ansible_SsgEzs/ansible_module_os_server_action.py\", line 170, in main\n    _wait(timeout, cloud, server, action, module, sdk)\n  File \"/tmp/ansible_SsgEzs/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
}
```

Btw. it does not matter whether `action` of `os_server_action`  is `start` or `stop`. Seems the waiting for the `action` is broken.
I seem to have found the issue in [`lib/ansible/modules/cloud/openstack/os_server_action.py` (L93/R97; look for my comment).](https://github.com/ansible/ansible/commit/89ce826a9fb53c304238923a73667ab820711338#diff-78b6488d27c66bca465ffbe62f98ac03R97).

Looks like the migration from shade to openstacksdk requires `sdk.utils.iterate_timeout` instead of `sdk.utils._iterate_timeout`.

Resolving patch:
diff --git a/lib/ansible/modules/cloud/openstack/os_server_action.py b/lib/ansible/modules/cloud/openstack/os_server_action.py
--- a/lib/ansible/modules/cloud/openstack/os_server_action.py
+++ b/lib/ansible/modules/cloud/openstack/os_server_action.py
@@ -94,7 +94,7 @@ def _action_url(server_id):
 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(
+    for count in sdk.utils.iterate_timeout(
             timeout,
             "Timeout waiting for server to complete %s" % action):
         try:
@@ -163,9 +163,9 @@ def main():
             if not _system_state_change(action, status):
                 module.exit_json(changed=False)
 
-                cloud.compute.post(
-                    _action_url(server.id),
-                    json={'os-start': None})
+            cloud.compute.post(
+                _action_url(server.id),
+                json={'os-start': None})
             if wait:
                 _wait(timeout, cloud, server, action, module, sdk)
                 module.exit_json(changed=True)
@@ -174,9 +174,9 @@ def main():
             if not _system_state_change(action, status):
                 module.exit_json(changed=False)
 
-                cloud.compute.post(
-                    _action_url(server.id),
-                    json={'pause': None})
+            cloud.compute.post(
+                _action_url(server.id),
+                json={'pause': None})
             if wait:
                 _wait(timeout, cloud, server, action, module, sdk)
                 module.exit_json(changed=True)
@@ -185,9 +185,9 @@ def main():
             if not _system_state_change(action, status):
                 module.exit_json(changed=False)
 
-                cloud.compute.post(
-                    _action_url(server.id),
-                    json={'unpause': None})
+            cloud.compute.post(
+                _action_url(server.id),
+                json={'unpause': None})
             if wait:
                 _wait(timeout, cloud, server, action, module, sdk)
             module.exit_json(changed=True)
@@ -210,9 +210,9 @@ def main():
             if not _system_state_change(action, status):
                 module.exit_json(changed=False)
 
-                cloud.compute.post(
-                    _action_url(server.id),
-                    json={'suspend': None})
+            cloud.compute.post(
+                _action_url(server.id),
+                json={'suspend': None})
             if wait:
                 _wait(timeout, cloud, server, action, module, sdk)
             module.exit_json(changed=True)
@@ -221,9 +221,9 @@ def main():
             if not _system_state_change(action, status):
                 module.exit_json(changed=False)
 
-                cloud.compute.post(
-                    _action_url(server.id),
-                    json={'resume': None})
+            cloud.compute.post(
+                _action_url(server.id),
+                json={'resume': None})
             if wait:
                 _wait(timeout, cloud, server, action, module, sdk)
             module.exit_json(changed=True)
@@ -235,9 +235,9 @@ def main():
                 module.fail_json(msg="Image does not exist")
 
             # rebuild doesn't set a state, just do it
-                cloud.compute.post(
-                    _action_url(server.id),
-                    json={'rebuild': None})
+            cloud.compute.post(
+                _action_url(server.id),
+                json={'rebuild': None})
             if wait:
                 _wait(timeout, cloud, server, action, module, sdk)
             module.exit_json(changed=True)


Test patch: