ARM 範本的部署函式

Resource Manager 提供下列函式來取得與目前 Azure Resource Manager 範本 (ARM 範本) 部署相關的值:

若要從資源、資源群組或訂用帳戶中取得值,請參閱 資源函式

提示

我們建議使用 Bicep,因為其提供的功能與 ARM 範本相同,而且語法更易於使用。 若要深入了解,請參閱部署函式。

部署

deployment()

傳回目前部署作業的相關資訊。

在 Bicep 中,使用 deployment 函式。

傳回值

此函式會傳回部署期間所傳遞的物件。 所傳回物件中的屬性取決於您是否執行下列內容:

  • 部署範本或範本規格。
  • 部署本機檔案的範本,或部署可透過 URI 存取的遠端檔案範本。
  • 部署至資源群組,或部署至其中一個其他範圍 (Azure 訂閱管理群組租用戶)。

將本機範本部署至資源群組時:此函式會傳回下列格式:

{
  "name": "",
  "properties": {
    "template": {
      "$schema": "",
      "contentVersion": "",
      "parameters": {},
      "variables": {},
      "resources": [],
      "outputs": {}
    },
    "templateHash": "",
    "parameters": {},
    "mode": "",
    "provisioningState": ""
  }
}

將遠端範本部署至資源群組時:此函式會傳回下列格式:

{
  "name": "",
  "properties": {
    "templateLink": {
      "uri": ""
    },
    "template": {
      "$schema": "",
      "contentVersion": "",
      "parameters": {},
      "variables": {},
      "resources": [],
      "outputs": {}
    },
    "templateHash": "",
    "parameters": {},
    "mode": "",
    "provisioningState": ""
  }
}

將範本規格部署至資源群組時:此函式會傳回下列格式:

{
  "name": "",
  "properties": {
    "templateLink": {
      "id": ""
    },
    "template": {
      "$schema": "",
      "contentVersion": "",
      "parameters": {},
      "variables": {},
      "resources": [],
      "outputs": {}
    },
    "templateHash": "",
    "parameters": {},
    "mode": "",
    "provisioningState": ""
  }
}

您部署至 Azure 訂閱、管理群組或租用戶時,傳回物件會包含 location 屬性。 部署本機範本或外部範本時,包含 location 屬性。 其格式為:

{
  "name": "",
  "location": "",
  "properties": {
    "template": {
      "$schema": "",
      "contentVersion": "",
      "resources": [],
      "outputs": {}
    },
    "templateHash": "",
    "parameters": {},
    "mode": "",
    "provisioningState": ""
  }
}

部署 languageVersion 2.0 (部分機器翻譯) 範本時,deployment 函式會傳回有限的屬性子集:

{
  "name": "",
  "location": "",
  "properties": {
    "template": {
      "contentVersion": ""
    },
    "templateLink": {
      "id": "",
      "uri": ""
    }
  }
}

備註

您可以上層範本的 URI 作為基礎,使用 deployment() 連結至另一個範本。

"variables": {
  "sharedTemplateUrl": "[uri(deployment().properties.templateLink.uri, 'shared-resources.json')]"
}

如果您在入口網站中重新部署部署歷程記錄中的範本,此範本會部署為本機檔案。 templateLink 屬性不會在部署函式中傳回。 如果您的範本依賴 templateLink 來建構另一個範本的連結,請勿使用入口網站來重新部署。 請改用您原先用來部署範本的命令。

範例

下列範例會傳回部署物件。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [],
  "outputs": {
    "deploymentOutput": {
      "type": "object",
      "value": "[deployment()]"
    }
  }
}

上述範例會傳回下列物件︰

{
  "name": "deployment",
  "properties": {
    "template": {
      "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
      "contentVersion": "1.0.0.0",
      "resources": [],
      "outputs": {
        "deploymentOutput": {
          "type": "Object",
          "value": "[deployment()]"
        }
      }
    },
    "templateHash": "13135986259522608210",
    "parameters": {},
    "mode": "Incremental",
    "provisioningState": "Accepted"
  }
}

針對訂用帳戶部署,下列範例會傳回部署物件。

{
  "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {},
  "variables": {},
  "resources": [],
  "outputs": {
    "exampleOutput": {
      "type": "object",
      "value": "[deployment()]"
    }
  }
}

environment

environment()

傳回用於部署的 Azure 環境相關資訊。

在 Bicep 中,使用 environment 函式。

備註

若要查看您帳戶的已註冊環境清單,請使用 az cloud listGet-AzEnvironment

傳回值

此函式會傳回目前 Azure 環境的屬性。 下列範例顯示全域 Azure 的屬性。 主權雲端可能會傳回稍微不同的屬性。

{
  "name": "",
  "gallery": "",
  "graph": "",
  "portal": "",
  "graphAudience": "",
  "activeDirectoryDataLake": "",
  "batch": "",
  "media": "",
  "sqlManagement": "",
  "vmImageAliasDoc": "",
  "resourceManager": "",
  "authentication": {
    "loginEndpoint": "",
    "audiences": [
      "",
      ""
    ],
    "tenant": "",
    "identityProvider": ""
  },
  "suffixes": {
    "acrLoginServer": "",
    "azureDatalakeAnalyticsCatalogAndJob": "",
    "azureDatalakeStoreFileSystem": "",
    "azureFrontDoorEndpointSuffix": "",
    "keyvaultDns": "",
    "sqlServerHostname": "",
    "storage": ""
  }
}

範例

下列範例範本會傳回環境物件。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [],
  "outputs": {
    "environmentOutput": {
      "type": "object",
      "value": "[environment()]"
    }
  }
}

