简体   繁体   English

Python Azure ARM 模板部署失败

[英]Python Azure ARM Template Deployment failed

I am trying to deploy a keyvault using ARM Templates.我正在尝试使用 ARM 模板部署密钥库。 The template I am using as base is located at azuredeploy.json , If I do not provide parameters, the deployment succeed, however as soon as I add a parameters file, like the following one.我用作基础的模板位于azuredeploy.json ,如果我不提供参数,部署成功,但是只要我添加一个参数文件,如下所示。
Parameters file:参数文件:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters":
  {
    "vaultName":
    {
      "value": <key vault name>
    }
  }
}

(InvalidTemplate) Deployment template validation failed: 'The template parameters '$schema, contentVersion, parameters' in the parameters file are not valid; (InvalidTemplate)部署模板验证失败:'参数文件中的模板参数'$schema、contentVersion、parameters'无效; they are not present in the original template and can therefore not be provided at deployment time.它们不存在于原始模板中,因此无法在部署时提供。 The only supported parameters for this template are 'vaultName, location, enabledForDeployment, enabledForDiskEncryption, enabledForTemplateDeployment, enablePurgeProtection, enableRbacAuthorization, enableSoftDelete, softDeleteRetentionInDays, tenantId,.networkRuleBypassOptions, NetworkRuleAction, ipRules, accessPolicies, virtualNetworkRules, skuName, tags'.此模板唯一支持的参数是“vaultName、location、enabledForDeployment、enabledForDiskEncryption、enabledForTemplateDeployment、enablePurgeProtection、enableRbacAuthorization、enableSoftDelete、softDeleteRetentionInDays、tenantId、.networkRuleBypassOptions、NetworkRuleAction、ipRules、accessPolicies、virtualNetworkRules、skuName、tags”。 Please see https://aka.ms/arm-deploy/#parameter-file for usage details.'.有关使用详细信息,请参阅https://aka.ms/arm-deploy/#parameter-file 。'。
#Code: InvalidTemplate #代码:无效模板
#Message: Deployment template validation failed: 'The template parameters '$schema, contentVersion, parameters' in the parameters file are not valid; #Message:部署模板验证失败:“参数文件中的模板参数‘$schema、contentVersion、parameters’无效; they are not present in the original template and can therefore not be provided at deployment time.它们不存在于原始模板中,因此无法在部署时提供。 The only supported parameters for this template are 'vaultName, location, enabledForDeployment, enabledForDiskEncryption, enabledForTemplateDeployment, enablePurgeProtection, enableRbacAuthorization, enableSoftDelete, softDeleteRetentionInDays, tenantId,.networkRuleBypassOptions, NetworkRuleAction, ipRules, accessPolicies, virtualNetworkRules, skuName, tags'.此模板唯一支持的参数是“vaultName、location、enabledForDeployment、enabledForDiskEncryption、enabledForTemplateDeployment、enablePurgeProtection、enableRbacAuthorization、enableSoftDelete、softDeleteRetentionInDays、tenantId、.networkRuleBypassOptions、NetworkRuleAction、ipRules、accessPolicies、virtualNetworkRules、skuName、tags”。 Please see https://aka.ms/arm-deploy/#parameter-file for usage details.'.有关使用详细信息,请参阅https://aka.ms/arm-deploy/#parameter-file 。'。
Additional Information:Type: TemplateViolation附加信息:类型: TemplateViolation
Info: { "lineNumber": 0, "linePosition": 0, "path": "" }信息: {“lineNumber”:0,“linePosition”:0,“路径”:“”}

Based on the error message, the issue is at the parameters file, but I am not able to identify what is wrong.根据错误消息,问题出在参数文件中,但我无法确定问题出在哪里。 Do you have any clue where the error can be?您知道错误可能出在哪里吗?

**Python Code: **蟒蛇代码:

import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.resource.resources.models import DeploymentMode
from azure.mgmt.resource.resources.models import Deployment
from azure.mgmt.resource.resources.models import DeploymentProperties
from miscellaneous.logger import Logger
from msrestazure.azure_cloud import get_cloud_from_metadata_endpoint
from uuid6 import uuid7
class AzureConnection(object):
    def __init__(self, subscriptionId, resourceGroup):
        self.logger = Logger("Azure Connection")
        self.logger.info("Retrieving the list of available endpoint")
        endpoints = get_cloud_from_metadata_endpoint(os.environ.get("ARM_ENDPOINT"))
        self.subscriptionId = subscriptionId
        self.resourceGroup = resourceGroup
        self.credentials = DefaultAzureCredential()
        self.logger.info("Creating a client for deploying resources on subscription {}".format(self.subscriptionId))
        self.client = ResourceManagementClient(self.credentials, self.subscriptionId,
            base_url=endpoints.endpoints.resource_manager)
        self.logger.success("Client was successfully created")
    def deploy(self, template):
        resources = ""
        for resource in template.get("resources"):
            resources += "\n\t {}".format(resource.get("type"))
        self.logger.info("The following resources: {}\nwill be deployed".format(resources))
        deploymentProperties = DeploymentProperties(
            mode=DeploymentMode.incremental,
            template=template
        )
        self.logger.info("Attempting deploy operation")
        try:
            deployment_async_operation = self.client.deployments.begin_create_or_update(
                self.resourceGroup,
                uuid7(),
                Deployment(properties=deploymentProperties)
            )
        except:
            self.logger.error("The resources could not be deployed");
        self.logger.success("Resources were successfully deployed")
    def deployWithParameters(self, template, parameters):
        resources = ""
        for resource in template.get("resources"):
            resources += "\n\t {}".format(resource.get("type"))
        self.logger.info("The following resources: {}\nwill be deployed".format(resources))
        parameters = {k: {"value": v} for k, v in parameters.items()}
        deploymentProperties = DeploymentProperties(
            mode=DeploymentMode.incremental,
            template=template,
            parameters=parameters
        )
        self.logger.info("Attempting deploy operation")
        deployment_async_operation = self.client.deployments.begin_create_or_update(
            self.resourceGroup,
            uuid7(),
            Deployment(properties=deploymentProperties)
        )

from dotenv import load_dotenv
load_dotenv()
azureConnection = AzureConnection(os.environ.get("AZURE_SUBSCRIPTION_ID"), os.environ.get("AZURE_RESOURCE_GROUP"))
with open((os.path.dirname(__file__), "templates", <fileName>), "r") as file:
  template = json.load(file)
with open((os.path.dirname(__file__), "parameters", <fileName>), "r") as file:
  json = json.load(file)
deployment = azureConnection.deployWithParameters(template, parameter)

After playing a lot with the code, I came with a solution out of the box.在大量使用代码之后,我找到了一个开箱即用的解决方案。 It looks like that a parameters template is not required看起来不需要参数模板

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters":
  {
    "vaultName":
    {
      "value": <key vault name>
    }
  }
}

I just need to send the parameters without further structure Ex:我只需要在没有进一步结构的情况下发送参数 Ex:

{
  "vaultName":
  {
    "value": <key vault name>
  },
  "location":
  {
    "value": <location value>
  },
  ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM