File functions for Bicep
This article describes the Bicep functions for loading content from external files.
loadFileAsBase64
loadFileAsBase64(filePath)
Loads the file as a base64 string.
Namespace: sys.
Parameters
| Parameter | Required | Type | Description |
|---|---|---|---|
| filePath | Yes | string | The path to the file to load. The path is relative to the deployed Bicep file. |
Remarks
Use this function when you have binary content you would like to include in deployment. Rather than manually encoding the file to a base64 string and adding it to your Bicep file, load the file with this function. The file is loaded when the Bicep file is compiled to a JSON template. During deployment, the JSON template contains the contents of the file as a hard-coded string.
This function requires Bicep version 0.4.412 or later.
The maximum allowed size of the file is 96 Kb.
Return value
The file as a base64 string.
loadTextContent
loadTextContent(filePath, [encoding])
Loads the content of the specified file as a string.
Namespace: sys.
Parameters
| Parameter | Required | Type | Description |
|---|---|---|---|
| filePath | Yes | string | The path to the file to load. The path is relative to the deployed Bicep file. |
| encoding | No | string | The file encoding. The default value is utf-8. The available options are: iso-8859-1, us-ascii, utf-16, utf-16BE, or utf-8. |
Remarks
Use this function when you have content that is more stored in a separate file. Rather than duplicating the content in your Bicep file, load the content with this function. For example, you can load a deployment script from a file. The file is loaded when the Bicep file is compiled to the JSON template. During deployment, the JSON template contains the contents of the file as a hard-coded string.
When loading a JSON file, you can use the json function with the loadTextContent function to create a JSON object. In VS Code, the properties of the loaded object are available intellisense. For example, you can create a file with values to share across many Bicep files. An example is shown in this article.
This function requires Bicep version 0.4.412 or later.
The maximum allowed size of the file is 131,072 characters, including line endings.
Return value
The contents of the file as a string.
Examples
The following example loads a script from a file and uses it for a deployment script.
resource exampleScript 'Microsoft.Resources/deploymentScripts@2020-10-01' = {
name: 'exampleScript'
location: resourceGroup().location
kind: 'AzurePowerShell'
identity: {
type: 'UserAssigned'
userAssignedIdentities: {
'/subscriptions/{sub-id}/resourcegroups/{rg-name}/providers/Microsoft.ManagedIdentity/userAssignedIdentities/{id-name}': {}
}
}
properties: {
azPowerShellVersion: '3.0'
scriptContent: loadTextContent('myscript.ps1')
retentionInterval: 'P1D'
}
}
In the next example, you create a JSON file that contains values you want to use for a network security group.
{
"description": "Allows SSH traffic",
"protocol": "Tcp",
"sourcePortRange": "*",
"destinationPortRange": "22",
"sourceAddressPrefix": "*",
"destinationAddressPrefix": "*",
"access": "Allow",
"priority": 100,
"direction": "Inbound"
}
You load that file and convert it to a JSON object. You use the object to assign values to the resource.
param location string = resourceGroup().location
var nsgconfig = json(loadTextContent('nsg-security-rules.json'))
resource newNSG 'Microsoft.Network/networkSecurityGroups@2021-02-01' = {
name: 'example-nsg'
location: location
properties: {
securityRules: [
{
name: 'SSH'
properties: {
description: nsgconfig.description
protocol: nsgconfig.protocol
sourcePortRange: nsgconfig.sourcePortRange
destinationPortRange: nsgconfig.destinationPortRange
sourceAddressPrefix: nsgconfig.sourceAddressPrefix
destinationAddressPrefix: nsgconfig.destinationAddressPrefix
access: nsgconfig.access
priority: nsgconfig.priority
direction: nsgconfig.direction
}
}
]
}
}
You can reuse the file of values in other Bicep files that deploy a network security group.
Next steps
- For a description of the sections in a Bicep file, see Understand the structure and syntax of Bicep files.
Maklum balas
Kirim dan lihat maklum balas untuk