【问题标题】:Passing additional parameters apart from parameter file in arm template在arm模板中传递除参数文件之外的其他参数
【发布时间】:2019-11-21 15:45:28
【问题描述】:

我有一些 arm 模板,其中子网资源的创建依赖于 VNet 的创建。下面是代码:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "vnetName": {
      "type": "string",
      "metadata": {
        "description": "Name of the Virtual Network"
      }
    },
    "vnetAddressPrefix": {
      "type": "string",
      "metadata": {
        "description": "The IP Address pool for the virtual network in CIDR format."
      }
    },
    "subnetPrefix": {
      "type": "string",
      "metadata": {
        "description": "The IP Address pool for the Subnet in CIDR format."
      }
    },
    "subnetName": {
      "type": "string",
      "metadata": {
        "description": "Name of the Subnet"
      }
    }
  },
  "variables": {
    "templateBaseUrl": "[deployment().properties.templateLink.uri]",
    "virtualNetworkTemplateUrl": "[uri(variables('templateBaseUrl'), 'VirtualNetwork.json')]",
    "subnetTemplateUrl": "[uri(variables('templateBaseUrl'), 'Subnet.json')]",
    "parametersUrl": "[uri(variables('templateBaseUrl'), 'networksubnetnsgtest.parameters.json')]"
  },
  "resources": [
    {
      "apiVersion": "2017-05-10",
      "name": "VnetDeployment",
      "type": "Microsoft.Resources/deployments",
      "properties": {
        "mode": "Incremental",
        "templateLink": {
          "uri": "[variables('virtualNetworkTemplateUrl')]"
        },
        "parameters": {
          "uri": {
            "value": "[variables('parametersUrl')]"
          }
        }
      }
    },
    {
      "apiVersion": "2017-05-10",
      "name": "SubnetDeployment",
      "type": "Microsoft.Resources/deployments",
      "properties": {
        "mode": "Incremental",
        "templateLink": {
          "uri": "[variables('subnetTemplateUrl')]"
        },
        "parameters": {
          "uri": {
            "value": "[variables('parametersUrl')]"
          }
        }
      },
      "dependsOn": [
        "VnetDeployment"
      ]
    }
  ],
  "outputs": {
    "returnedVnetName": {
      "type": "string",
      "value": "[resourceId('Microsoft.Network/virtualNetworks', parameters('vnetName'))]"
    },
    "returnedVnetAddressPrefix": {
      "type": "string",
      "value": "[resourceId('Microsoft.Network/virtualNetworks', parameters('vnetAddressPrefix'))]"
    }
  }
}

如您所见,parameterUrl 变量引用的参数文件中保存的参数很少。但除此之外,我需要传递额外的参数,比如密码或任何必需的参数,我可能是从保险库等外部源读取的,并且需要从命令行传递。 我将如何在资源中的 parameters 部分传递它。

我用来部署 azure 模板的命令如下:

az group deployment create --resource-group testrg --template-file ${WORKSPACE}/azuredeploy.json --parameters @${WORKSPACE}/networksubnetnsgtest.parameters.json --parameters DBMasterUserPassword=${DBMasterUserPassword}

【问题讨论】:

  • 您好,想看看4c74356b41最后的建议对您有用吗?如果没有,请随时在下面发表评论。然后我们可以继续帮助解决这个问题。
  • @MerlinLiang-MSFT 我仍然有疑问。在上面的 json 中,我有 2 个资源,1 个 VNetDeployment 和 2) SubnetDeployment。现在 SubNetDeployment 需要 2 类参数。 1)在我作为“uri”传递的networksubnetnsgtest.parameters.json文件中提到:{“value”:“[variables('parametersUrl')]”} 2)在一些密码上,我作为--parameter = 在 CLI 命令执行期间。现在我将如何将 = 作为参数传递给参数 {} 部分中的 SubnetDeployment 资源。或者我不需要传入参数 {} 部分。

标签: azure azure-devops azure-resource-manager


【解决方案1】:

摘自官方文档:

az group deployment create -g MyResourceGroup --template-file azuredeploy.json \
    --parameters @params.json --parameters https://mysite/params.json --parameters MyValue=This MyArray=@array.json

https://docs.microsoft.com/en-us/cli/azure/group/deployment?view=azure-cli-latest#examples

【讨论】:

  • 所以,假设在上面的示例中我有部署资源 SubnetDeployment,它使用 Subnet.json 部署子网资源,在 Subnet.json 内部,我使用参数 MyValue,即“parameters('MyValue')”,那么它是否会自己获取参数值,或者我们需要在 azuredeploy.json 的参数部分中传递参数值,除了参数文件引用 "parameters": { "uri": { "value": "[variables('parametersUrl') ]" }
  • 我不确定我是否按照您的要求进行操作,但是您不能将参数传递给嵌套模板(父模板会这样做)您只能将参数传递给父模板
  • 如果在嵌套模板中使用参数值,它将如何传播到嵌套模板。说在参数文件中保密是不可能的,那么除了嵌套模板参数文件之外,如何将秘密参数传递给嵌套模板?。
  • 你将在模板中传递它,就像你应该做的那样:docs.microsoft.com/en-us/azure/azure-resource-manager/…
  • 正确,在您提供的上述链接中,我们将参数作为“参数”传递:{“StorageAccountName”:{“value”:“[parameters('StorageAccountName')]”}} 但现在如何我会像我们在“parameters”上方传递的那样传递参数文件url:{“uri”:{“value”:“[variables('parametersUrl')]”}}。是否可以同时传递 url 和存储帐户名称作为参数?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-15
  • 1970-01-01
  • 1970-01-01
  • 2014-01-16
  • 1970-01-01
相关资源
最近更新 更多