Skip to content

Latest commit

 

History

History
372 lines (307 loc) · 10.3 KB

resource-group-template-functions-deployment.md

File metadata and controls

372 lines (307 loc) · 10.3 KB
title description services documentationcenter author manager editor ms.assetid ms.service ms.devlang ms.topic ms.tgt_pltfrm ms.workload ms.date ms.author
Azure Resource Manager template functions - deployment | Microsoft Docs
Describes the functions to use in an Azure Resource Manager template to retrieve deployment information.
azure-resource-manager
na
tfitzmac
timlt
tysonn
azure-resource-manager
na
article
na
na
09/05/2017
tomfitz

Deployment functions for Azure Resource Manager templates

Resource Manager provides the following functions for getting values from sections of the template and values related to the deployment:

To get values from resources, resource groups, or subscriptions, see Resource functions.

deployment()

Returns information about the current deployment operation.

Return value

This function returns the object that is passed during deployment. The properties in the returned object differ based on whether the deployment object is passed as a link or as an in-line object. When the deployment object is passed in-line, such as when using the -TemplateFile parameter in Azure PowerShell to point to a local file, the returned object has the following format:

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

When the object is passed as a link, such as when using the -TemplateUri parameter to point to a remote object, the object is returned in the following format:

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

Remarks

You can use deployment() to link to another template based on the URI of the parent template.

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

Example

The following example template returns the deployment object:

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

The preceding example returns the following object:

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

To deploy this example template with Azure CLI, use:

az group deployment create -g functionexamplegroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/deployment.json

To deploy this example template with PowerShell, use:

New-AzureRmResourceGroupDeployment -ResourceGroupName functionexamplegroup -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/deployment.json

parameters(parameterName)

Returns a parameter value. The specified parameter name must be defined in the parameters section of the template.

Parameters

Parameter Required Type Description
parameterName Yes string The name of the parameter to return.

Return value

The value of the specified parameter.

Remarks

Typically, you use parameters to set resource values. The following example sets the name of web site to the parameter value passed in during deployment.

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

Example

The following example template shows a simplified use of the parameters function.

{
	"$schema": "https://schema.management.azure.com/schemas/2015-01-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": {
			"value": "[parameters('stringParameter')]",
			"type" : "string"
		},
        "intOutput": {
			"value": "[parameters('intParameter')]",
			"type" : "int"
		},
        "objectOutput": {
			"value": "[parameters('objectParameter')]",
			"type" : "object"
		},
        "arrayOutput": {
			"value": "[parameters('arrayParameter')]",
			"type" : "array"
		},
        "crossOutput": {
			"value": "[parameters('crossParameter')]",
			"type" : "string"
		}
	}
}

The output from the preceding example with the default values is:

Name Type Value
stringOutput String option 1
intOutput Int 1
objectOutput Object {"one": "a", "two": "b"}
arrayOutput Array [1, 2, 3]
crossOutput String option 1

To deploy this example template with Azure CLI, use:

az group deployment create -g functionexamplegroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/parameters.json

To deploy this example template with PowerShell, use:

New-AzureRmResourceGroupDeployment -ResourceGroupName functionexamplegroup -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/parameters.json

variables(variableName)

Returns the value of variable. The specified variable name must be defined in the variables section of the template.

Parameters

Parameter Required Type Description
variableName Yes String The name of the variable to return.

Return value

The value of the specified variable.

Remarks

Typically, you use variables to simplify your template by constructing complex values only once. The following example constructs a unique name for a storage account.

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

Example

The following example template returns different variable values.

{
	"$schema": "https://schema.management.azure.com/schemas/2015-01-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": {
			"value": "[variables('var1')]",
			"type" : "string"
		},
		"exampleOutput2": {
			"value": "[variables('var2')]",
			"type" : "array"
		},
		"exampleOutput3": {
			"value": "[variables('var3')]",
			"type" : "string"
		},
		"exampleOutput4": {
			"value": "[variables('var4')]",
			"type" : "object"
		}
	}
}

The output from the preceding example with the default values is:

Name Type Value
exampleOutput1 String myVariable
exampleOutput2 Array [1, 2, 3, 4]
exampleOutput3 String myVariable
exampleOutput4 Object {"property1": "value1", "property2": "value2"}

To deploy this example template with Azure CLI, use:

az group deployment create -g functionexamplegroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/variables.json

To deploy this example template with PowerShell, use:

New-AzureRmResourceGroupDeployment -ResourceGroupName functionexamplegroup -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/functions/variables.json

Next steps