Repository: ansible/ansible

Issue:
ec2_vpc_subnet: NoneType' object is not iterable
##### ISSUE TYPE
 - Bug Report
 - Documentation Report

##### COMPONENT NAME
ec2_vpc_subnet

##### ANSIBLE VERSION
```
ansible 2.2.1.0
  config file = 
  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).
-->
Default ansible.cfg - not modified.

##### OS / ENVIRONMENT
Mac OS Sierra 10.12.3

##### SUMMARY
Even if the documentation of ec2_vpc_subnet says that the tags key is optional, the ec2_vpc_subnet fails if no tags are provided.

##### STEPS TO REPRODUCE
Execute following tasks with proper values, without the tags key of ec2_vpc_subnet.
```yaml
- name: Create default VPC subnets.
      ec2_vpc_subnet:
        aws_access_key: "{{ aws_access_key }}"
        aws_secret_key: "{{ aws_secret_key }}"
        az: "{{ aws_az }}"
        cidr: "{{ item }}"
        region: "{{ aws_region }}"
        state: "{{ aws_state }}"
        vpc_id: "{{ ansible_ec2_vpc.vpc_id }}"
      with_items:
        - 172.31.0.0/20
        - 172.31.16.0/20
      tags: ec2_vpc_subnet
      register: ansible_ec2_vpc_subnets
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
According to http://docs.ansible.com/ansible/ec2_vpc_subnet_module.html it should just work.

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->

<!--- Paste verbatim command output between quotes below -->
```
TASK [Create default VPC subnets.] *********************************************
task path: /Users/chhaci/Documents/Magento2/provision/provision.yml:31
Using module file /usr/local/Cellar/ansible/2.2.1.0_1/libexec/lib/python2.7/site-packages/ansible/modules/extras/cloud/amazon/ec2_vpc_subnet.py
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: chhaci
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo ~/.ansible/tmp/ansible-tmp-1487775336.92-72635135088711 `" && echo ansible-tmp-1487775336.92-72635135088711="` echo ~/.ansible/tmp/ansible-tmp-1487775336.92-72635135088711 `" ) && sleep 0'
<127.0.0.1> PUT /var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/tmpAFBO2N TO /Users/chhaci/.ansible/tmp/ansible-tmp-1487775336.92-72635135088711/ec2_vpc_subnet.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /Users/chhaci/.ansible/tmp/ansible-tmp-1487775336.92-72635135088711/ /Users/chhaci/.ansible/tmp/ansible-tmp-1487775336.92-72635135088711/ec2_vpc_subnet.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/local/Cellar/ansible/2.2.1.0_1/libexec/bin/python2.7 /Users/chhaci/.ansible/tmp/ansible-tmp-1487775336.92-72635135088711/ec2_vpc_subnet.py; rm -rf "/Users/chhaci/.ansible/tmp/ansible-tmp-1487775336.92-72635135088711/" > /dev/null 2>&1 && sleep 0'
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
  File "/var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/ansible_vv7sXj/ansible_module_ec2_vpc_subnet.py", line 272, in <module>
    main()
  File "/var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/ansible_vv7sXj/ansible_module_ec2_vpc_subnet.py", line 261, in main
    check_mode=module.check_mode)
  File "/var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/ansible_vv7sXj/ansible_module_ec2_vpc_subnet.py", line 199, in ensure_subnet_present
    ensure_tags(vpc_conn, subnet.id, tags, False, check_mode)
  File "/var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/ansible_vv7sXj/ansible_module_ec2_vpc_subnet.py", line 169, in ensure_tags
    to_add = dict((k, tags[k]) for k in tags if k not in cur_tags or cur_tags[k] != tags[k])
TypeError: 'NoneType' object is not iterable

failed: [localhost] (item=172.31.0.0/20) => {
    "failed": true, 
    "invocation": {
        "module_name": "ec2_vpc_subnet"
    }, 
    "item": "172.31.0.0/20", 
    "module_stderr": "Traceback (most recent call last):\n  File \"/var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/ansible_vv7sXj/ansible_module_ec2_vpc_subnet.py\", line 272, in <module>\n    main()\n  File \"/var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/ansible_vv7sXj/ansible_module_ec2_vpc_subnet.py\", line 261, in main\n    check_mode=module.check_mode)\n  File \"/var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/ansible_vv7sXj/ansible_module_ec2_vpc_subnet.py\", line 199, in ensure_subnet_present\n    ensure_tags(vpc_conn, subnet.id, tags, False, check_mode)\n  File \"/var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/ansible_vv7sXj/ansible_module_ec2_vpc_subnet.py\", line 169, in ensure_tags\n    to_add = dict((k, tags[k]) for k in tags if k not in cur_tags or cur_tags[k] != tags[k])\nTypeError: 'NoneType' object is not iterable\n", 
    "module_stdout": "", 
    "msg": "MODULE FAILURE"
}
Using module file /usr/local/Cellar/ansible/2.2.1.0_1/libexec/lib/python2.7/site-packages/ansible/modules/extras/cloud/amazon/ec2_vpc_subnet.py
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo ~/.ansible/tmp/ansible-tmp-1487775338.13-18218926660937 `" && echo ansible-tmp-1487775338.13-18218926660937="` echo ~/.ansible/tmp/ansible-tmp-1487775338.13-18218926660937 `" ) && sleep 0'
<127.0.0.1> PUT /var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/tmpGh7EMs TO /Users/chhaci/.ansible/tmp/ansible-tmp-1487775338.13-18218926660937/ec2_vpc_subnet.py
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /Users/chhaci/.ansible/tmp/ansible-tmp-1487775338.13-18218926660937/ /Users/chhaci/.ansible/tmp/ansible-tmp-1487775338.13-18218926660937/ec2_vpc_subnet.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/local/Cellar/ansible/2.2.1.0_1/libexec/bin/python2.7 /Users/chhaci/.ansible/tmp/ansible-tmp-1487775338.13-18218926660937/ec2_vpc_subnet.py; rm -rf "/Users/chhaci/.ansible/tmp/ansible-tmp-1487775338.13-18218926660937/" > /dev/null 2>&1 && sleep 0'
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
  File "/var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/ansible_0UP5vW/ansible_module_ec2_vpc_subnet.py", line 272, in <module>
    main()
  File "/var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/ansible_0UP5vW/ansible_module_ec2_vpc_subnet.py", line 261, in main
    check_mode=module.check_mode)
  File "/var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/ansible_0UP5vW/ansible_module_ec2_vpc_subnet.py", line 199, in ensure_subnet_present
    ensure_tags(vpc_conn, subnet.id, tags, False, check_mode)
  File "/var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/ansible_0UP5vW/ansible_module_ec2_vpc_subnet.py", line 169, in ensure_tags
    to_add = dict((k, tags[k]) for k in tags if k not in cur_tags or cur_tags[k] != tags[k])
TypeError: 'NoneType' object is not iterable

failed: [localhost] (item=172.31.16.0/20) => {
    "failed": true, 
    "invocation": {
        "module_name": "ec2_vpc_subnet"
    }, 
    "item": "172.31.16.0/20", 
    "module_stderr": "Traceback (most recent call last):\n  File \"/var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/ansible_0UP5vW/ansible_module_ec2_vpc_subnet.py\", line 272, in <module>\n    main()\n  File \"/var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/ansible_0UP5vW/ansible_module_ec2_vpc_subnet.py\", line 261, in main\n    check_mode=module.check_mode)\n  File \"/var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/ansible_0UP5vW/ansible_module_ec2_vpc_subnet.py\", line 199, in ensure_subnet_present\n    ensure_tags(vpc_conn, subnet.id, tags, False, check_mode)\n  File \"/var/folders/1z/97v4r2zd2bd7_l_z29x_8n680000gn/T/ansible_0UP5vW/ansible_module_ec2_vpc_subnet.py\", line 169, in ensure_tags\n    to_add = dict((k, tags[k]) for k in tags if k not in cur_tags or cur_tags[k] != tags[k])\nTypeError: 'NoneType' object is not iterable\n", 
    "module_stdout": "", 
    "msg": "MODULE FAILURE"
}
```



Discussion:
cc @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_subnet.py b/lib/ansible/modules/cloud/amazon/ec2_vpc_subnet.py
--- a/lib/ansible/modules/cloud/amazon/ec2_vpc_subnet.py
+++ b/lib/ansible/modules/cloud/amazon/ec2_vpc_subnet.py
@@ -233,7 +233,7 @@ def main():
             az = dict(default=None, required=False),
             cidr = dict(default=None, required=True),
             state = dict(default='present', choices=['present', 'absent']),
-            tags = dict(default=None, required=False, type='dict', aliases=['resource_tags']),
+            tags = dict(default={}, required=False, type='dict', aliases=['resource_tags']),
             vpc_id = dict(default=None, required=True)
         )
     )


Test patch: