انشر قالب Azure Resource Manager في دفتر تشغيل Automation PowerShell
يمكنك كتابة دفتر تشغيل Automation PowerShell الذي ينشر مورد Azure باستخدام قالب Azure Resource Manager . تسمح لك القوالب باستخدام Azure Automation لأتمتة نشر موارد Azure. يمكنك الاحتفاظ بقوالب Resource Manager في موقع مركزي وآمن، مثل تخزين Azure Storage.
في هذه المقالة، نقوم بإنشاء دفتر تشغيل PowerShell يستخدم قالب إدارة الموارد المخزن في تخزين Azure لنشر حساب Azure Storage جديد.
في حال لم يكن لديك اشتراك Azure، فأنشئ حساباً مجانيّاً قبل البدء.
المتطلبات الأساسية
حساب Azure Automation مع هوية مدارة معينة من قبل مستخدم واحد على الأقل. لمزيد من المعلومات، راجع استخدام هوية مُدارة يعينها المستخدم لحساب Azure Automation.
وحدات Az:
Az.Accounts
وAz.ManagedServiceIdentity
وAz.Resources
وAz.Storage
. استيرادها إلى حساب Automation. لمزيد من المعلومات، راجع استيراد وحدات Az.حساب تخزين Azure لتخزين قالب Resource Manager.
تم تثبيت Azure PowerShell على جهاز محلي. راجع تثبيت وحدة Azure PowerShell النمطية للحصول على معلومات حول كيفية الحصول على Azure PowerShell. ستحتاج أيضًا إلى الوحدة النمطية Az.ManagedServiceIdentity .
Az.ManagedServiceIdentity
هي وحدة معاينة وليست مثبتة كجزء من الوحدة النمطية Az. للتمكن من تثبيته، احرص على تشغيلInstall-Module -Name Az.ManagedServiceIdentity
تعيين أذونات للهويات المدارة
قم بتعيين أذونات للهويات المُدارة للقيام بالمهام المتعلقة بالتخزين في Runbook.
سجّل الدخول إلى Azure بشكل تفاعلي باستخدام Connect-AzAccount cmdlet واتبع الإرشادات.
# Sign in to your Azure subscription $sub = Get-AzSubscription -ErrorAction SilentlyContinue if(-not($sub)) { Connect-AzAccount } # If you have multiple subscriptions, set the one to use # Select-AzSubscription -SubscriptionId <SUBSCRIPTIONID>
قم بتوفير قيمة مناسبة للمتغيرات أدناه ثم قم بتنفيذ البرنامج النصي.
$resourceGroup = "resourceGroup" $automationAccount = "automationAccount" $storageAccount = "storageAccount" $userAssignedManagedIdentity = "userAssignedManagedIdentity" $storageTemplate = "path\storageTemplate.json" $runbookScript = "path\runbookScript.ps1"
عيّن الدور
reader
للهوية المدارة المعينة من قبل النظام لتنفيذ الأمر cmdletGet-AzUserAssignedIdentity
.$SAMI = (Get-AzAutomationAccount -ResourceGroupName $resourceGroup -Name $automationAccount).Identity.PrincipalId New-AzRoleAssignment ` -ObjectId $SAMI ` -ResourceGroupName $resourceGroup ` -RoleDefinitionName "Reader"
قم بتعيين الدور
Storage Account Contributor
للهوية المُدارة التي عيّنها المستخدم للإجراءات المتخذة ضد حساب التخزين.$UAMI_ID = (Get-AzUserAssignedIdentity -ResourceGroupName $resourceGroup -Name $userAssignedManagedIdentity).PrincipalId New-AzRoleAssignment ` -ObjectId $UAMI_ID ` -ResourceGroupName $resourceGroup ` -RoleDefinitionName "Storage Account Contributor"
إنشاء قالب Resource Manager
في هذا المثال، تستخدم قالب إدارة الموارد الذي ينشر حساب Azure Storage جديدًا. قم بإنشاء ملف محلي يسمى storageTemplate.json
ثم الصق الكود التالي:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"storageAccountType": {
"type": "string",
"defaultValue": "Standard_LRS",
"allowedValues": [
"Standard_LRS",
"Standard_GRS",
"Standard_ZRS",
"Premium_LRS"
],
"metadata": {
"description": "Storage Account type"
}
},
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]",
"metadata": {
"description": "Location for all resources."
}
}
},
"variables": {
"storageAccountName": "[concat(uniquestring(resourceGroup().id), 'standardsa')]"
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"name": "[variables('storageAccountName')]",
"apiVersion": "2018-02-01",
"location": "[parameters('location')]",
"sku": {
"name": "[parameters('storageAccountType')]"
},
"kind": "Storage",
"properties": {
}
}
],
"outputs": {
"storageAccountName": {
"type": "string",
"value": "[variables('storageAccountName')]"
}
}
}
حفظ قالب Resource Manager في ملفات Azure
استخدم PowerShell لإنشاء مشاركة ملف Azure وتحميله storageTemplate.json
. للحصول على إرشادات حول كيفية إنشاء مشاركة ملف وتحميل ملف في مدخل Microsoft Azure، راجع بدء استخدام ملفات Azure على Windows .
قم بتشغيل الأوامر التالية لإنشاء مشاركة ملف وتحميل قالب Resource Manager إلى مشاركة الملف تلك.
# Get the access key for your storage account
$key = Get-AzStorageAccountKey -ResourceGroupName $resourceGroup -Name $storageAccount
# Create an Azure Storage context using the first access key
$context = New-AzStorageContext -StorageAccountName $storageAccount -StorageAccountKey $key[0].value
# Create a file share named 'resource-templates' in your Azure Storage account
$fileShare = New-AzStorageShare -Name 'resource-templates' -Context $context
# Add the storageTemplate.json file to the new file share
Set-AzStorageFileContent -ShareName $fileShare.Name -Context $context -Source $storageTemplate
إنشاء البرنامج النصي لدفتر تشغيل PowerShell
أنشئ برنامج PowerShell نصيًا يحصل على الملف storageTemplate.json
من Azure Storage وينشر القالب لإنشاء حساب Azure Storage جديد. قم بإنشاء ملف محلي يسمى runbookScript.ps1
ثم الصق الكود التالي:
param (
[Parameter(Mandatory=$true)]
[string]
$resourceGroup,
[Parameter(Mandatory=$true)]
[string]
$storageAccount,
[Parameter(Mandatory=$true)]
[string]
$storageAccountKey,
[Parameter(Mandatory=$true)]
[string]
$storageFileName,
[Parameter(Mandatory=$true)]
[string]
$userAssignedManagedIdentity
)
# Ensures you do not inherit an AzContext in your runbook
Disable-AzContextAutosave -Scope Process
# Connect to Azure with user-assigned managed identity
$AzureContext = (Connect-AzAccount -Identity).context
$identity = Get-AzUserAssignedIdentity -ResourceGroupName $resourceGroup `
-Name $userAssignedManagedIdentity `
-DefaultProfile $AzureContext
$AzureContext = (Connect-AzAccount -Identity -AccountId $identity.ClientId).context
# set and store context
$AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription `
-DefaultProfile $AzureContext
#Set the parameter values for the Resource Manager template
$Parameters = @{
"storageAccountType"="Standard_LRS"
}
# Create a new context
$Context = New-AzStorageContext -StorageAccountName $storageAccount -StorageAccountKey $storageAccountKey
Get-AzStorageFileContent `
-ShareName 'resource-templates' `
-Context $Context `
-path 'storageTemplate.json' `
-Destination 'C:\Temp' -Force
$TemplateFile = Join-Path -Path 'C:\Temp' -ChildPath $storageFileName
# Deploy the storage account
New-AzResourceGroupDeployment `
-ResourceGroupName $resourceGroup `
-TemplateFile $TemplateFile `
-TemplateParameterObject $Parameters
استيراد ونشر دفتر التشغيل في حساب Azure Automation الخاص بك
استخدم PowerShell لاستيراد دفتر التشغيل إلى حساب الأتمتة الخاص بك، ثم انشر دفتر التشغيل. للحصول على معلومات حول استيراد ونشر كتيبات التشغيل في مدخل Microsoft Azure، راجع إدارة كتيبات التشغيل في Azure Automation .
لاستيراد runbookScript.ps1
إلى حساب الأتمتة الخاص بك كدفتر تشغيل PowerShell، قم بتشغيل أوامر PowerShell التالية:
$importParams = @{
Path = $runbookScript
ResourceGroupName = $resourceGroup
AutomationAccountName = $automationAccount
Type = "PowerShell"
}
Import-AzAutomationRunbook @importParams
# Publish the runbook
$publishParams = @{
ResourceGroupName = $resourceGroup
AutomationAccountName = $automationAccount
Name = "runbookScript"
}
Publish-AzAutomationRunbook @publishParams
بدء تشغيل دفتر التشغيل
لنبدأ الآن باستخدام دفتر التشغيل عن طريق استدعاء Start-AzAutomationRunbook cmdlet. للحصول على معلومات حول كيفية بدء تشغيل دفتر في مدخل Microsoft Azure، راجع بدء دفتر تشغيل في أتمتة Azure .
تشغيل الأوامر التالية في وحدة التحكم الخاصة بـPowerShell:
# Set up the parameters for the runbook
$runbookParams = @{
resourceGroup = $resourceGroup
storageAccount = $storageAccount
storageAccountKey = $key[0].Value # We got this key earlier
storageFileName = "storageTemplate.json"
userAssignedManagedIdentity = $userAssignedManagedIdentity
}
# Set up parameters for the Start-AzAutomationRunbook cmdlet
$startParams = @{
resourceGroup = $resourceGroup
AutomationAccountName = $automationAccount
Name = "runbookScript"
Parameters = $runbookParams
}
# Start the runbook
$job = Start-AzAutomationRunbook @startParams
بعد تشغيل دفتر التشغيل، يمكنك التحقق من حالته عن طريق استرداد قيمة خاصية عنصر الوظيفة $job.Status
.
يحصل دفتر التشغيل على قالب Resource Manager ويستخدمه لنشر حساب Azure Storage جديد. يمكنك رؤية إنشاء حساب التخزين الجديد عن طريق تشغيل الأمر التالي:
Get-AzStorageAccount
الخطوات التالية
- لمعرفة المزيد حول قوالب إدارة الموارد، راجع نظرة عامة على Azure Resource Manager .
- لبدء استخدام Azure Storage، راجع مقدمة عن تخزين Azure .
- للعثور على كتيبات تشغيل Azure Automation مفيدة أخرى، راجع استخدام دفاتر التشغيل والوحدات النمطية في Azure Automation .