上述範例會在部署到全域 Azure 時傳回下列物件:

{
  "name": "AzureCloud",
  "gallery": "https://gallery.azure.com/",
  "graph": "https://graph.windows.net/",
  "portal": "https://portal.azure.com",
  "graphAudience": "https://graph.windows.net/",
  "activeDirectoryDataLake": "https://datalake.azure.net/",
  "batch": "https://batch.core.windows.net/",
  "media": "https://rest.media.azure.net",
  "sqlManagement": "https://management.core.windows.net:8443/",
  "vmImageAliasDoc": "https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master/arm-compute/quickstart-templates/aliases.json",
  "resourceManager": "https://management.azure.com/",
  "authentication": {
    "loginEndpoint": "https://login.microsoftonline.com/",
    "audiences": [
      "https://management.core.windows.net/",
      "https://management.azure.com/"
    ],
    "tenant": "common",
    "identityProvider": "AAD"
  },
  "suffixes": {
    "acrLoginServer": ".azurecr.io",
    "azureDatalakeAnalyticsCatalogAndJob": "azuredatalakeanalytics.net",
    "azureDatalakeStoreFileSystem": "azuredatalakestore.net",
    "azureFrontDoorEndpointSuffix": "azurefd.net",
    "keyvaultDns": ".vault.azure.net",
    "sqlServerHostname": ".database.windows.net",
    "storage": "core.windows.net"
  }
}

parameters

parameters(parameterName)

傳回參數值。 指定的參數名稱必須定義於範本的 parameters 區段中。

在 Bicep 中,使用參數的符號名稱直接參考參數

參數

參數 必要 類型​ 描述
parameterName Yes string 要傳回的參數名稱。

傳回值

指定參數的值。

備註

一般而言,您可以使用參數來設定資源的值。 下列範例會將網站的名稱設定為部署期間所傳入的參數值。

"parameters": {
  "siteName": {
    "type": "string"
  }
}, "resources": [
  {
    "type": "Microsoft.Web/Sites",
    "apiVersion": "2016-08-01",
    "name": "[parameters('siteName')]",
    ...
  }
]

範例

下列範例顯示 parameters 函數的簡化用法。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "stringParameter": {
      "type": "string",
      "defaultValue": "option 1"
    },
    "intParameter": {
      "type": "int",
      "defaultValue": 1
    },
    "objectParameter": {
      "type": "object",
      "defaultValue": {
        "one": "a",
        "two": "b"
      }
    },
    "arrayParameter": {
      "type": "array",
      "defaultValue": [ 1, 2, 3 ]
    },
    "crossParameter": {
      "type": "string",
      "defaultValue": "[parameters('stringParameter')]"
    }
  },
  "variables": {},
  "resources": [],
  "outputs": {
    "stringOutput": {
      "type": "string",
      "value": "[parameters('stringParameter')]"
    },
    "intOutput": {
      "type": "int",
      "value": "[parameters('intParameter')]"
    },
    "objectOutput": {
      "type": "object",
      "value": "[parameters('objectParameter')]"
    },
    "arrayOutput": {
      "type": "array",
      "value": "[parameters('arrayParameter')]"
    },
    "crossOutput": {
      "type": "string",
      "value": "[parameters('crossParameter')]"
    }
  }
}

上述範例中具有預設值的輸出如下:

名稱 類型
stringOutput String 選項 1
intOutput int 1
objectOutput Object {"one": "a", "two": "b"}
arrayOutput 陣列 [1, 2, 3]
crossOutput String 選項 1

如需使用參數的詳細資訊,請參閱 ARM 範本中的參數

variables

variables(variableName)

傳回變數的值。 指定的變數名稱必須定義於範本的 variables 區段中。

在 Bicep 中,使用變數的符號名稱直接參考變數

參數

參數 必要 類型​ 描述
variableName Yes String 要傳回的變數名稱。

傳回值

指定變數的值。

備註

一般而言,您可以使用變數將範本簡化,方法是僅建構一次複雜的值。 下列範例會建構儲存體帳戶的唯一名稱。

"variables": {
  "storageName": "[concat('storage', uniqueString(resourceGroup().id))]"
},
"resources": [
  {
    "type": "Microsoft.Storage/storageAccounts",
    "name": "[variables('storageName')]",
    ...
  },
  {
    "type": "Microsoft.Compute/virtualMachines",
    "dependsOn": [
      "[variables('storageName')]"
    ],
    ...
  }
],

範例

下列範例會傳回不同的變數值。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {},
  "variables": {
    "var1": "myVariable",
    "var2": [ 1, 2, 3, 4 ],
    "var3": "[ variables('var1') ]",
    "var4": {
      "property1": "value1",
      "property2": "value2"
    }
  },
  "resources": [],
  "outputs": {
    "exampleOutput1": {
      "type": "string",
      "value": "[variables('var1')]"
    },
    "exampleOutput2": {
      "type": "array",
      "value": "[variables('var2')]"
    },
    "exampleOutput3": {
      "type": "string",
      "value": "[variables('var3')]"
    },
    "exampleOutput4": {
      "type": "object",
      "value": "[variables('var4')]"
    }
  }
}

上述範例中具有預設值的輸出如下:

名稱 類型
exampleOutput1 String myVariable
exampleOutput2 陣列 [1, 2, 3, 4]
exampleOutput3 String myVariable
exampleOutput4 Object {"property1": "value1", "property2": "value2"}

如需使用變數的詳細資訊,請參閱 ARM 範本中的變數

下一步