Repository: ansible/ansible

Issue:
ec2_asg fails when using tags field
##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Bug Report

##### COMPONENT NAME
ec2_asg

##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
```
ansible 2.2.1.0 (detached HEAD 5362910000) last updated 2017/01/19 09:55:08 (GMT +000)
  lib/ansible/modules/core: (detached HEAD 7e8bb413b7) last updated 2017/01/19 09:55:18 (GMT +000)
  lib/ansible/modules/extras: (detached HEAD c501e006ea) last updated 2017/01/19 09:55:18 (GMT +000)
```

##### CONFIGURATION
n/a

##### OS / ENVIRONMENT
mac OS

##### SUMMARY
When using `tags` field in `ec2_asg`, the play fails every time with this error:

```
TypeError: 'NoneType' object is not iterable
```

##### STEPS TO REPRODUCE
Setup an ASG in AWS and associate it with an ALB target group.  

Use `ec2_asg` to update tags for the ASG.

e.g.

```yaml
  - name: Create auto scaling group
    ec2_asg:
      name: foo
      launch_config_name: "{{ lc.name }}"
      min_size: "{{ min_size }}"
      max_size: "{{ max_size }}"
      region: "{{ region }}"
      default_cooldown: "{{ asg_default_cooldown }}" #seconds
      replace_all_instances: no
      health_check_period: 500 # seconds
      health_check_type: EC2
      availability_zones: "{{ availability_zones }}"
      termination_policies: ClosestToNextInstanceHour
      tags:
        - env: "{{ env }}"
          propagate_at_launch: yes
```

##### EXPECTED RESULTS
Expect to update the tag for the ASG

##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
An error occurred.
<!--- Paste verbatim command output between quotes below -->
```
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_AeBGy6/ansible_module_ec2_asg.py", line 875, in <module>
    main()
  File "/tmp/ansible_AeBGy6/ansible_module_ec2_asg.py", line 864, in main
    create_changed, asg_properties=create_autoscaling_group(connection, module)
  File "/tmp/ansible_AeBGy6/ansible_module_ec2_asg.py", line 496, in create_autoscaling_group
    for tag in as_group.tags:
TypeError: 'NoneType' object is not iterable

fatal: [localhost]: FAILED! => {
    "changed": false, 
    "failed": true, 
    "invocation": {
        "module_name": "ec2_asg"
    }, 
    "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_AeBGy6/ansible_module_ec2_asg.py\", line 875, in <module>\n    main()\n  File \"/tmp/ansible_AeBGy6/ansible_module_ec2_asg.py\", line 864, in main\n    create_changed, asg_properties=create_autoscaling_group(connection, module)\n  File \"/tmp/ansible_AeBGy6/ansible_module_ec2_asg.py\", line 496, in create_autoscaling_group\n    for tag in as_group.tags:\nTypeError: 'NoneType' object is not iterable\n", 
    "module_stdout": "", 
    "msg": "MODULE FAILURE"
}
```



Discussion:

Resolving patch:
diff --git a/lib/ansible/modules/cloud/amazon/ec2_asg.py b/lib/ansible/modules/cloud/amazon/ec2_asg.py
--- a/lib/ansible/modules/cloud/amazon/ec2_asg.py
+++ b/lib/ansible/modules/cloud/amazon/ec2_asg.py
@@ -551,11 +551,14 @@ def create_autoscaling_group(connection, module):
                 want_tags[tag.key] = [tag.value, tag.propagate_at_launch]
 
             dead_tags = []
-            for tag in as_group.tags:
-                have_tags[tag.key] = [tag.value, tag.propagate_at_launch]
-                if tag.key not in want_tags:
-                    changed = True
-                    dead_tags.append(tag)
+            if getattr(as_group, "tags", None):
+                for tag in as_group.tags:
+                    have_tags[tag.key] = [tag.value, tag.propagate_at_launch]
+                    if tag.key not in want_tags:
+                        changed = True
+                        dead_tags.append(tag)
+            elif getattr(as_group, "tags", None) is None and asg_tags:
+                module.warn("It appears your ASG is attached to a target group. This is a boto2 bug. Tags will be added but no tags are able to be removed.")
 
             if dead_tags != []:
                 connection.delete_tags(dead_tags)


Test patch: