Repository: ansible/ansible

Issue:
elb_target_group UnboundLocalError: local variable 'new_target_group' referenced before assignment
<!---
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 -->
cloud/amazon/elb_target_group

##### ANSIBLE VERSION
<!--- Paste verbatim output from "ansible --version" between quotes below -->
```
ansible 2.4.0.0
  config file = None
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python2.7/dist-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609]
```

##### CONFIGURATION
<!---
If using Ansible 2.4 or above, paste the results of "ansible-config dump --only-changed"

Otherwise, mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).

-->
Nothing changed
##### 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.
-->
Running from docker container with:
```
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.1 LTS"
NAME="Ubuntu"
VERSION="16.04.1 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.1 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial
```
##### SUMMARY
<!--- Explain the problem briefly -->
Trying to create an ELB Target group, if the target does not exist the execution fails to me.

##### 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.
-->

<!--- Paste example playbooks or commands between quotes below -->
```yaml
---
- hosts: localhost
  gather_facts: false
  tasks:
    - name: "Create target group"
      elb_target_group:
        name: "test-elb"
        protocol: http
        port: 8081
        vpc_id: "vpc-8dfff2f4"
        successful_response_codes: "200"
        state: present
        deregistration_delay_timeout: 60
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
The ELB target goupt is created as expected
##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
Ansible fails with error.
<!--- Paste verbatim command output between quotes below -->
```
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1507230053.3-234723047541257/ /root/.ansible/tmp/ansible-tmp-1507230053.3-234723047541257/elb_target_group.py && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-tmp-1507230053.3-234723047541257/elb_target_group.py; rm -rf "/root/.ansible/tmp/ansible-tmp-1507230053.3-234723047541257/" > /dev/null 2>&1 && sleep 0'
The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_lpZ57g/ansible_module_elb_target_group.py", line 673, in <module>
    main()
  File "/tmp/ansible_lpZ57g/ansible_module_elb_target_group.py", line 668, in main
    create_or_update_target_group(connection, module)
  File "/tmp/ansible_lpZ57g/ansible_module_elb_target_group.py", line 563, in create_or_update_target_group
    if new_target_group:
UnboundLocalError: local variable 'new_target_group' referenced before assignment

fatal: [localhost]: FAILED! => {
    "changed": false,
    "failed": true,
    "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_lpZ57g/ansible_module_elb_target_group.py\", line 673, in <module>\n    main()\n  File \"/tmp/ansible_lpZ57g/ansible_module_elb_target_group.py\", line 668, in main\n    create_or_update_target_group(connection, module)\n  File \"/tmp/ansible_lpZ57g/ansible_module_elb_target_group.py\", line 563, in create_or_update_target_group\n    if new_target_group:\nUnboundLocalError: local variable 'new_target_group' referenced before assignment\n",
    "module_stdout": "Aqui!\n",
    "msg": "MODULE FAILURE",
    "rc": 0
}
```



Discussion:

Resolving patch:
diff --git a/lib/ansible/modules/cloud/amazon/elb_target_group.py b/lib/ansible/modules/cloud/amazon/elb_target_group.py
--- a/lib/ansible/modules/cloud/amazon/elb_target_group.py
+++ b/lib/ansible/modules/cloud/amazon/elb_target_group.py
@@ -339,6 +339,7 @@ def wait_for_status(connection, module, target_group_arn, targets, status):
 def create_or_update_target_group(connection, module):
 
     changed = False
+    new_target_group = False
     params = dict()
     params['Name'] = module.params.get("name")
     params['Protocol'] = module.params.get("protocol").upper()


Test patch: