def run(self, subscription_id, deployment_name, group_name, template_uri, parameters_uri): credentials = self.credentials resource_client = ResourceManagementClient( ResourceManagementClientConfiguration( credentials, subscription_id)) template = TemplateLink( uri=template_uri, ) parameters = ParametersLink( uri=parameters_uri, ) result = resource_client.deployments.create_or_update( group_name, deployment_name, Deployment( properties=DeploymentProperties( mode=DeploymentMode.incremental, template_link=template, parameters_link=parameters, ) ) ) return result
def __init__(self, **kwargs): """ Args: All Azure supported sections are allowed, plus: - BuildId(str): The build ID. This will be merged to the parameters dict. All arguments provided will be set as object attributes, but the attributes not supported by the resources API will be unset after initialization so the attributes can be fed to the API wholesale. """ super().__init__(**kwargs) # Can't have both template and templateLink if hasattr(self, "templateLink") and hasattr(self, "template"): raise SystemExit( f"Please specify either template and templateLink") if hasattr(self, "templateLink"): self.templateLink = TemplateLink(self.templateLink) if hasattr(self, "parametersLink"): self.parametersLink = ParametersLink(self.parametersLink) if hasattr(self, "template"): # If template not already a dict, render from path/URL if not isinstance(self.template, dict): parsed_url, template_body = \ gpwm.renderers.get_template_body(self.template) if parsed_url.path[-5:] == ".mako": if not hasattr(self, "parameters"): self.parameters = {} self.parameters["build_id"] = self.BuildId args = [self.name, template_body, self.parameters] template = gpwm.renderers.parse_mako(*args) # mako doesn't need Parameters as they're available to the # template as python variables del self.parameters elif parsed_url.path[-6:] == ".jinja": args = [self.name, template_body, self.parameters] template = gpwm.renderers.parse_jinja(*args) # jinja doesn't need Parameters as they're available to the # template as python variables del self.parameters elif parsed_url.path[-5:] == ".json": args = [self.name, template_body, self.parameters] template = gpwm.renderers.parse_json(*args) elif parsed_url.path[-5:] == ".yaml": args = [self.name, template_body, self.parameters] template = gpwm.renderers.parse_yaml(*args) else: raise SystemExit("file extension not supported") self.template = template self.api_client = AzureClient().get( "resource.ResourceManagementClient")
def deploy_template(self): """ Deploy the targeted template and parameters :param module: Ansible module containing the validated configuration for the deployment template :param client: resource management client for azure :param conn_info: connection info needed :return: """ deploy_parameter = DeploymentProperties() deploy_parameter.mode = self.deployment_mode if not self.parameters_link: deploy_parameter.parameters = self.parameters else: deploy_parameter.parameters_link = ParametersLink( uri=self.parameters_link ) if not self.template_link: deploy_parameter.template = self.template else: deploy_parameter.template_link = TemplateLink( uri=self.template_link ) params = ResourceGroup(location=self.location, tags=self.tags) try: self.rm_client.resource_groups.create_or_update(self.resource_group_name, params) except CloudError as exc: self.fail("Resource group create_or_update failed with status code: %s and message: %s" % (exc.status_code, exc.message)) try: result = self.rm_client.deployments.create_or_update(self.resource_group_name, self.deployment_name, deploy_parameter) deployment_result = self.get_poller_result(result) if self.wait_for_deployment_completion: while deployment_result.properties.provisioning_state not in ['Canceled', 'Failed', 'Deleted', 'Succeeded']: time.sleep(self.wait_for_deployment_polling_period) deployment_result = self.rm_client.deployments.get(self.resource_group_name, self.deployment_name) except CloudError as exc: failed_deployment_operations = self._get_failed_deployment_operations(self.deployment_name) self.log("Deployment failed %s: %s" % (exc.status_code, exc.message)) self.fail("Deployment failed with status code: %s and message: %s" % (exc.status_code, exc.message), failed_deployment_operations=failed_deployment_operations) if self.wait_for_deployment_completion and deployment_result.properties.provisioning_state != 'Succeeded': self.log("provisioning state: %s" % deployment_result.properties.provisioning_state) failed_deployment_operations = self._get_failed_deployment_operations(self.deployment_name) self.fail('Deployment failed. Deployment id: %s' % deployment_result.id, failed_deployment_operations=failed_deployment_operations) return deployment_result
def deploy_template(module, client, conn_info): """ Deploy the targeted template and parameters :param module: Ansible module containing the validated configuration for the deployment template :param client: resource management client for azure :param conn_info: connection info needed :return: """ deployment_name = conn_info["deployment_name"] group_name = conn_info["resource_group_name"] deploy_parameter = DeploymentProperties() deploy_parameter.mode = "Complete" if module.params.get('parameters_link') is None: deploy_parameter.parameters = module.params.get('parameters') else: parameters_link = ParametersLink( uri=module.params.get('parameters_link')) deploy_parameter.parameters_link = parameters_link if module.params.get('template_link') is None: deploy_parameter.template = module.params.get('template') else: template_link = TemplateLink(uri=module.params.get('template_link')) deploy_parameter.template_link = template_link params = ResourceGroup(location=module.params.get('location'), tags=module.params.get('tags')) try: client.resource_groups.create_or_update(group_name, params) result = client.deployments.create_or_update(group_name, deployment_name, deploy_parameter) return result.result() # Blocking wait, return the Deployment object except CloudError as e: module.fail_json( msg='Deploy create failed with status code: %s and message: "%s"' % (e.status_code, e.message))
def deploy_template(module, client, conn_info): """ Deploy the targeted template and parameters :param module: Ansible module containing the validated configuration for the deployment template :param client: resource management client for azure :param conn_info: connection info needed :return: """ deployment_name = conn_info["deployment_name"] group_name = conn_info["resource_group_name"] deploy_parameter = DeploymentProperties() deploy_parameter.mode = module.params.get('deployment_mode') if module.params.get('parameters_link') is None: deploy_parameter.parameters = module.params.get('parameters') else: parameters_link = ParametersLink( uri=module.params.get('parameters_link')) deploy_parameter.parameters_link = parameters_link if module.params.get('template_link') is None: deploy_parameter.template = module.params.get('template') else: template_link = TemplateLink(uri=module.params.get('template_link')) deploy_parameter.template_link = template_link params = ResourceGroup(location=module.params.get('location'), tags=module.params.get('tags')) try: client.resource_groups.create_or_update(group_name, params) result = client.deployments.create_or_update(group_name, deployment_name, deploy_parameter) deployment_result = result.result( ) # Blocking wait, return the Deployment object if module.params.get('wait_for_deployment_completion'): while not deployment_result.properties.provisioning_state in [ 'Canceled', 'Failed', 'Deleted', 'Succeeded' ]: deployment_result = client.deployments.get( group_name, deployment_name) time.sleep( module.params.get('wait_for_deployment_polling_period')) if deployment_result.properties.provisioning_state == 'Succeeded': return deployment_result failed_deployment_operations = get_failed_deployment_operations( module, client, group_name, deployment_name) module.fail_json( msg='Deployment failed. Deployment id: %s' % (deployment_result.id), failed_deployment_operations=failed_deployment_operations) except CloudError as e: failed_deployment_operations = get_failed_deployment_operations( module, client, group_name, deployment_name) module.fail_json( msg='Deploy create failed with status code: %s and message: "%s"' % (e.status_code, e.message), failed_deployment_operations=failed_deployment_operations)
# create deployment name deployment_name = 'testvm' # In[86]: # create deployment using templatelink template = TemplateLink( uri= 'https://raw.githubusercontent.com/dstolts/Azure_Classroom/master/Python/azuredeploy.json', ) parameters = ParametersLink( uri= 'https://raw.githubusercontent.com/dstolts/Azure_Classroom/master/Python/azuredeploy.parameters.json', ) result = client.deployments.create_or_update( group_name, deployment_name, properties=DeploymentProperties(mode=DeploymentMode.incremental, template_link=template, parameters_link=parameters)) pprint.pprint(result) print("Created Deployment:", deployment_name) print(deployment_name + " is being deployed...") result.wait() print("The deployment finished successfully")