Repository: ansible/ansible

Issue:
ZeroDivisionError in ec2_elb_facts module
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Bug Report


##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
module ec2_elb_facts

##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
```
ansible 2.2.1.0
  config file = /kraken/ansible.cfg
  configured module search path = Default w/o overrides
```

##### CONFIGURATION
<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->

##### OS / ENVIRONMENT
<!---
Mention the OS you are running Ansible from, and the OS you are
managing, or say “N/A” for anything that is not platform-specific.
-->
N/A

##### SUMMARY
<!--- Explain the problem briefly -->
When querying ec2_elb_facts the module threw a traceback from a ZeroDivisionError.

##### 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: Wait for ELBs to be deleted
  action:
    module: ec2_elb_facts
    region: "{{ kraken_config.providerConfig.region }}"
    aws_access_key: "{{ kraken_config.providerConfig.authentication.accessKey or omit}}"
    aws_secret_key: "{{ kraken_config.providerConfig.authentication.accessSecret or omit }}"
    profile: "{{ kraken_config.providerConfig.authentication.credentialsProfile or omit }}"
  register: elb_facts
  vars:
    vpc_lookup: "elbs[?vpc_id=='{{ vpcid }}']"
  when: kraken_action == 'down' and kraken_config.provider == 'aws'
  until: (elb_facts is none) or (elb_facts|json_query(vpc_lookup) is none) or (elb_facts|json_query(vpc_lookup)|length <= 1)
  retries: 120
  delay: 5
```

This appears to happen when both `instances_inservice_count` and `instances_outofservice_count` are both 0 on line 195.

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
Loop until there's 1 or fewer elbs

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
ZeroDivisionError

<!--- Paste verbatim command output between quotes below -->
```
Traceback (most recent call last):
  File "/tmp/ansible_mX7IN8/ansible_module_ec2_elb_facts.py", line 248, in <module>
    main()
  File "/tmp/ansible_mX7IN8/ansible_module_ec2_elb_facts.py", line 240, in main
    elbs=elb_information.list_elbs())
  File "/tmp/ansible_mX7IN8/ansible_module_ec2_elb_facts.py", line 215, in list_elbs
    return list(map(self._get_elb_info, elb_array))
  File "/tmp/ansible_mX7IN8/ansible_module_ec2_elb_facts.py", line 195, in _get_elb_info
    float(elb_info['instances_outofservice_count']))*100
ZeroDivisionError: float division by zero
```



Discussion:
cc @mjschultz
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->

Resolving patch:
diff --git a/lib/ansible/modules/cloud/amazon/ec2_elb_facts.py b/lib/ansible/modules/cloud/amazon/ec2_elb_facts.py
--- a/lib/ansible/modules/cloud/amazon/ec2_elb_facts.py
+++ b/lib/ansible/modules/cloud/amazon/ec2_elb_facts.py
@@ -196,9 +196,13 @@ def _get_elb_info(self, elb):
             elb_info['instances_inservice_count'] = len(elb_info['instances_inservice'])
             elb_info['instances_outofservice'] = [inst.instance_id for inst in instance_health if inst.state == 'OutOfService']
             elb_info['instances_outofservice_count'] = len(elb_info['instances_outofservice'])
-            elb_info['instances_inservice_percent'] = float(elb_info['instances_inservice_count'])/(
-                float(elb_info['instances_inservice_count']) +
-                float(elb_info['instances_outofservice_count']))*100
+            try:
+                elb_info['instances_inservice_percent'] = (
+                    float(elb_info['instances_inservice_count']) /
+                    float(elb_info['instances_inservice_count'] + elb_info['instances_outofservice_count'])
+                ) * 100.
+            except ZeroDivisionError:
+                elb_info['instances_inservice_percent'] = 0.
         return elb_info
 
 


Test patch: