Repository: ansible/ansible

Issue:
ec2_vpc_vgw_facts module throws an error when listing VPN Gateway which has no tags.


##### ISSUE TYPE
<!--- Pick one below and delete the rest -->
 - Bug Report

##### COMPONENT NAME

ec2_vpc_vgw_facts


##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes below -->
```
ansible 2.4.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/centos-dev/.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.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
```

##### CONFIGURATION
DEFAULT_ROLES_PATH(/etc/ansible/ansible.cfg) = [u'/etc/ansible/roles', u'/usr/share/ansible/roles']

##### OS / ENVIRONMENT
N/A

##### SUMMARY
I have a virtual private gateway in my AWS account. This gateway has no tags. While collecting information about this, module invocation failed with an error "An exception occurred during task execution. To see the full traceback, use -vvv. The error was: KeyError: 'Tags'".
Also, there is small error in documentation on line number 26 of the module. The link describing APIReference should be corrected to 'https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeVpnGateways.html'

##### STEPS TO REPRODUCE
1. Have at least one virtual private gateway in AWS region without any tags.
2. Playbook call is as below:
```
- hosts: localhost
  tasks:
    - name: Get virtual private gateway facts
      ec2_vpc_vgw_facts:
        region: ap-south-1
      register: vgw_facts
```
3. Run playbook with required access/secret keys as environment variables.

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
As Vpn Gateway exists, module should not fail and should collect facts for existing gateways.

##### ACTUAL RESULTS
<!--- Paste verbatim command output between quotes below -->
```
The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_c6AWJi/ansible_module_ec2_vpc_vgw_facts.py", line 174, in <module>
    main()
  File "/tmp/ansible_c6AWJi/ansible_module_ec2_vpc_vgw_facts.py", line 168, in main
    results = list_virtual_gateways(connection, module)
  File "/tmp/ansible_c6AWJi/ansible_module_ec2_vpc_vgw_facts.py", line 141, in list_virtual_gateways
    for vgw in all_virtual_gateways['VpnGateways']]
  File "/tmp/ansible_c6AWJi/ansible_module_ec2_vpc_vgw_facts.py", line 112, in get_virtual_gateway_info
    'Tags': virtual_gateway['Tags']}
KeyError: 'Tags'

fatal: [localhost]: FAILED! => {
    "changed": false,
    "failed": true,
    "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_c6AWJi/ansible_module_ec2_vpc_vgw_facts.py\", line 174, in <module>\n    main()\n  File \"/tmp/ansible_c6AWJi/ansible_module_ec2_vpc_vgw_facts.py\", line 168, in main\n    results = list_virtual_gateways(connection, module)\n  File \"/tmp/ansible_c6AWJi/ansible_module_ec2_vpc_vgw_facts.py\", line 141, in list_virtual_gateways\n    for vgw in all_virtual_gateways['VpnGateways']]\n  File \"/tmp/ansible_c6AWJi/ansible_module_ec2_vpc_vgw_facts.py\", line 112, in get_virtual_gateway_info\n    **'Tags': virtual_gateway['Tags']}\nKeyError: 'Tags'**\n",
    "module_stdout": "",
    "msg": "MODULE FAILURE",
    "rc": 0
}
```

ec2_vpc_vgw_facts module throws an error when listing VPN Gateway which has no tags.


##### ISSUE TYPE
<!--- Pick one below and delete the rest -->
 - Bug Report

##### COMPONENT NAME

ec2_vpc_vgw_facts


##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes below -->
```
ansible 2.4.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/centos-dev/.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.5 (default, Aug  4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
```

##### CONFIGURATION
DEFAULT_ROLES_PATH(/etc/ansible/ansible.cfg) = [u'/etc/ansible/roles', u'/usr/share/ansible/roles']

##### OS / ENVIRONMENT
N/A

##### SUMMARY
I have a virtual private gateway in my AWS account. This gateway has no tags. While collecting information about this, module invocation failed with an error "An exception occurred during task execution. To see the full traceback, use -vvv. The error was: KeyError: 'Tags'".
Also, there is small error in documentation on line number 26 of the module. The link describing APIReference should be corrected to 'https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeVpnGateways.html'

##### STEPS TO REPRODUCE
1. Have at least one virtual private gateway in AWS region without any tags.
2. Playbook call is as below:
```
- hosts: localhost
  tasks:
    - name: Get virtual private gateway facts
      ec2_vpc_vgw_facts:
        region: ap-south-1
      register: vgw_facts
```
3. Run playbook with required access/secret keys as environment variables.

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
As Vpn Gateway exists, module should not fail and should collect facts for existing gateways.

##### ACTUAL RESULTS
<!--- Paste verbatim command output between quotes below -->
```
The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_c6AWJi/ansible_module_ec2_vpc_vgw_facts.py", line 174, in <module>
    main()
  File "/tmp/ansible_c6AWJi/ansible_module_ec2_vpc_vgw_facts.py", line 168, in main
    results = list_virtual_gateways(connection, module)
  File "/tmp/ansible_c6AWJi/ansible_module_ec2_vpc_vgw_facts.py", line 141, in list_virtual_gateways
    for vgw in all_virtual_gateways['VpnGateways']]
  File "/tmp/ansible_c6AWJi/ansible_module_ec2_vpc_vgw_facts.py", line 112, in get_virtual_gateway_info
    'Tags': virtual_gateway['Tags']}
KeyError: 'Tags'

fatal: [localhost]: FAILED! => {
    "changed": false,
    "failed": true,
    "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_c6AWJi/ansible_module_ec2_vpc_vgw_facts.py\", line 174, in <module>\n    main()\n  File \"/tmp/ansible_c6AWJi/ansible_module_ec2_vpc_vgw_facts.py\", line 168, in main\n    results = list_virtual_gateways(connection, module)\n  File \"/tmp/ansible_c6AWJi/ansible_module_ec2_vpc_vgw_facts.py\", line 141, in list_virtual_gateways\n    for vgw in all_virtual_gateways['VpnGateways']]\n  File \"/tmp/ansible_c6AWJi/ansible_module_ec2_vpc_vgw_facts.py\", line 112, in get_virtual_gateway_info\n    **'Tags': virtual_gateway['Tags']}\nKeyError: 'Tags'**\n",
    "module_stdout": "",
    "msg": "MODULE FAILURE",
    "rc": 0
}
```



Discussion:
Files identified in the description:
* [lib/ansible/modules/cloud/amazon/ec2_vpc_vgw_facts.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/cloud/amazon/ec2_vpc_vgw_facts.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 @naslanidis @willthames
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
Files identified in the description:
* [lib/ansible/modules/cloud/amazon/ec2_vpc_vgw_facts.py](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/cloud/amazon/ec2_vpc_vgw_facts.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 @naslanidis @willthames
[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_vpc_vgw_facts.py b/lib/ansible/modules/cloud/amazon/ec2_vpc_vgw_facts.py
--- a/lib/ansible/modules/cloud/amazon/ec2_vpc_vgw_facts.py
+++ b/lib/ansible/modules/cloud/amazon/ec2_vpc_vgw_facts.py
@@ -110,7 +110,7 @@ def get_virtual_gateway_info(virtual_gateway):
                             'State': virtual_gateway['State'],
                             'Type': virtual_gateway['Type'],
                             'VpcAttachments': virtual_gateway['VpcAttachments'],
-                            'Tags': virtual_gateway['Tags']}
+                            'Tags': virtual_gateway.get('Tags', [])}
     return virtual_gateway_info
 
 


Test patch: