การฝึกอบรม
โมดูล
ปรับใช้กับหลายสภาพแวดล้อม Azure โดยใช้ฟีเจอร์เทมเพลต JSON ARM - Training
จัดการการปรับใช้สภาพแวดล้อม Azure หลายรายการของเทมเพลต JSON Azure Resource Manager (เทมเพลต ARM) ของคุณโดยใช้ฟังก์ชัน ตัวแปร แท็ก และไฟล์พารามิเตอร์
เบราว์เซอร์นี้ไม่ได้รับการสนับสนุนอีกต่อไป
อัปเกรดเป็น Microsoft Edge เพื่อใช้ประโยชน์จากคุณลักษณะล่าสุด เช่น การอัปเดตความปลอดภัยและการสนับสนุนด้านเทคนิค
Reduce the repetition of shared values in your Bicep files. Instead, load those values from a shared JSON file within your Bicep file. When using arrays, concatenate the shared values with deployment-specific values in your Bicep code.
When you write your Bicep code, you might have common variables that you reuse across a set of Bicep files. You could duplicate the values each time you declare the resource, such as by copying and pasting the values between your Bicep files. However, this approach is error-prone, and when you need to make changes you need to update each resource definition to keep it in sync with the others.
Furthermore, when you work with variables defined as arrays, you might have a set of common values across multiple Bicep files and also need to add specific values for the resource that you're deploying. When you mix the shared variables with the resource-specific variables, it's harder for someone to understand the distinction between the two categories of variables.
Create a JSON file that includes the variables you need to share. Use the loadJsonContent()
function to load the file and access the variables. For array variables, use the concat()
function to combine the shared values with any custom values for the specific resource.
Suppose you have multiple Bicep files that define resources. You need to use a consistent naming prefix for all of your resources.
Define a JSON file that includes the common naming prefixes that apply across your company:
{
"storageAccountPrefix": "stg",
"appServicePrefix": "app"
}
In your Bicep file, declare a variable that imports the shared naming prefixes:
var sharedNamePrefixes = loadJsonContent('./shared-prefixes.json')
When you define your resource names, use string interpolation to concatenate the shared name prefixes with unique name suffixes:
var appServiceAppName = '${sharedNamePrefixes.appServicePrefix}-myapp-${uniqueString(resourceGroup().id)}'
var storageAccountName = '${sharedNamePrefixes.storageAccountPrefix}myapp${uniqueString(resourceGroup().id)}'
Suppose you have multiple Bicep files that define their own network security groups (NSG). You have a common set of security rules that must be applied to each NSG, and then you have application-specific rules that must be added.
Define a JSON file that includes the common security rules that apply across your company:
{
"securityRules": [
{
"name": "Allow_RDP_from_company_IP_address",
"properties": {
"description": "Allow inbound RDP from the company's IP address range.",
"protocol": "Tcp",
"sourceAddressPrefix": "203.0.113.0/24",
"sourcePortRange": "*",
"destinationAddressPrefix": "VirtualNetwork",
"destinationPortRange": "3389",
"access": "Allow",
"priority": 100,
"direction": "Inbound"
}
},
{
"name": "Allow_VirtualNetwork_to_Storage",
"properties": {
"description": "Allow outbound connections to the Azure Storage service tag.",
"protocol": "Tcp",
"sourceAddressPrefix": "VirtualNetwork",
"sourcePortRange": "*",
"destinationAddressPrefix": "Storage",
"destinationPortRange": "*",
"access": "Allow",
"priority": 100,
"direction": "Outbound"
}
}
// other rules here
]
}
In your Bicep file, declare a variable that imports the shared security rules:
var sharedRules = loadJsonContent('./shared-rules.json', 'securityRules')
Create a variable array that represents the custom rules for this specific NSG:
var customRules = [
{
name: 'Allow_Internet_HTTPS_Inbound'
properties: {
description: 'Allow inbound internet connectivity for HTTPS only.'
protocol: 'Tcp'
sourcePortRange: '*'
destinationPortRange: '443'
sourceAddressPrefix: 'Internet'
destinationAddressPrefix: 'VirtualNetwork'
access: 'Allow'
priority: 400
direction: 'Inbound'
}
}
]
Define the NSG resource. Use the concat()
function to combine the two arrays together and set the securityRules
property:
resource nsg 'Microsoft.Network/networkSecurityGroups@2021-08-01' = {
name: nsgName
location: location
properties: {
securityRules: concat(sharedRules, customRules)
}
}
การฝึกอบรม
โมดูล
ปรับใช้กับหลายสภาพแวดล้อม Azure โดยใช้ฟีเจอร์เทมเพลต JSON ARM - Training
จัดการการปรับใช้สภาพแวดล้อม Azure หลายรายการของเทมเพลต JSON Azure Resource Manager (เทมเพลต ARM) ของคุณโดยใช้ฟังก์ชัน ตัวแปร แท็ก และไฟล์พารามิเตอร์
เอกสาร
Configuration set pattern - Azure Resource Manager
Describes the configuration set pattern.
Bicep modules - Azure Resource Manager
This article describes how to define a module in a Bicep file and how to use module scopes.
Bicep functions - scopes - Azure Resource Manager
Describes the functions to use in a Bicep file to retrieve values about deployment scopes.