1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

azure check for hosted service and vm before creating

This commit is contained in:
Richard Lander 2014-12-17 17:29:07 -05:00 committed by Matt Clay
parent d8af3f3b67
commit 2091278ff0

View file

@ -242,7 +242,7 @@ def create_virtual_machine(module, azure):
azure: authenticated azure ServiceManagementService object
Returns:
True if a new virtual machine was created, false otherwise
True if a new virtual machine and/or cloud service was created, false otherwise
"""
name = module.params.get('name')
hostname = module.params.get('hostname') or name + ".cloudapp.net"
@ -258,18 +258,24 @@ def create_virtual_machine(module, azure):
wait = module.params.get('wait')
wait_timeout = int(module.params.get('wait_timeout'))
changed = False
# Check if a deployment with the same name already exists
cloud_service_name_available = azure.check_hosted_service_name_availability(name)
if not cloud_service_name_available.result:
changed = False
else:
changed = True
# Create cloud service if necessary
if cloud_service_name_available.result:
# cloud service does not exist; create it
try:
result = azure.create_hosted_service(service_name=name, label=name, location=location)
_wait_for_completion(azure, result, wait_timeout, "create_hosted_service")
changed = True
except WindowsAzureError as e:
module.fail_json(msg="failed to create the new service name, it already exists: %s" % str(e))
module.fail_json(msg="failed to create the new service, error was: %s" % str(e))
try:
# check to see if a vm with this name exists; if so, do nothing
azure.get_role(name, name, name)
except WindowsAzureMissingResourceError:
# vm does not exist; create it
# Create linux configuration
disable_ssh_password_authentication = not password
@ -322,10 +328,10 @@ def create_virtual_machine(module, azure):
role_type='PersistentVMRole',
virtual_network_name=virtual_network_name)
_wait_for_completion(azure, result, wait_timeout, "create_virtual_machine_deployment")
changed = True
except WindowsAzureError as e:
module.fail_json(msg="failed to create the new virtual machine, error was: %s" % str(e))
try:
deployment = azure.get_deployment_by_name(service_name=name, deployment_name=name)
return (changed, urlparse(deployment.url).hostname, deployment)