Repository: ansible/ansible-modules-core

Issue:
Regression: Commit 1bd04f7 broke GCE module's instance creation
## Issue Type

Bug report (regression)
## Ansible Version

Built from git: ansible-2.1.0-0.git201601051933.095790a
ansible-modules-core commit: 33014c6db1ce757d0ffa065e6c9924ac4db1cacc
System: CentOS 7.1
Apache libcloud version: 0.19.0 (installed via pip)
## Summary

I can't create GCE instances (virtual machines) anymore. Ansible fails with the message below:

> An exception occurred during task execution. To see the full traceback, use -vvv. The error was: UnboundLocalError: local variable 'md' referenced before assignment

This issue was introduced with commit 1bd04f797e2da8da8261c067eda09777ddf23fd7 (GCE libcloud 0.15 support and code cleanup) at [line 344](https://github.com/ansible/ansible-modules-core/blob/devel/cloud/google/gce.py#L344) of file `cloud/google/gce.py`.

`md` variable seems to not be declared anywhere before read.
## Steps to reproduce

If you use GCE, here is a simple playbook that creates an instance:

```

---
# Create a GCE instance

- hosts: localhost
  connection: local
  tasks:
    - gce:
        name: ans-test-creation
        machine_type: n1-standard-1
        zone: europe-west1-d
        project_id: <PROJECTID>
        service_account_email: <SERVICE_ACCOUNT_EMAIL>
        pem_file: <KEY_FILE>
```

It will fail with:

```
PLAY ***************************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [gce] *********************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: UnboundLocalError: local variable 'md' referenced before assignment
fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "parsed": false}

PLAY RECAP *********************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1
```

Deleting the offending line seems to work for me, but I do not have an understanding of the underlying code, thus I can't say if this creates other problems.

Regression: Commit 1bd04f7 broke GCE module's instance creation
## Issue Type

Bug report (regression)
## Ansible Version

Built from git: ansible-2.1.0-0.git201601051933.095790a
ansible-modules-core commit: 33014c6db1ce757d0ffa065e6c9924ac4db1cacc
System: CentOS 7.1
Apache libcloud version: 0.19.0 (installed via pip)
## Summary

I can't create GCE instances (virtual machines) anymore. Ansible fails with the message below:

> An exception occurred during task execution. To see the full traceback, use -vvv. The error was: UnboundLocalError: local variable 'md' referenced before assignment

This issue was introduced with commit 1bd04f797e2da8da8261c067eda09777ddf23fd7 (GCE libcloud 0.15 support and code cleanup) at [line 344](https://github.com/ansible/ansible-modules-core/blob/devel/cloud/google/gce.py#L344) of file `cloud/google/gce.py`.

`md` variable seems to not be declared anywhere before read.
## Steps to reproduce

If you use GCE, here is a simple playbook that creates an instance:

```

---
# Create a GCE instance

- hosts: localhost
  connection: local
  tasks:
    - gce:
        name: ans-test-creation
        machine_type: n1-standard-1
        zone: europe-west1-d
        project_id: <PROJECTID>
        service_account_email: <SERVICE_ACCOUNT_EMAIL>
        pem_file: <KEY_FILE>
```

It will fail with:

```
PLAY ***************************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [gce] *********************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: UnboundLocalError: local variable 'md' referenced before assignment
fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "parsed": false}

PLAY RECAP *********************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1
```

Deleting the offending line seems to work for me, but I do not have an understanding of the underlying code, thus I can't say if this creates other problems.



Discussion:
cc @erjohnso

Thanks for the report @andmarios and for flagging it @bcoca.

This is definitely a bug and I'm glad you have a workaround for now. Looks like that code could use a good cleanup.

cc @erjohnso

Thanks for the report @andmarios and for flagging it @bcoca.

This is definitely a bug and I'm glad you have a workaround for now. Looks like that code could use a good cleanup.


Resolving patch:
diff --git a/cloud/google/gce.py b/cloud/google/gce.py
--- a/cloud/google/gce.py
+++ b/cloud/google/gce.py
@@ -389,13 +389,13 @@ def create_instances(module, gce, instance_names):
             except SyntaxError as e:
                 module.fail_json(msg='bad metadata syntax')
 
-    if hasattr(libcloud, '__version__') and libcloud.__version__ < '0.15':
-        items = []
-        for k, v in md.items():
-            items.append({"key": k, "value": v})
-        metadata = {'items': items}
-    else:
-        metadata = md
+        if hasattr(libcloud, '__version__') and libcloud.__version__ < '0.15':
+            items = []
+            for k, v in md.items():
+                items.append({"key": k, "value": v})
+            metadata = {'items': items}
+        else:
+            metadata = md
 
     ex_sa_perms = []
     bad_perms = []


Test patch: