สคริปต์การปรับใช้คืออะไร
ในหน่วยนี้ คุณจะได้เรียนรู้วิธีการที่ deploymentScripts ทรัพยากรสามารถขยายเทมเพลต Azure Resource Manager (ARM)
เทมเพลต ARM เป็นสิ่งที่ยอดเยี่ยม คุณสามารถใช้เพื่อประกาศสถานะที่ต้องการของโครงสร้างพื้นฐานระบบคลาวด์ของคุณ และให้ API และบริการค้นหาวิธีการนําคุณไปที่นั่นได้ อย่างไรก็ตาม ในบางครั้งคุณจําเป็นต้องดําเนินการที่อยู่นอกสิ่งที่ Azure Resource Manager มี
สคริปต์การปรับใช้คืออะไร
ทรัพยากร deploymentScripts คือ PowerShell หรือสคริปต์ Bash ที่ทํางานในคอนเทนเนอร์ Docker โดยเป็นส่วนหนึ่งของการปรับใช้เทมเพลตของคุณ รูปภาพคอนเทนเนอร์เริ่มต้นมี Azure CLI หรือ Azure PowerShell ที่พร้อมใช้งาน สคริปต์เหล่านี้ทํางานในระหว่างการประมวลผลเทมเพลต ARM ดังนั้นคุณสามารถเพิ่มลักษณะการทํางานแบบกําหนดเองในกระบวนการปรับใช้
สคริปต์การปรับใช้ใช้ใช้ข้อมูลประจําตัวที่มีการจัดการ การรับรองความถูกต้องกับ Azure ข้อมูลประจําตัวที่มีการจัดการคือองค์ประกอบหลักของบริการที่มีการจัดการข้อมูลประจําตัวและวงจรชีวิตของแพลตฟอร์ม Azure ข้อมูลประจําตัวนี้คือสิ่งที่คําสั่ง Azure PowerShell หรือ Azure CLI ใช้เพื่อดําเนินการกับสภาพแวดล้อม เนื่องจากคุณกําหนดข้อมูลประจําตัว คุณควบคุมขอบเขตของสิ่งที่ทรัพยากร deploymentScripts อาจส่งผลต่อ
ทรัพยากร deploymentScripts สร้างเอาต์พุตที่ทรัพยากรอื่น ๆ ในการปรับใช้สามารถใช้ได้ จากนั้นคุณสามารถค้นหาข้อมูลจากระบบภายนอกหรือให้ข้อมูลตามสถานะปัจจุบันของสภาพแวดล้อมของคุณเพื่อส่งผลกระทบต่อส่วนที่เหลือของการปรับใช้ได้
สคริปต์การปรับใช้ทํางานอย่างไร
ทรัพยากร deploymentScripts ใช้สคริปต์ที่ผู้ใช้เตรียมไว้ (จากเทมเพลตหรือ URI) และอาจสนับสนุนสคริปต์และเรียกใช้งานในอินสแตนซ์คอนเทนเนอร์ Azure อินสแตนซ์คอนเทนเนอร์ได้รับการกําหนดข้อมูลประจําตัวที่มีการจัดการที่คุณระบุ สคริปต์และผลลัพธ์จะถูกจัดเก็บไว้ในการแชร์ไฟล์ สําหรับบัญชีเก็บข้อมูล Azure
เมื่อการปรับใช้เทมเพลตทํางาน ระบบจะตรวจสอบว่ามีทรัพยากร deploymentScripts อยู่ในกลุ่มทรัพยากรที่กําหนดเป้าหมายหรือไม่ ถ้าเป็นเช่นนั้น จะเปรียบเทียบคุณสมบัติ หากทุกอย่างตรงกัน ไม่มีอะไรใหม่เกิดขึ้น ถ้าไม่มีทรัพยากรหรือมีการเปลี่ยนแปลง Azure Resource Manager จะสร้างอินสแตนซ์คอนเทนเนอร์ใหม่และเรียกใช้สคริปต์การปรับใช้ภายในอินสแตนซ์คอนเทนเนอร์นั้น เอาต์พุตที่กําหนดจะถูกส่งกลับไปยัง Azure Resource Manager สําหรับการใช้งานในภายหลังในการปรับใช้
โครงสร้างสคริปต์การปรับใช้
เมื่อต้องเพิ่มลักษณะการทํางานแบบกําหนดเองกับแม่แบบ ARM คุณเริ่มต้น ด้วยทรัพยากร deploymentScripts อย่างน้อยคุณจําเป็นต้องให้รายละเอียดทั่วไปเช่น:
-
nameสําหรับทรัพยากรdeploymentScripts - ค่า
typeและapiVersion - ตําแหน่งที่ตั้ง (
locationค่า) ที่สร้างทรัพยากรสนับสนุน - วัตถุ
propertiesที่ว่างเปล่า อีกไม่นานคุณจะทําแบบนั้น
จําเป็นต้องมีสอง deploymentScripts- ค่าเฉพาะ:
kind: ชนิดของสคริปต์ที่จะเรียกใช้ (AzurePowerShellหรือAzureCLI){ "type": "Microsoft.Resources/deploymentScripts", "apiVersion": "2020-10-01", "name": "myFirstDeploymentScript", "location": "[resourceGroup().location]", "kind": "AzurePowerShell", "identity": { "type": "UserAssigned", "userAssignedIdentities": { "/subscriptions/01234567-89AB-CDEF-0123-456789ABCDEF/resourcegroups/deploymenttest/providers/Microsoft.ManagedIdentity/userAssignedIdentities/myscriptingid": {} } } }resource myFirstDeploymentScript 'Microsoft.Resources/deploymentScripts@2020-10-01' = { name: 'myFirstDeploymentScript' location: resourceGroup().location kind: 'AzurePowerShell' identity: { type: 'UserAssigned' userAssignedIdentities: { '/subscriptions/01234567-89AB-CDEF-0123-456789ABCDEF/resourcegroups/deploymenttest/providers/Microsoft.ManagedIdentity/userAssignedIdentities/myscriptingid': {} } } }identity: ข้อมูลประจําตัวที่มีการจัดการที่อินสแตนซ์คอนเทนเนอร์ใช้ คุณสามารถสร้างข้อมูลประจําตัวที่มีการจัดการไว้ล่วงหน้าและระบุได้ดังตัวอย่างต่อไปนี้ หรือคุณสามารถสร้างชุดเนื้อหาในเทมเพลตและอ้างอิงในแบบฝึกหัดถัดไปซึ่งคุณทําในแบบฝึกหัดถัดไป{ "type": "Microsoft.Resources/deploymentScripts", "apiVersion": "2020-10-01", "name": "myFirstDeploymentScript", "location": "[resourceGroup().location]", "kind": "AzurePowerShell", "identity": { "type": "UserAssigned", "userAssignedIdentities": { "/subscriptions/01234567-89AB-CDEF-0123-456789ABCDEF/resourcegroups/deploymenttest/providers/Microsoft.ManagedIdentity/userAssignedIdentities/myscriptingid": {} } } }resource myFirstDeploymentScript 'Microsoft.Resources/deploymentScripts@2020-10-01' = { name: 'myFirstDeploymentScript' location: resourceGroup().location kind: 'AzurePowerShell' identity: { type: 'UserAssigned' userAssignedIdentities: { '/subscriptions/01234567-89AB-CDEF-0123-456789ABCDEF/resourcegroups/deploymenttest/providers/Microsoft.ManagedIdentity/userAssignedIdentities/myscriptingid': {} } } }
หลังจากที่คุณตั้งค่าองค์ประกอบเหล่านั้นแล้ว คุณสามารถย้ายไปยังส่วน properties ของทรัพยากร deploymentScripts ได้ ส่วนหลักของส่วน properties คือ scriptContentซึ่งระบุสคริปต์จริงที่จะดําเนินการ:
"properties": {
"scriptContent": "
$output = 'Hello Learner!'
Write-Output $output
$DeploymentScriptOutputs = @{}
$DeploymentScriptOutputs['text'] = $output
",
}
properties: {
scriptContent: '''
$output = 'Hello Learner!'
Write-Output $output
$DeploymentScriptOutputs = @{}
$DeploymentScriptOutputs['text'] = $output
'''
}
โปรดสังเกตว่า scriptContent ใช้สตริงหลายบรรทัด ใน Bicep คุณสามารถระบุสตริงหลายบรรทัดโดยใช้เครื่องหมายอัญประกาศสามรายการด้วยกัน (''') ก่อนและหลังสตริงของคุณ
เป็นเรื่องปกติที่สคริปต์การปรับใช้จะส่งผ่านเอาต์พุตกลับไปยังการปรับใช้ ตัวอย่างเช่น ถ้าคุณกําลังใช้สคริปต์เพื่อค้นหาข้อมูลบางอย่างจาก API คุณอาจส่งผ่านข้อมูลกลับไปยังการปรับใช้เป็นเอาต์พุตได้ แหล่งข้อมูลอื่น ๆ ในการปรับใช้สามารถใช้ข้อมูลในข้อกําหนดของตนเองได้
สําหรับสคริปต์ PowerShell คุณส่งผลลัพธ์กลับมาโดยการสร้างตัวแปรที่ชื่อว่า $DeploymentScriptOutputsซึ่งต้องเป็นตารางแฮช สคริปต์ตัวอย่างจะเริ่มต้นตารางแฮช จากนั้นสร้างเอาต์พุตที่เรียกว่า textซึ่งรับค่าจากตัวแปรภายในเครื่อง $output:
$output = 'Hello Learner!'
Write-Output $output
$DeploymentScriptOutputs = @{}
$DeploymentScriptOutputs['text'] = $output
$output = 'Hello Learner!'
Write-Output $output
$DeploymentScriptOutputs = @{}
$DeploymentScriptOutputs['text'] = $output
ปลาย
คุณยังสามารถเขียนสคริปต์การปรับใช้ใน Bash ได้ เมื่อต้องสร้างผลลัพธ์จากสคริปต์ Bash คุณจําเป็นต้องสร้างไฟล์ JSON ในตําแหน่งที่ตั้งที่ระบุโดยตัวแปรสภาพแวดล้อม AZ_SCRIPTS_OUTPUT_PATH
ภายในส่วน properties คุณยังกําหนดตัวเลือกต่าง ๆ ที่ deploymentScripts สามารถทําได้ ในมอดูลนี้ เราจะทําให้มันง่ายและเพิ่มเพียงพอที่จะเรียกใช้สคริปต์ อย่างน้อยที่สุด คุณจําเป็นต้องระบุเวอร์ชันของ Azure PowerShell หรือ Azure CLI เพื่อใช้ สคริปต์เพื่อเรียกใช้ และช่วงเวลาการเก็บข้อมูล
ระยะเวลาการเก็บรักษาคือระยะเวลาที่ผลลัพธ์ควรเก็บไว้ถ้าคุณต้องการเก็บทรัพยากรไว้ ตามค่าเริ่มต้น ผลลัพธ์จะถูกลบออกหลังจากที่คุณเรียกใช้สคริปต์
"properties": {
"azPowerShellVersion": "3.0",
"scriptContent": "
$output = 'Hello Learner!'
Write-Output $output
$DeploymentScriptOutputs = @{}
$DeploymentScriptOutputs['text'] = $output
",
"retentionInterval": "P1D"
}
properties: {
azPowerShellVersion: '3.0'
scriptContent: '''
$output = 'Hello Learner!'
Write-Output $output
$DeploymentScriptOutputs = @{}
$DeploymentScriptOutputs['text'] = $output
'''
retentionInterval: 'P1D'
}
เทมเพลตแบบเต็มของเราจะมีลักษณะดังนี้:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.1",
"apiProfile": "",
"parameters": {},
"variables": {},
"functions": [],
"resources": [
{
"type": "Microsoft.Resources/deploymentScripts",
"apiVersion": "2020-10-01",
"name": "myFirstDeploymentScript",
"location": "[resourceGroup().location]",
"kind": "AzurePowerShell",
"identity": {
"type": "UserAssigned",
"userAssignedIdentities": {
"/subscriptions/01234567-89AB-CDEF-0123-456789ABCDEF/resourcegroups/deploymenttest/providers/Microsoft.ManagedIdentity/userAssignedIdentities/myscriptingid": {}
}
},
"properties": {
"azPowerShellVersion": "3.0",
"scriptContent": "
$output = 'Hello Learner!'
Write-Output $output
$DeploymentScriptOutputs = @{}
$DeploymentScriptOutputs['text'] = $output
",
"retentionInterval": "P1D"
}
}
],
"outputs": {
"scriptResult": {
"type": "string",
"value": "[reference('myFirstDeploymentScript').outputs.text]"
}
}
}
resource myFirstDeploymentScript 'Microsoft.Resources/deploymentScripts@2020-10-01' = {
name: 'myFirstDeploymentScript'
location: resourceGroup().location
kind: 'AzurePowerShell'
identity: {
type: 'UserAssigned'
userAssignedIdentities: {
'/subscriptions/01234567-89AB-CDEF-0123-456789ABCDEF/resourcegroups/deploymenttest/providers/Microsoft.ManagedIdentity/userAssignedIdentities/myscriptingid': {}
}
}
properties: {
azPowerShellVersion: '3.0'
scriptContent: '''
$output = 'Hello Learner!'
Write-Output $output
$DeploymentScriptOutputs = @{}
$DeploymentScriptOutputs['text'] = $output
'''
retentionInterval: 'P1D'
}
}
output scriptResult string = myFirstDeploymentScript.properties.outputs.text
รวมไฟล์สคริปต์
การฝังสคริปต์แบบอินไลน์ในเทมเพลตอาจยุ่งยาก อ่านและเข้าใจยาก และยากต่อการเปลี่ยนแปลง Bicep ใช้ฟังก์ชัน loadTextContent() เพื่อฝังไฟล์ข้อความภายนอกในการปรับใช้ของคุณ เมื่อ Bicep เคลื่อนย้ายเทมเพลตของคุณลงใน JSON เทมเพลตนั้นจะฝังไฟล์ภายนอกลงในเทมเพลตที่จะปล่อยออกมา
สมมติว่าคุณมีไฟล์ PowerShell ที่ชื่อ myscript.ps1 ในโฟลเดอร์เดียวกันกับเทมเพลต Bicep ของคุณ คุณสามารถบอก Bicep ให้ฝังไฟล์เช่นนี้:
properties: {
azPowerShellVersion: '3.0'
scriptContent: loadTextContent('myscript.ps1')
retentionInterval: 'P1D'
}
คุณสามารถค้นหาคุณสมบัติทั้งหมดสําหรับทรัพยากร deploymentScripts ในเอกสารอ้างอิงเทมเพลต ARM