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

时间:2019-01-24 21:00:27

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

我们正在尝试使用链接模板创建ARM模板。所以我从vnet和子网开始,创建了2个不同的链接模板。现在我尝试创建一个master.json和master.parameters.json。参数文件具有网络和一个子网的名称和地址空间的值。 现在,在主模板中,我尝试使用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'))]"
    }
  }
}

这里的问题是,不可能有一个主模板和多个链接模板吗?我知道有一个巨大的模板,上面写着所有内容,但我们不想要它。

2 个答案:

答案 0 :(得分:0)

部署的名称必须不同(因为部署名称不能相同)。因此,只需将它们命名为template1,template2,template3。或通过功能-他们做什么。例如deployVnet,deployVm等。

答案 1 :(得分:0)

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

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

enter image description here

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

差异输出 enter image description here

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