假设我也有很多*.ps1脚本可以转换成ARM模板
有没有一种方法(脚本、命令等)我可以自动转换成一个蓝色的powershell *.ps1到一个ARM模板,不需要实际上做的部署到Azure?
我不是来找防弹衣的。如果确实有一种自动转换的方法,如果ps1脚本不正确,转换就会失败,我对此没有意见。
发布于 2017-08-12 07:02:43
不,没有办法做到这一点(除非您可以自动化部署+导出,这将创建有缺陷的模板)。
最接近这一点的是使用-Debug
开关运行所有cmdlet并捕获它们正在执行的HTTP请求,并将这些请求转换为ARM模板(不应该太硬、复制粘贴和一堆编辑)。
发布于 2018-07-17 12:36:27
您可以在任何与创建资源相关的Azure Powershell cmdlet上运行"-Debug“标志,比如,输出将显示它将要创建的ARM模板:
请小心使用这一点,因为我发现Powershell cmdlet并不是您应该自动化部署的方式。您应该严格使用ARM,因为Powershell cmdlet有时不输出成功部署所需的正确参数,因为Powershell cmdlet没有指定要使用的ARM API版本的方法。
使用带有"-Debug“标志的”“输出示例:
New-AzureRmVM -ResourceGroupName $RGName -Location $Location -VM $VM -LicenseType "Windows_Server" -Debug
DEBUG: ============================ HTTP REQUEST ============================
HTTP Method:
PUT
Absolute Uri:
https://management.azure.com/subscriptions/<subscription>/resourceGroups/LL_SQL_Test/providers/Microsoft.Compute/virtualMachines/LLSQL3?api-version=2018-04-01
Headers:
x-ms-client-request-id : 5920b683-e8fe-455e-969a-63f4c6e246d7
accept-language : en-US
Body:
{
"properties": {
"hardwareProfile": {
"vmSize": "Standard_DS2_v2"
},
"storageProfile": {
"osDisk": {
"osType": "Windows",
"image": {
"uri": "https://<storageaccount>.blob.core.windows.net/vhds/<VM>.vhd"
},
"caching": "ReadWrite",
"writeAcceleratorEnabled": false,
"createOption": "FromImage",
"diskSizeGB": 127,
"managedDisk": {
"storageAccountType": "Standard_LRS"
}
}
},
"osProfile": {
"computerName": "<computername>",
"adminUsername": "<username>",
"adminPassword": "<Password>",
"windowsConfiguration": {
"provisionVMAgent": true,
"enableAutomaticUpdates": true
}
},
"networkProfile": {
"networkInterfaces": [
{
"id": "/subscriptions/<subscription>/resourceGroups/<resourcegroup>/providers/Microsoft.Network/networkInterfaces/<NIC>"
}
]
},
"diagnosticsProfile": {
"bootDiagnostics": {
"enabled": false
}
},
"availabilitySet": {
"id": "/subscriptions/<subscription>/resourceGroups/<resourcegroup>/providers/Microsoft.Compute/availabilitySets/SQL_Availability_Set_Test"
},
"licenseType": "Windows_Server"
},
"location": "West US"
}
以上是使用Powershell的“为什么不”的完美示例,因为当前,这将返回一个错误:
Body:
{
"error": {
"code": "InvalidParameter",
"target": "osDisk.image",
"message": "Parameter 'osDisk.image' is not allowed."
}
}
由于API版本(2018-04-01),Powershell命令用于将Powershell输入转换为JSON模板,因此不允许参数‘osDisk.Image’,因为它期望将其格式化为:
"storageProfile": {
"imageReference": {
"id": "[resourceId('Microsoft.Compute/images', parameters('images_LL_SQL_IMG_name'))]"
},
"osDisk": {
"osType": "Windows",
相反,它正在使用
"storageProfile": {
"osDisk": {
"osType": "Windows",
"image": {
"uri": "https://<storageaccount>.blob.core.windows.net/vhds/LLSQL220180621090257.vhd"
},
发布于 2017-08-12 14:47:18
正如其他人所评论的那样,没有办法自动将PowerShell脚本转换为ARM模板。但是,如果已经部署了这些资源,则可以考虑使用ARM导出功能检索ARM模板。
https://learn.microsoft.com/en-us/azure/azure-resource-manager/resource-manager-export-template
https://stackoverflow.com/questions/45649608
复制相似问题