ARM 模板 - 主模板中不允许有多个链接模板

ARM Templates - Multiple Linked Templates in a master template not allowed

我们正在尝试使用链接模板创建 ARM 模板。所以我从 vnet 和子网开始,创建了 2 个不同的链接模板。现在我尝试创建一个 master.json 和 master.parameters.json 。参数文件具有网络和一个子网的名称和地址 space 的值。 现在,在主模板中,我尝试使用 2 个链接模板,但 azure devops 中的发布失败并出现以下错误: 部署模板验证失败:“第 37 行和第 5 列的资源 'Microsoft.Resources/deployments/LinkedTemplate' 在模板中定义了多次。 创建或更新模板部署时任务失败。

networkSubnetTest.json

"{
"$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": "https://github.com/something/",
"virtualNetworkTemplateUrl": "[concat(variables('templateBaseUrl'), 'VirtualNetwork.json')]",
"subnetTemplateUrl": "[concat(variables('templateBaseUrl'), 'Subnet.json')]",
"parametersUrl": "[concat(variables('templateBaseUrl'), 'networksubnetnsgtest.parameters.json')]"
},

"resources": [
{
   "apiVersion": "2017-05-10",
   "name": "LinkedTemplate",
   "type": "Microsoft.Resources/deployments",
   "properties": {
     "mode": "Incremental",
     "templateLink": {
        "uri":"[parameters('virtualNetworkTemplateUrl')]"
     },
     "parameters": {
      "uri":"[parameters('parametersUrl')]"
      }
   }
},
{
  "apiVersion": "2017-05-10",
  "name": "LinkedTemplate",
  "type": "Microsoft.Resources/deployments",
  "properties": {
    "mode": "Incremental",
    "templateLink": {
       "uri":"[parameters('subnetTemplateUrl')]"
    },
    "parameters": {
      "uri":"[parameters('parametersUrl')]"
     },
     "dependsOn": [
      "LinkedTemplate",
      "[resourceId('Microsoft.Network/virtualNetworks', parameters('vnetName'))]"
     ]
    }
},
],

"outputs": {
    "returnedVnetName": {
        "type": "string",
        "value": "[resourceId('Microsoft.Network/virtualNetworks', parameters('vnetName'))]"
    },
    "returnedVnetAddressPrefix": {
        "type": "string",
        "value": "[resourceId('Microsoft.Network/virtualNetworks', parameters('vnetAddressPrefix'))]"
    }
  }
}

这里的问题是不是可以有一个主模板和多个链接模板吗?我知道有一个巨大的模板,其中包含所有内容,但我们不想要它。

部署的名称必须不同(因为如果它们相同则无法区分)。所以只需将它们命名为 template1、template2、template3。或按功能 - 他们做什么。比如 deployVnet、deployVm 等

这是我的 ARM 模板版本。模板 name 值在同时部署期间肯定需要不同。如部署记录所示,一个主模板创建了三个部署。

我做了三处值得注意的更改以及其他一些更改来处理 URI。请参阅底部的差异输出。

{
  "$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'))]"
    }
  }
}

差异输出

用于执行的部分 PowerShell:

$templateUri = 'https://<storageAccountName>.blob.core.windows.net/<containerName>/Lab/Master-Fixed.json'
$parameters = @{ 'vnetName' = 'myvnet'; 'vnetAddressPrefix' = '10.0.0.0/16'; 'subnetPrefix' = '10.0.0.0/24'; 'subnetName' = 'mysubnet'; }
New-AzureRmResourceGroupDeployment -Name "brstring-20190124" -ResourceGroupName $resourceGroupName -TemplateUri $templateUri @parameters