Repository: ansible/ansible-modules-core

Issue:
KeyError: u'available' with elasticache
##### Issue Type:

Bug Report
##### Ansible Version:

$ ansible --version
ansible 1.8.2
  configured module search path = None
##### Environment:

Running from OS X and managed host is AWS cloud intances.
##### Summary:

When I try to run playbook with elasticache task I get following error:

``` python
Traceback (most recent call last):
  File "ansible-tmp-1421210891.68-4727581320551/elasticache", line 2305, in <module>
    main()
  File "ansible-tmp-1421210891.68-4727581320551/elasticache", line 532, in main
    elasticache_manager.ensure_present()
  File "ansible-tmp-1421210891.68-4727581320551/elasticache", line 189, in ensure_present
    self.sync()
  File "ansible-tmp-1421210891.68-4727581320551/elasticache", line 292, in sync
    self.modify()
  File "ansible-tmp-1421210891.68-4727581320551/elasticache", line 313, in modify
    self._wait_for_status('available')
  File ansible-tmp-1421210891.68-4727581320551/elasticache", line 364, in _wait_for_status
    if status_map[self.status] != awaited_status:
KeyError: u'available'
```
##### Steps To Reproduce:

I have following task in two different playbooks and in one it works but in another I get error below. 

``` yaml
   - name: Create Elasticache instances
      local_action:
        module: elasticache
        name: "cache-{{env}}"
        state: present
        engine: redis
        cache_engine_version: 2.8.6
        node_type: "{{elasticache_instance_type}}"
        num_nodes: 1
        region: "{{aws_region}}"
        cache_security_groups: []
        wait: yes
      register: elasticache_info
```
##### Expected Results:

```
ok: [localhost -> 127.0.0.1]
```
##### Actual Results:

```
failed: [localhost -> 127.0.0.1] => {"failed": true, "parsed": false}
Traceback (most recent call last):
  File .ansible/tmp/ansible-tmp-1421210891.68-4727581320551/elasticache", line 2305, in <module>
    main()
  File .ansible/tmp/ansible-tmp-1421210891.68-4727581320551/elasticache", line 532, in main
    elasticache_manager.ensure_present()
  File ".ansible/tmp/ansible-tmp-1421210891.68-4727581320551/elasticache", line 189, in     ensure_present
    self.sync()
   File ".ansible/tmp/ansible-tmp-1421210891.68-4727581320551/elasticache", line 292, in sync
     self.modify()
   File ".ansible/tmp/ansible-tmp-1421210891.68-4727581320551/elasticache", line 313, in modify
    self._wait_for_status('available')
   File ".ansible/tmp/ansible-tmp-1421210891.68-4727581320551/elasticache", line 364, in _wait_for_status
     if status_map[self.status] != awaited_status:
KeyError: u'available'


FATAL: all hosts have already failed -- aborting
```



Discussion:
This seems to happen when ansible tries to modify the instance. My Elasticache instance had security group assigned which wasn't on playbook and after I added same group to playbook it seems to work.


Resolving patch:
diff --git a/cloud/amazon/elasticache.py b/cloud/amazon/elasticache.py
--- a/cloud/amazon/elasticache.py
+++ b/cloud/amazon/elasticache.py
@@ -357,7 +357,9 @@ def _wait_for_status(self, awaited_status):
             'modifying': 'available',
             'deleting': 'gone'
         }
-
+        if self.status == awaited_status:
+            # No need to wait, we're already done
+            return
         if status_map[self.status] != awaited_status:
             msg = "Invalid awaited status. '%s' cannot transition to '%s'"
             self.module.fail_json(msg=msg % (self.status, awaited_status))


Test patch: