Repository: ansible/ansible

Issue:
TypeError in ec2_remote_facts get_instance_info()
w/ https://github.com/ansible/ansible/pull/25979 in devel the following occurs on attempts to gather remote facts on fresh instances:

```
The full traceback is:
Traceback (most recent call last):
  File "/var/folders/d_/ws2shnvj1_lf5kpslt_jsllw0000gn/T/ansible_jGgoay/ansible_module_ec2_remote_facts.py", line 194, in <module>
    main()
  File "/var/folders/d_/ws2shnvj1_lf5kpslt_jsllw0000gn/T/ansible_jGgoay/ansible_module_ec2_remote_facts.py", line 190, in main
    list_ec2_instances(connection, module)
  File "/var/folders/d_/ws2shnvj1_lf5kpslt_jsllw0000gn/T/ansible_jGgoay/ansible_module_ec2_remote_facts.py", line 161, in list_ec2_instances
    instance_dict_array.append(get_instance_info(instance))
  File "/var/folders/d_/ws2shnvj1_lf5kpslt_jsllw0000gn/T/ansible_jGgoay/ansible_module_ec2_remote_facts.py", line 113, in get_instance_info
    'instance_profile': dict(instance.instance_profile),
TypeError: 'NoneType' object is not iterable

fatal: [127.0.0.1]: FAILED! => {
    "changed": false,
    "failed": true,
    "module_stderr": "Traceback (most recent call last):\n  File \"/var/folders/d_/ws2shnvj1_lf5kpslt_jsllw0000gn/T/ansible_jGgoay/ansible_module_ec2_remote_facts.py\", line 194, in <module>\n    main()\n  File \"/var/folders/d_/ws2shnvj1_lf5kpslt_jsllw0000gn/T/ansible_jGgoay/ansible_module_ec2_remote_facts.py\", line 190, in main\n    list_ec2_instances(connection, module)\n  File \"/var/folders/d_/ws2shnvj1_lf5kpslt_jsllw0000gn/T/ansible_jGgoay/ansible_module_ec2_remote_facts.py\", line 161, in list_ec2_instances\n    instance_dict_array.append(get_instance_info(instance))\n  File \"/var/folders/d_/ws2shnvj1_lf5kpslt_jsllw0000gn/T/ansible_jGgoay/ansible_module_ec2_remote_facts.py\", line 113, in get_instance_info\n    'instance_profile': dict(instance.instance_profile),\nTypeError: 'NoneType' object is not iterable\n",
    "module_stdout": "",
    "msg": "MODULE FAILURE",
    "rc": 0
}
```

##### ISSUE TYPE
 - Bug Report


##### COMPONENT NAME
ec2_remote_facts

##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
```
ansible 2.4.0 (devel 42b608db0c) last updated 2017/06/23 09:23:34 (GMT -400)
```


Discussion:

Resolving patch:
diff --git a/lib/ansible/modules/cloud/amazon/ec2_remote_facts.py b/lib/ansible/modules/cloud/amazon/ec2_remote_facts.py
--- a/lib/ansible/modules/cloud/amazon/ec2_remote_facts.py
+++ b/lib/ansible/modules/cloud/amazon/ec2_remote_facts.py
@@ -108,9 +108,11 @@ def get_instance_info(instance):
     except AttributeError:
         pass
 
+    instance_profile = dict(instance.instance_profile) if instance.instance_profile is not None else None
+
     instance_info = { 'id': instance.id,
                     'kernel': instance.kernel,
-                    'instance_profile': dict(instance.instance_profile),
+                    'instance_profile': instance_profile,
                     'root_device_type': instance.root_device_type,
                     'private_dns_name': instance.private_dns_name,
                     'public_dns_name': instance.public_dns_name,


Test patch: