Repository: ansible/ansible

Issue:
ec2_customer_gateway returns different values depending if CGW exists already or not
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
ec2_customer_gateway

##### ANSIBLE VERSION
```
ansible 2.3.0.0
  config file = 
  configured module search path = Default w/o overrides
  python version = 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609]

```

##### CONFIGURATION
standard

##### OS / ENVIRONMENT
Ubuntu, AWS

##### SUMMARY
Creating CGW vs reporting on existing CGW returns different results.

##### STEPS TO REPRODUCE
Sample task:
```yaml
- name: Create Spoke Customer Gateway
  ec2_customer_gateway:
    bgp_asn: "{{ asn_spoke }}"
    ip_address: "{{ item.public_ip }}"
    name: cgw-spoke
    region: "{{ aws_region }}"
    aws_access_key: "{{ ec2_access_key }}"
    aws_secret_key: "{{ ec2_secret_key }}"
  with_items: "{{ eip_spoke.results }}"
  register: cgw_spoke
```


##### EXPECTED RESULTS
On the first run, a dictionary "customer_gateway" is returned. On subsequent runs, list "customer_gateway" and list "customer_gateways" are returned. This creates issues when attempting to code for create and already created scenarios.

##### ACTUAL RESULTS

Running the above task the first time returns the following result:
```
"gateway": {
                    "customer_gateway": {
                        "bgp_asn": "65000", 
                        "customer_gateway_id": "cgw-1d05de03", 
                        "ip_address": "52.27.234.211", 
                        "state": "available", 
                        "type": "ipsec.1"
                    }
}
```

Running the above task with the same information will return the following result:
```
"gateway": {
                    "customer_gateway": [
                        {
                            "bgp_asn": "65000", 
                            "customer_gateway_id": "cgw-1d05de03", 
                            "ip_address": "52.27.234.211", 
                            "state": "available", 
                            "tags": [
                                {
                                    "key": "Name", 
                                    "value": "cgw-spoke"
                                }
                            ], 
                            "type": "ipsec.1"
                        }
                    ], 
                    "customer_gateways": [
                        {
                            "bgp_asn": "65000", 
                            "customer_gateway_id": "cgw-1d05de03", 
                            "ip_address": "52.27.234.211", 
                            "state": "available", 
                            "tags": [
                                {
                                    "key": "Name", 
                                    "value": "cgw-spoke"
                                }
                            ], 
                            "type": "ipsec.1"
                        }
                    ]
}

```


Discussion:
cc @michaelbaydoun
[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_customer_gateway.py b/lib/ansible/modules/cloud/amazon/ec2_customer_gateway.py
--- a/lib/ansible/modules/cloud/amazon/ec2_customer_gateway.py
+++ b/lib/ansible/modules/cloud/amazon/ec2_customer_gateway.py
@@ -226,22 +226,20 @@ def main():
     name = module.params.get('name')
 
     existing = gw_mgr.describe_gateways(module.params['ip_address'])
-    # describe_gateways returns a key of CustomerGateways where as create_gateway returns a
-    # key of CustomerGateway. For consistency, change it here
-    existing['CustomerGateway'] = existing['CustomerGateways']
 
     results = dict(changed=False)
     if module.params['state'] == 'present':
-        if existing['CustomerGateway']:
+        if existing['CustomerGateways']:
+            existing['CustomerGateway'] = existing['CustomerGateways'][0]
             results['gateway'] = existing
-            if existing['CustomerGateway'][0]['Tags']:
-                tag_array = existing['CustomerGateway'][0]['Tags']
+            if existing['CustomerGateway']['Tags']:
+                tag_array = existing['CustomerGateway']['Tags']
                 for key, value in enumerate(tag_array):
                     if value['Key'] == 'Name':
                         current_name = value['Value']
                         if current_name != name:
                             results['name'] = gw_mgr.tag_cgw_name(
-                                results['gateway']['CustomerGateway'][0]['CustomerGatewayId'],
+                                results['gateway']['CustomerGateway']['CustomerGatewayId'],
                                 module.params['name'],
                             )
                             results['changed'] = True
@@ -258,11 +256,12 @@ def main():
             results['changed'] = True
 
     elif module.params['state'] == 'absent':
-        if existing['CustomerGateway']:
+        if existing['CustomerGateways']:
+            existing['CustomerGateway'] = existing['CustomerGateways'][0]
             results['gateway'] = existing
             if not module.check_mode:
                 results['gateway'] = gw_mgr.ensure_cgw_absent(
-                    existing['CustomerGateway'][0]['CustomerGatewayId']
+                    existing['CustomerGateway']['CustomerGatewayId']
                 )
             results['changed'] = True
 


Test patch: