From f3d248de9aa5adb2c4e69e929216d0e1cd06e86a Mon Sep 17 00:00:00 2001
From: kubilus1 <mattkubilus@gmail.com>
Date: Wed, 18 May 2016 03:26:58 -0400
Subject: [PATCH] Check to see existence of same named/location system before
 creation. (#2150)

---
 lib/ansible/modules/extras/cloud/softlayer/sl_vm.py | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/lib/ansible/modules/extras/cloud/softlayer/sl_vm.py b/lib/ansible/modules/extras/cloud/softlayer/sl_vm.py
index 8ef808d9a2..44772fbd90 100644
--- a/lib/ansible/modules/extras/cloud/softlayer/sl_vm.py
+++ b/lib/ansible/modules/extras/cloud/softlayer/sl_vm.py
@@ -225,6 +225,16 @@ except ImportError:
 
 def create_virtual_instance(module):
 
+  instances = vsManager.list_instances(
+    hostname = module.params.get('hostname'),
+    domain = module.params.get('domain'),
+    datacenter = module.params.get('datacenter')
+  )
+  
+  if instances:
+    return False, None
+
+
   # Check if OS or Image Template is provided (Can't be both, defaults to OS)
   if (module.params.get('os_code') != None and module.params.get('os_code') != ''):
     module.params['image_id'] = ''
@@ -303,6 +313,7 @@ def cancel_instance(module):
 
 
 def main():
+
   module = AnsibleModule(
     argument_spec=dict(
       instance_id=dict(),
@@ -338,7 +349,7 @@ def main():
 
   elif module.params.get('state') == 'present':
       (changed, instance) = create_virtual_instance(module)
-      if module.params.get('wait') == True:
+      if module.params.get('wait') == True and instance:
         (changed, instance) = wait_for_instance(module, instance['id'])
 
   module.exit_json(changed=changed, instance=json.loads(json.dumps(instance, default=lambda o: o.__dict__)))