首页 > 解决方案 > 在arm模板中传递除参数文件之外的其他参数

问题描述

我有一些 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 变量引用的参数文件中。但除此之外,我需要传递额外的参数,比如密码或任何必需的参数,我可能是从保险库等外部源读取的,并且需要从命令行传递。我将如何在资源中的参数部分传递它。

我用来部署天蓝色模板的命令如下:

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

标签: azureazure-devopsazure-resource-manager

解决方案


摘自官方文档:

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


推荐阅读