Tutorial: Membuat spesifikasi templat dengan templat yang ditautkan

Pelajari cara membuat spesifikasi templat dengan templat utama dan templat yang ditautkan. Anda menggunakan spesifikasi templat untuk berbagi templat ARM dengan pengguna lain di organisasi Anda. Artikel ini menunjukkan kepada Anda cara membuat spesifikasi templat untuk mengemas templat utama dan templat yang ditautkan menggunakan properti relativePath dari sumber daya penyebaran.

Prasyarat

Akun Azure dengan langganan aktif. Buat akun gratis.

Catatan

Untuk menggunakan spesifikasi templat dengan Azure PowerShell, Anda harus menginstal versi 5.0.0 atau yang lebih baru. Untuk menggunakannya dengan Azure CLI, gunakan versi 2.14.2 atau yang lebih baru.

Membuat templat yang ditautkan

Buat templat utama dan templat yang ditautkan.

Untuk menautkan templat, tambahkan sumber daya penyebaran ke templat utama Anda. Di properti templateLink, tentukan jalur relatif templat yang ditautkan sesuai dengan jalur templat induk.

Templat yang ditautkan disebut linkedTemplate.json, dan disimpan dalam subfolder yang disebut artefak di jalur di mana templat utama disimpan. Anda bisa menggunakan salah satu nilai berikut ini untuk relativePath:

  • ./artifacts/linkedTemplate.json
  • /artifacts/linkedTemplate.json
  • artifacts/linkedTemplate.json

Properti relativePath selalu relatif terhadap file templat di mana relativePath dinyatakan, jadi jika ada linkedTemplate2.json lain yang dipanggil dari linkedTemplate.json dan linkedTemplate2.json disimpan dalam subfolder artefak yang sama, relativePath yang ditentukan dalam linkedTemplate.json hanya linkedTemplate2.json.

  1. Buat templat utama dengan JSON berikut ini. Simpan templat utama sebagai azuredeploy.json komputer lokal Anda. Tutorial ini mengasumsikan Anda telah menyimpan ke jalur c:\Templates\linkedTS\azuredeploy.json tetapi Anda dapat menggunakan jalur apa pun.

    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "location": {
          "type": "string",
          "defaultValue": "westus2",
          "metadata":{
            "description": "Specify the location for the resources."
          }
        },
        "storageAccountType": {
          "type": "string",
          "defaultValue": "Standard_LRS",
          "metadata":{
            "description": "Specify the storage account type."
          }
        }
      },
      "variables": {
        "appServicePlanName": "[format('plan{0}', uniquestring(resourceGroup().id))]"
      },
      "resources": [
        {
          "type": "Microsoft.Web/serverfarms",
          "apiVersion": "2022-09-01",
          "name": "[variables('appServicePlanName')]",
          "location": "[parameters('location')]",
          "sku": {
            "name": "B1",
            "tier": "Basic",
            "size": "B1",
            "family": "B",
            "capacity": 1
          },
          "kind": "linux",
          "properties": {
            "perSiteScaling": false,
            "reserved": true,
            "targetWorkerCount": 0,
            "targetWorkerSizeId": 0
          }
        },
        {
          "type": "Microsoft.Resources/deployments",
          "apiVersion": "2022-09-01",
          "name": "createStorage",
          "properties": {
            "mode": "Incremental",
            "templateLink": {
              "relativePath": "artifacts/linkedTemplate.json"
            },
            "parameters": {
              "storageAccountType": {
                "value": "[parameters('storageAccountType')]"
              }
            }
          }
        }
      ]
    }
    

    Catatan

    ApiVersion Microsoft.Resources/deployments harus 2020-06-01 atau yang lebih baru.

  2. Buat direktori yang disebut artefak di folder tempat templat utama disimpan.

  3. Buat templat yang ditautkan dengan JSON berikut:

    {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "parameters": {
        "storageAccountType": {
          "type": "string",
          "defaultValue": "Standard_LRS",
          "allowedValues": [
            "Standard_LRS",
            "Standard_GRS",
            "Standard_ZRS",
            "Premium_LRS"
          ],
          "metadata": {
            "description": "Storage Account type"
          }
        },
        "location": {
          "type": "string",
          "defaultValue": "[resourceGroup().location]",
          "metadata": {
            "description": "Location for all resources."
          }
        }
      },
      "variables": {
        "storageAccountName": "[format('store{0}', uniquestring(resourceGroup().id))]"
      },
      "resources": [
        {
          "type": "Microsoft.Storage/storageAccounts",
          "apiVersion": "2022-09-01",
          "name": "[variables('storageAccountName')]",
          "location": "[parameters('location')]",
          "sku": {
            "name": "[parameters('storageAccountType')]"
          },
          "kind": "StorageV2",
          "properties": {}
        }
      ],
      "outputs": {
        "storageAccountName": {
          "type": "string",
          "value": "[variables('storageAccountName')]"
        }
      }
    }
    
  4. Simpan templat sebagai linkedTemplate.json di folder artefak.

Membuat spesifikasi templat

Spesifikasi templat disimpan dalam Grup sumber daya. Buat grup sumber daya, lalu buat spesifikasi templat dengan skrip berikut. Nama spesifikasi templat adalah webSpec.

New-AzResourceGroup `
  -Name templateSpecRG `
  -Location westus2

New-AzTemplateSpec `
  -Name webSpec `
  -Version "1.0.0.0" `
  -ResourceGroupName templateSpecRG `
  -Location westus2 `
  -TemplateFile "c:\Templates\linkedTS\azuredeploy.json"

Jika sudah selesai, Anda bisa menampilkan spesifikasi templat dari portal Microsoft Azure atau dengan menggunakan cmdlet berikut:

Get-AzTemplateSpec -ResourceGroupName templatespecRG -Name webSpec

Menyebarkan spesifikasi templat

Sekarang Anda dapat menyebarkan spesifikasi templat. Menyebarkan spesifikasi templat sama seperti menyebarkan templat yang ada di dalamnya, kecuali Anda meneruskan ID sumber daya dari spesifikasi templat. Anda menggunakan perintah penyebaran yang sama, dan jika perlu, berikan nilai parameter untuk spesifikasi templat.

New-AzResourceGroup `
  -Name webRG `
  -Location westus2

$id = (Get-AzTemplateSpec -ResourceGroupName templateSpecRG -Name webSpec -Version "1.0.0.0").Versions.Id

New-AzResourceGroupDeployment `
  -TemplateSpecId $id `
  -ResourceGroupName webRG

Langkah berikutnya

Untuk informasi selengkapnya tentang menyebarkan spesifikasi templat sebagai templat yang ditautkan, lihat Tutorial: Menyebarkan spesifikasi templat sebagai templat yang ditautkan.