Repository: ansible/ansible

Issue:
SNS_Topic 
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Bug Report


##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
SNS_Topic

##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
```
ansible 2.4.0.0 (stable-2.4 a9d81ef3db) last updated 2017/09/07 14:32:13 (GMT -400)
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/Users/username/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/username/ansible/lib/ansible
  executable location = /Users/username/ansible/bin/ansible
  python version = 3.6.2 (default, Sep  6 2017, 09:50:17) [GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)]
```

##### CONFIGURATION
<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->
None
##### OS / ENVIRONMENT
<!---
Mention the OS you are running Ansible from, and the OS you are
managing, or say “N/A” for anything that is not platform-specific.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.
-->
Mac Sierra 10.12.6
##### SUMMARY
<!--- Explain the problem briefly -->
SQS to SNS_Topic variable not handled well.
##### STEPS TO REPRODUCE
<!---
For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used.
-->
Variable handling doesn't seem to work with SNS_Topic in Ansible 2.4.0.0 however works in 2.3.0.0.

<!--- Paste example playbooks or commands between quotes below -->
```yaml
# roles/SQS/tasks/main.yml

- name:              Create SQS
  sqs_queue:
    name:             "{{ sqs_name }}_{{ profile }}"
    region:           "{{ aws_region }}"
    profile:          "{{ profile }}"
    state:            "present"
    policy:           "{{ lookup('template', '{{ sqs_policy }}' )}}"
  register: my_sqs
- name:               Set SQS ID in variable
  set_fact:
    sqs_id:           "{{ my_sqs.queue_arn }}"

- name:              Create SNS
  sns_topic:
    name:             "{{ sns_name }}_{{ profile }}"
    region:           "{{ aws_region }}"
    profile:          "{{ profile }}"
    state:            "present"
    display_name:     "{{ sns_name }}_{{ profile }}"
    policy:           "{{ lookup('template', '{{ sns_policy }}' )}}"
    delivery_policy:  "{{ lookup('template', '{{ sns_deliv_policy }}' )}}"
    subscriptions:
      - endpoint: "{{ sqs_id }}"
        protocol: "SQS"

```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
That the sqs_id would be handed over the to the SNS task for subscribing to it.
##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
```
UnboundLocalError: local variable 'sub' referenced before assignment

fatal: [localhost]: FAILED! => {
    "changed": false,
    "failed": true,
    "module_stderr": "Traceback (most recent call last):\n  File \"/var/folders/kr/pdz2r4pd43q4f4bwfsx6d_20b2sqwb/T/ansible_1a6h0hk2/ansible_module_sns_topic.py\", line 405, in <module>\n    main()\n  File \"/var/folders/kr/pdz2r4pd43q4f4bwfsx6d_20b2sqwb/T/ansible_1a6h0hk2/ansible_module_sns_topic.py\", line 392, in main\n    sns_topic.ensure_ok()\n  File \"/var/folders/kr/pdz2r4pd43q4f4bwfsx6d_20b2sqwb/T/ansible_1a6h0hk2/ansible_module_sns_topic.py\", line 312, in ensure_ok\n    self._set_topic_subs()\n  File \"/var/folders/kr/pdz2r4pd43q4f4bwfsx6d_20b2sqwb/T/ansible_1a6h0hk2/ansible_module_sns_topic.py\", line 283, in _set_topic_subs\n    self.subscriptions_added.append(sub)\nUnboundLocalError: local variable 'sub' referenced before assignment\n",
    "module_stdout": "",
    "msg": "MODULE FAILURE",
    "rc": 0
}

*********************************************************************************************************************************************************************************
task path: XXXXXXX
ok: [localhost] => {
    "ansible_facts": {
        "sqs_id": "arn:aws:sqs:us-east-1:XXXXXXXX:cloudtrail_sqs_sbx"
    },
    "changed": false,
    "failed": false
}

TASK [SNS : Create SNS] *********************************************************************************************************************************************************************************************

The full traceback is:
Traceback (most recent call last):
  File "/var/folders/kr/pdz2r4pd43q4f4bwfsx6d_20b2sqwb/T/ansible_1a6h0hk2/ansible_module_sns_topic.py", line 405, in <module>
    main()
  File "/var/folders/kr/pdz2r4pd43q4f4bwfsx6d_20b2sqwb/T/ansible_1a6h0hk2/ansible_module_sns_topic.py", line 392, in main
    sns_topic.ensure_ok()
  File "/var/folders/kr/pdz2r4pd43q4f4bwfsx6d_20b2sqwb/T/ansible_1a6h0hk2/ansible_module_sns_topic.py", line 312, in ensure_ok
    self._set_topic_subs()
  File "/var/folders/kr/pdz2r4pd43q4f4bwfsx6d_20b2sqwb/T/ansible_1a6h0hk2/ansible_module_sns_topic.py", line 283, in _set_topic_subs
    self.subscriptions_added.append(sub)
UnboundLocalError: local variable 'sub' referenced before assignment

fatal: [localhost]: FAILED! => {
    "changed": false,
    "failed": true,
    "module_stderr": "Traceback (most recent call last):\n  File \"/var/folders/kr/pdz2r4pd43q4f4bwfsx6d_20b2sqwb/T/ansible_1a6h0hk2/ansible_module_sns_topic.py\", line 405, in <module>\n    main()\n  File \"/var/folders/kr/pdz2r4pd43q4f4bwfsx6d_20b2sqwb/T/ansible_1a6h0hk2/ansible_module_sns_topic.py\", line 392, in main\n    sns_topic.ensure_ok()\n  File \"/var/folders/kr/pdz2r4pd43q4f4bwfsx6d_20b2sqwb/T/ansible_1a6h0hk2/ansible_module_sns_topic.py\", line 312, in ensure_ok\n    self._set_topic_subs()\n  File \"/var/folders/kr/pdz2r4pd43q4f4bwfsx6d_20b2sqwb/T/ansible_1a6h0hk2/ansible_module_sns_topic.py\", line 283, in _set_topic_subs\n    self.subscriptions_added.append(sub)\nUnboundLocalError: local variable 'sub' referenced before assignment\n",
    "module_stdout": "",
    "msg": "MODULE FAILURE",
    "rc": 0
}
```



Discussion:
cc @joelthompson @nand0p
[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/sns_topic.py b/lib/ansible/modules/cloud/amazon/sns_topic.py
--- a/lib/ansible/modules/cloud/amazon/sns_topic.py
+++ b/lib/ansible/modules/cloud/amazon/sns_topic.py
@@ -280,7 +280,7 @@ def _set_topic_subs(self):
         for (protocol, endpoint) in desired_subscriptions:
             if (protocol, endpoint) not in subscriptions_existing_list:
                 self.changed = True
-                self.subscriptions_added.append(sub)
+                self.subscriptions_added.append((protocol, endpoint))
                 if not self.check_mode:
                     self.connection.subscribe(self.arn_topic, protocol, endpoint)
 


Test patch: