Parameters and variables

Our first template is rather static, and it uses neither variables in the JSON context nor parameters that a user can set during a deployment. The variables and parameters in templates work exactly as they do in PowerShell cmdlets. Variables can be defined at runtime, and can get their values from parameters, whereas parameters are requested before the template is deployed and are then used internally:

# Extend the existing template by parameters and variables
$template.parameters = @{
storageAccountType = @{
type = 'string'
allowedValues = @(
'Standard_LRS',
'Standard_ZRS'
)
metadata = @{
description = 'The type of storage account. Mandatory.'
}
}
location = @{
type = 'string'
defaultValue = $location
metadata = @{
description = 'Location for all resources.'
}
}
}

The template now contains two parameters that can automatically be used with the New-AzureRmResourceGroupDeployment cmdlet as dynamic parameters. When using lists, such as the allowed values of the storage account type, a ValidateSet attribute will be used for the dynamic parameter, resulting in a nice drop-down field:

$template.variables = @{
storageAccountName = "contoso$((1..8 | ForEach-Object { [char[]](97..122) | Get-Random }) -join '')"
}

# Reference variables and parameters
$template.resources[0].name = "[variables('storageAccountName')]"
$template.resources[0].location = "[parameters('location')]"
$template.resources[0].sku.name = "[parameters('storageAccountType')]"

# Export and deploy again
$template | ConvertTo-Json -Depth 100 | Out-File -FilePath .StorageAccountParameter.json -Encoding UTF8

$deployment = @{
Name = 'SecondDeployment'
ResourceGroupName = $resourceGroupName
TemplateFile = '.StorageAccountParameter.json'
Verbose = $true
}

# Use your template parameters in the resource group deployment!
New-AzureRmResourceGroupDeployment @deployment -location 'westeurope' -storageAccountType 'Standard_LRS'
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset